when i implemented a test a case against the API call to test the reducer, we can use NOCK to mock the API with JEST test run.
here is the sample code for the above test case.
however i ran the test case with JEST, the following error came immediately. since i use fetch function from fetch-node module.
● fetchUser action should add user object to state
ReferenceError: fetch is not defined
23 | types:[FETCH_USER_REQUEST, FETCH_USER_SUCCESS, FETCH_USER_FAILURE],
24 | promise: fetch(`http://localhost:8080/api/users/${username}`)
> 25 | .then(response=>response.json())
| ^
26 | })
27 |
28 | export const createUser=(username, realname, password)=>thunkCreator({
at fetchUser (src/actions/users.js:25:6)
at Object.<anonymous> (_test_/reducers/users.test.js:88:18)
● fetchUser action should add user object to state
expect.assertions(1)
Expected one assertion to be called but received zero assertion calls.
84 | .reply(200, userObj)
85 |
> 86 | expect.assertions(1)
| ^
87 |
88 | const action = fetchUser(username)
89 | return store.dispatch(action)
at Object.<anonymous> (_test_/reducers/users.test.js:86:10)
the fix is very easy, we just have to add the reference to fetch in the test file. the test case was passed and succeed.
No comments:
Post a Comment