Skip to content

Commit 0e73bcc

Browse files
HeinrichFiltergaearon
authored andcommitted
reduxjs#1944 converting test suite from Mocha to Jest (reduxjs#1951)
* Fix Jest spy code in documentation While looking into reduxjs#1944 I noticed that the examples in the documentation still uses expect's createSpy() instead of Jest's fn() approach * reduxjs#1944 Converting tests from Mocha to Jest Added Jest dependencies Removed expect from files Updated mocks to use `jest.fn()` Current `expect.spyOn` replacement can be improved on TODO: Fix linter not to complain about using `console` * Disable eslint no-console for console related tests * Made changes suggested by @Florian-R that enable no-args jest.fn() calls https://github.com/reactjs/redux/pull/1951/files/927bf45f1c7666e42ae8c4c390cc726a75cab903#r78733124 * Removed moch, expect and isparta Updated test:cov command to use jest's --coverage option * Update typescript.spec.js
1 parent 91c6715 commit 0e73bcc

10 files changed

+157
-142
lines changed

docs/recipes/WritingTests.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ import Header from '../../components/Header'
304304

305305
function setup() {
306306
const props = {
307-
addTodo: expect.createSpy()
307+
addTodo: jest.fn()
308308
}
309309

310310
const enzymeWrapper = shallow(<Header {...props} />)
@@ -333,9 +333,9 @@ describe('components', () => {
333333
const { enzymeWrapper, props } = setup()
334334
const input = enzymeWrapper.find('TodoTextInput')
335335
input.props().onSave('')
336-
expect(props.addTodo.calls.length).toBe(0)
336+
expect(props.addTodo.mock.calls.length).toBe(0)
337337
input.props().onSave('Use Redux')
338-
expect(props.addTodo.calls.length).toBe(1)
338+
expect(props.addTodo.mock.calls.length).toBe(1)
339339
})
340340
})
341341
})

package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
"lint": "npm run lint:src && npm run lint:examples",
1919
"lint:src": "eslint src test build",
2020
"lint:examples": "eslint examples",
21-
"test": "cross-env BABEL_ENV=commonjs mocha --compilers js:babel-register --recursive",
21+
"test": "cross-env BABEL_ENV=commonjs jest",
2222
"test:watch": "npm test -- --watch",
23-
"test:cov": "cross-env BABEL_ENV=commonjs babel-node $(npm bin)/isparta cover $(npm bin)/_mocha -- --recursive",
23+
"test:cov": "npm test -- --coverage",
2424
"test:examples": "cross-env BABEL_ENV=commonjs babel-node examples/testAll.js",
2525
"check:src": "npm run lint:src && npm run test",
2626
"check:examples": "npm run build:examples && npm run lint:examples && npm run test:examples",
@@ -73,6 +73,7 @@
7373
"babel-cli": "^6.3.15",
7474
"babel-core": "^6.3.15",
7575
"babel-eslint": "^4.1.6",
76+
"babel-jest": "^15.0.0",
7677
"babel-loader": "^6.2.0",
7778
"babel-plugin-check-es2015-constants": "^6.3.13",
7879
"babel-plugin-transform-es2015-arrow-functions": "^6.3.13",
@@ -101,11 +102,9 @@
101102
"eslint": "^1.10.3",
102103
"eslint-config-rackt": "^1.1.1",
103104
"eslint-plugin-react": "^3.16.1",
104-
"expect": "^1.8.0",
105105
"gitbook-cli": "^2.3.0",
106106
"glob": "^6.0.4",
107-
"isparta": "^4.0.0",
108-
"mocha": "^2.2.5",
107+
"jest": "^15.1.1",
109108
"rimraf": "^2.3.4",
110109
"rxjs": "^5.0.0-beta.6",
111110
"typescript": "^1.8.0",
@@ -125,5 +124,8 @@
125124
"transform": [
126125
"loose-envify"
127126
]
127+
},
128+
"jest": {
129+
"testRegex": "(/test/.*\\.spec.js)$"
128130
}
129131
}

test/.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"env": {
3-
"mocha": true
3+
"jest": true
44
}
55
}

test/applyMiddleware.spec.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import expect from 'expect'
21
import { createStore, applyMiddleware } from '../src/index'
32
import * as reducers from './helpers/reducers'
43
import { addTodo, addTodoAsync, addTodoIfEmpty } from './helpers/actionCreators'
@@ -13,15 +12,15 @@ describe('applyMiddleware', () => {
1312
}
1413
}
1514

16-
const spy = expect.createSpy(() => {})
15+
const spy = jest.fn()
1716
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos)
1817

1918
store.dispatch(addTodo('Use Redux'))
2019
store.dispatch(addTodo('Flux FTW!'))
2120

22-
expect(spy.calls.length).toEqual(1)
21+
expect(spy.mock.calls.length).toEqual(1)
2322

24-
expect(Object.keys(spy.calls[0].arguments[0])).toEqual([
23+
expect(Object.keys(spy.mock.calls[0][0])).toEqual([
2524
'getState',
2625
'dispatch'
2726
])
@@ -37,11 +36,11 @@ describe('applyMiddleware', () => {
3736
}
3837
}
3938

40-
const spy = expect.createSpy(() => {})
39+
const spy = jest.fn()
4140
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos)
4241

4342
return store.dispatch(addTodoAsync('Use Redux')).then(() => {
44-
expect(spy.calls.length).toEqual(2)
43+
expect(spy.mock.calls.length).toEqual(2)
4544
})
4645
})
4746

test/bindActionCreators.spec.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import expect from 'expect'
21
import { bindActionCreators, createStore } from '../src'
32
import { todos } from './helpers/reducers'
43
import * as actionCreators from './helpers/actionCreators'

test/combineReducers.spec.js

+42-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import expect from 'expect'
1+
/* eslint-disable no-console */
22
import { combineReducers } from '../src'
33
import createStore, { ActionTypes } from '../src/createStore'
44

@@ -32,21 +32,24 @@ describe('Utils', () => {
3232
})
3333

3434
it('warns if a reducer prop is undefined', () => {
35-
const spy = expect.spyOn(console, 'error')
35+
const preSpy = console.error
36+
const spy = jest.fn()
37+
console.error = spy
3638

3739
let isNotDefined
3840
combineReducers({ isNotDefined })
39-
expect(spy.calls[0].arguments[0]).toMatch(
41+
expect(spy.mock.calls[0][0]).toMatch(
4042
/No reducer provided for key "isNotDefined"/
4143
)
4244

43-
spy.reset()
45+
spy.mockClear()
4446
combineReducers({ thing: undefined })
45-
expect(spy.calls[0].arguments[0]).toMatch(
47+
expect(spy.mock.calls[0][0]).toMatch(
4648
/No reducer provided for key "thing"/
4749
)
4850

49-
spy.restore()
51+
spy.mockClear()
52+
console.error = preSpy
5053
})
5154

5255
it('throws an error if a reducer returns undefined handling an action', () => {
@@ -166,7 +169,7 @@ describe('Utils', () => {
166169
})
167170

168171
const initialState = reducer(undefined, '@@INIT')
169-
expect(reducer(initialState, { type: 'increment' })).toNotBe(initialState)
172+
expect(reducer(initialState, { type: 'increment' })).not.toBe(initialState)
170173
})
171174

172175
it('throws an error on first call if a reducer attempts to handle a private action', () => {
@@ -191,17 +194,24 @@ describe('Utils', () => {
191194
})
192195

193196
it('warns if no reducers are passed to combineReducers', () => {
194-
const spy = expect.spyOn(console, 'error')
197+
const preSpy = console.error
198+
const spy = jest.fn()
199+
console.error = spy
200+
195201
const reducer = combineReducers({ })
196202
reducer({ })
197-
expect(spy.calls[0].arguments[0]).toMatch(
203+
expect(spy.mock.calls[0][0]).toMatch(
198204
/Store does not have a valid reducer/
199205
)
200-
spy.restore()
206+
spy.mockClear()
207+
console.error = preSpy
201208
})
202209

203210
it('warns if input state does not match reducer shape', () => {
204-
const spy = expect.spyOn(console, 'error')
211+
const preSpy = console.error
212+
const spy = jest.fn()
213+
console.error = spy
214+
205215
const reducer = combineReducers({
206216
foo(state = { bar: 1 }) {
207217
return state
@@ -212,69 +222,75 @@ describe('Utils', () => {
212222
})
213223

214224
reducer()
215-
expect(spy.calls.length).toBe(0)
225+
expect(spy.mock.calls.length).toBe(0)
216226

217227
reducer({ foo: { bar: 2 } })
218-
expect(spy.calls.length).toBe(0)
228+
expect(spy.mock.calls.length).toBe(0)
219229

220230
reducer({
221231
foo: { bar: 2 },
222232
baz: { qux: 4 }
223233
})
224-
expect(spy.calls.length).toBe(0)
234+
expect(spy.mock.calls.length).toBe(0)
225235

226236
createStore(reducer, { bar: 2 })
227-
expect(spy.calls[0].arguments[0]).toMatch(
237+
expect(spy.mock.calls[0][0]).toMatch(
228238
/Unexpected key "bar".*createStore.*instead: "foo", "baz"/
229239
)
230240

231241
createStore(reducer, { bar: 2, qux: 4, thud: 5 })
232-
expect(spy.calls[1].arguments[0]).toMatch(
242+
expect(spy.mock.calls[1][0]).toMatch(
233243
/Unexpected keys "qux", "thud".*createStore.*instead: "foo", "baz"/
234244
)
235245

236246
createStore(reducer, 1)
237-
expect(spy.calls[2].arguments[0]).toMatch(
247+
expect(spy.mock.calls[2][0]).toMatch(
238248
/createStore has unexpected type of "Number".*keys: "foo", "baz"/
239249
)
240250

241251
reducer({ corge: 2 })
242-
expect(spy.calls[3].arguments[0]).toMatch(
252+
expect(spy.mock.calls[3][0]).toMatch(
243253
/Unexpected key "corge".*reducer.*instead: "foo", "baz"/
244254
)
245255

246256
reducer({ fred: 2, grault: 4 })
247-
expect(spy.calls[4].arguments[0]).toMatch(
257+
expect(spy.mock.calls[4][0]).toMatch(
248258
/Unexpected keys "fred", "grault".*reducer.*instead: "foo", "baz"/
249259
)
250260

251261
reducer(1)
252-
expect(spy.calls[5].arguments[0]).toMatch(
262+
expect(spy.mock.calls[5][0]).toMatch(
253263
/reducer has unexpected type of "Number".*keys: "foo", "baz"/
254264
)
255265

256-
spy.restore()
266+
spy.mockClear()
267+
console.error = preSpy
257268
})
258269

259270
it('only warns for unexpected keys once', () => {
260-
const spy = expect.spyOn(console, 'error')
271+
const preSpy = console.error
272+
const spy = jest.fn()
273+
console.error = spy
274+
261275
const foo = (state = { foo: 1 }) => state
262276
const bar = (state = { bar: 2 }) => state
263277

264-
expect(spy.calls.length).toBe(0)
278+
expect(spy.mock.calls.length).toBe(0)
265279
const reducer = combineReducers({ foo, bar })
266280
const state = { foo: 1, bar: 2, qux: 3 }
267281
reducer(state, {})
268282
reducer(state, {})
269283
reducer(state, {})
270284
reducer(state, {})
271-
expect(spy.calls.length).toBe(1)
285+
expect(spy.mock.calls.length).toBe(1)
272286
reducer({ ...state, baz: 5 }, {})
273287
reducer({ ...state, baz: 5 }, {})
274288
reducer({ ...state, baz: 5 }, {})
275289
reducer({ ...state, baz: 5 }, {})
276-
expect(spy.calls.length).toBe(2)
277-
spy.restore()
290+
expect(spy.mock.calls.length).toBe(2)
291+
292+
spy.mockClear()
293+
console.error = preSpy
278294
})
279295
})
280296
})

test/compose.spec.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import expect from 'expect'
21
import { compose } from '../src'
32

43
describe('Utils', () => {

0 commit comments

Comments
 (0)