Jest: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 66: Line 66:
     await delay(100)
     await delay(100)
  );
  );
=Mocking=
Create directory __mocking__ at the same level as node_modules
Here is an example for mocking fetch package isomorphic-fetch
<syntaxhighlight lang="javascript">
let __value = 42;
const isomorphicFetch = jest.fn( ()=> __value);
isomorphicFetch.__setValue = v => __value = v;
export default  isomorphicFetch;
</syntaxhighlight>
And the test case. We import the function to be mocked from out __mocks__/isomorphic-fetch.js
<syntaxhighlight lang="javascript">
import { handleFetchQuestion } from './fetch-question-saga';
import fetch from 'isomorphic-fetch';
describe("Fetch question s saga", ()=>{
    beforeAll( () => {
        fetch.__setValue([{question_id:42}] );
    });
    it ("should fetch the question saga", async ()=> {
        const gen = handleFetchQuestion({question_id:42});
        const { value } = await gen.next();
        expect(value).toEqual( [{question_id:42}] )
        expect(fetch).toHaveBeenCalledWith('/api/questions/42');
    });
});
</syntaxhighlight>

Revision as of 02:56, 21 May 2020

Naming Test

__tests__/*.js
*.spec.js
*.test.js

Example Test

Describe is the suite, it is the test

describe("The question list ", ()=> {
    it ("should display a list of items", ()=> {
        expect(2+2).toEqual(4);
    })
    it ("should display a list of items", ()=> {
        expect(2+4).toEqual(6);
    })
})

Setup and Teardown

This can be done using

BeforeEach BeforeAll
beforeAll(()=>{
   console.log("Hello");
});

and

AfterEach AfterAll
afterAll(()=>{
   console.log("Hello");
});

Note does not matter what order you write them in.

Skipping and Isolating Tests

Add keyword only to include tests

    it.only ("should display a list of items", ()=> {
        expect(2+2).toEqual(4);
    })

Add keyword skip to exlude tests

    it.skip ("should display a list of items", ()=> {
        expect(2+2).toEqual(4);
    })

Async Tests

it('async test 1', done=> {
    setTimeout(done, 100)
});
it('async test 2',()=> {
    return new Promise(
        resolve => setTimeout(resolve,100)
    );
});
it('async test 1', async ()=>
    await delay(100)
);

Mocking

Create directory __mocking__ at the same level as node_modules

Here is an example for mocking fetch package isomorphic-fetch

 let __value = 42;
 const isomorphicFetch = jest.fn( ()=> __value);
 isomorphicFetch.__setValue = v => __value = v;
 export default  isomorphicFetch;

And the test case. We import the function to be mocked from out __mocks__/isomorphic-fetch.js

import { handleFetchQuestion } from './fetch-question-saga';
import fetch from 'isomorphic-fetch';

 describe("Fetch question s saga", ()=>{
 
     beforeAll( () => {
         fetch.__setValue([{question_id:42}] );
     }); 
 
     it ("should fetch the question saga", async ()=> {
         const gen = handleFetchQuestion({question_id:42});
         const { value } = await gen.next(); 

         expect(value).toEqual( [{question_id:42}] )
         expect(fetch).toHaveBeenCalledWith('/api/questions/42');
     });
 });