Skip to content

Commit 4a35430

Browse files
committed
Switch tests to Vitest
1 parent a459be6 commit 4a35430

11 files changed

+98
-87
lines changed

.github/workflows/test.yaml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
name: Check for changes
77
runs-on: ubuntu-latest
88
outputs:
9-
toolkit: ${{ steps.filter.outputs.toolkit }}
9+
src: ${{ steps.filter.outputs.src }}
1010
steps:
1111
- uses: actions/checkout@v2
1212
- uses: dorny/paths-filter@v2
@@ -15,10 +15,11 @@ jobs:
1515
filters: |
1616
src:
1717
- 'src/**'
18+
- 'test/**'
1819
1920
build:
2021
needs: changes
21-
#if: ${{ needs.changes.outputs.src == 'true' }}
22+
if: ${{ needs.changes.outputs.src == 'true' }}
2223

2324
name: Lint, Test, Build & Pack on Node ${{ matrix.node }}
2425

@@ -82,13 +83,7 @@ jobs:
8283
- name: Install build artifact
8384
run: yarn add ./package.tgz
8485

85-
- run: sed -i -e /@remap-prod-remove-line/d ./tsconfig.json ./jest.config.cjs ./test/tsconfig.json ./test/typescript/tsconfig.json
86-
87-
- run: cat ./tsconfig.json
88-
- run: cat ./test/tsconfig.json
89-
- run: cat ./test/typescript/tsconfig.json
90-
91-
- run: ls -lah ./node_modules/redux
86+
- run: sed -i -e /@remap-prod-remove-line/d ./tsconfig.json ./vitest.config.ts ./test/tsconfig.json ./test/typescript/tsconfig.json
9287

9388
- name: Run tests, against dist
9489
run: yarn test

jest.config.cjs

Lines changed: 0 additions & 19 deletions
This file was deleted.

test/applyMiddleware.spec.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Store,
99
Dispatch
1010
} from 'redux'
11+
import { vi } from 'vitest'
1112
import * as reducers from './helpers/reducers'
1213
import { addTodo, addTodoAsync, addTodoIfEmpty } from './helpers/actionCreators'
1314
import { thunk } from './helpers/middleware'
@@ -34,7 +35,7 @@ describe('applyMiddleware', () => {
3435
}
3536
}
3637

37-
const spy = jest.fn()
38+
const spy = vi.fn()
3839
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos)
3940

4041
store.dispatch(addTodo('Use Redux'))
@@ -59,7 +60,7 @@ describe('applyMiddleware', () => {
5960
}
6061
}
6162

62-
const spy = jest.fn()
63+
const spy = vi.fn()
6364
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos)
6465

6566
// the typing for redux-thunk is super complex, so we will use an as unknown hack
@@ -71,7 +72,7 @@ describe('applyMiddleware', () => {
7172
})
7273
})
7374

74-
it('works with thunk middleware', done => {
75+
it('works with thunk middleware', async () => {
7576
const store = applyMiddleware(thunk)(createStore)(reducers.todos)
7677

7778
store.dispatch(addTodoIfEmpty('Hello') as any)
@@ -106,27 +107,25 @@ describe('applyMiddleware', () => {
106107
const dispatchedValue = store.dispatch(
107108
addTodoAsync('Maybe') as any
108109
) as unknown as Promise<void>
109-
dispatchedValue.then(() => {
110-
expect(store.getState()).toEqual([
111-
{
112-
id: 1,
113-
text: 'Hello'
114-
},
115-
{
116-
id: 2,
117-
text: 'World'
118-
},
119-
{
120-
id: 3,
121-
text: 'Maybe'
122-
}
123-
])
124-
done()
125-
})
110+
await dispatchedValue
111+
expect(store.getState()).toEqual([
112+
{
113+
id: 1,
114+
text: 'Hello'
115+
},
116+
{
117+
id: 2,
118+
text: 'World'
119+
},
120+
{
121+
id: 3,
122+
text: 'Maybe'
123+
}
124+
])
126125
})
127126

128127
it('passes through all arguments of dispatch calls from within middleware', () => {
129-
const spy = jest.fn()
128+
const spy = vi.fn()
130129
const testCallArgs = ['test']
131130

132131
interface MultiDispatch<A extends Action = AnyAction> {

test/combineReducers.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
AnyAction,
77
__DO_NOT_USE__ActionTypes as ActionTypes
88
} from 'redux'
9+
import { vi } from 'vitest'
910

1011
describe('Utils', () => {
1112
describe('combineReducers', () => {
@@ -39,7 +40,7 @@ describe('Utils', () => {
3940

4041
it('warns if a reducer prop is undefined', () => {
4142
const preSpy = console.error
42-
const spy = jest.fn()
43+
const spy = vi.fn()
4344
console.error = spy
4445

4546
let isNotDefined: any
@@ -199,7 +200,7 @@ describe('Utils', () => {
199200

200201
it('warns if no reducers are passed to combineReducers', () => {
201202
const preSpy = console.error
202-
const spy = jest.fn()
203+
const spy = vi.fn()
203204
console.error = spy
204205

205206
const reducer = combineReducers({})
@@ -214,7 +215,7 @@ describe('Utils', () => {
214215

215216
it('warns if input state does not match reducer shape', () => {
216217
const preSpy = console.error
217-
const spy = jest.fn()
218+
const spy = vi.fn()
218219
const nullAction = undefined as unknown as AnyAction
219220
console.error = spy
220221

@@ -287,7 +288,7 @@ describe('Utils', () => {
287288

288289
it('only warns for unexpected keys once', () => {
289290
const preSpy = console.error
290-
const spy = jest.fn()
291+
const spy = vi.fn()
291292
console.error = spy
292293
const nullAction = { type: '' }
293294

test/createStore.spec.ts

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Action,
66
Store
77
} from 'redux'
8+
import { vi } from 'vitest'
89
import {
910
addTodo,
1011
dispatchInMiddle,
@@ -205,8 +206,8 @@ describe('createStore', () => {
205206

206207
it('supports multiple subscriptions', () => {
207208
const store = createStore(reducers.todos)
208-
const listenerA = jest.fn()
209-
const listenerB = jest.fn()
209+
const listenerA = vi.fn()
210+
const listenerB = vi.fn()
210211

211212
let unsubscribeA = store.subscribe(listenerA)
212213
store.dispatch(unknownAction())
@@ -252,8 +253,8 @@ describe('createStore', () => {
252253

253254
it('only removes listener once when unsubscribe is called', () => {
254255
const store = createStore(reducers.todos)
255-
const listenerA = jest.fn()
256-
const listenerB = jest.fn()
256+
const listenerA = vi.fn()
257+
const listenerB = vi.fn()
257258

258259
const unsubscribeA = store.subscribe(listenerA)
259260
store.subscribe(listenerB)
@@ -268,7 +269,7 @@ describe('createStore', () => {
268269

269270
it('only removes relevant listener when unsubscribe is called', () => {
270271
const store = createStore(reducers.todos)
271-
const listener = jest.fn()
272+
const listener = vi.fn()
272273

273274
store.subscribe(listener)
274275
const unsubscribeSecond = store.subscribe(listener)
@@ -282,9 +283,9 @@ describe('createStore', () => {
282283

283284
it('supports removing a subscription within a subscription', () => {
284285
const store = createStore(reducers.todos)
285-
const listenerA = jest.fn()
286-
const listenerB = jest.fn()
287-
const listenerC = jest.fn()
286+
const listenerA = vi.fn()
287+
const listenerB = vi.fn()
288+
const listenerC = vi.fn()
288289

289290
store.subscribe(listenerA)
290291
const unSubB = store.subscribe(() => {
@@ -308,9 +309,9 @@ describe('createStore', () => {
308309
const doUnsubscribeAll = () =>
309310
unsubscribeHandles.forEach(unsubscribe => unsubscribe())
310311

311-
const listener1 = jest.fn()
312-
const listener2 = jest.fn()
313-
const listener3 = jest.fn()
312+
const listener1 = vi.fn()
313+
const listener2 = vi.fn()
314+
const listener3 = vi.fn()
314315

315316
unsubscribeHandles.push(store.subscribe(() => listener1()))
316317
unsubscribeHandles.push(
@@ -335,9 +336,9 @@ describe('createStore', () => {
335336
it('notifies only subscribers active at the moment of current dispatch', () => {
336337
const store = createStore(reducers.todos)
337338

338-
const listener1 = jest.fn()
339-
const listener2 = jest.fn()
340-
const listener3 = jest.fn()
339+
const listener1 = vi.fn()
340+
const listener2 = vi.fn()
341+
const listener3 = vi.fn()
341342

342343
let listener3Added = false
343344
const maybeAddThirdListener = () => {
@@ -367,10 +368,10 @@ describe('createStore', () => {
367368
it('uses the last snapshot of subscribers during nested dispatch', () => {
368369
const store = createStore(reducers.todos)
369370

370-
const listener1 = jest.fn()
371-
const listener2 = jest.fn()
372-
const listener3 = jest.fn()
373-
const listener4 = jest.fn()
371+
const listener1 = vi.fn()
372+
const listener2 = vi.fn()
373+
const listener3 = vi.fn()
374+
const listener4 = vi.fn()
374375

375376
let unsubscribe4: any
376377
const unsubscribe1 = store.subscribe(() => {
@@ -406,27 +407,41 @@ describe('createStore', () => {
406407
expect(listener4.mock.calls.length).toBe(1)
407408
})
408409

409-
it('provides an up-to-date state when a subscriber is notified', done => {
410+
it('provides an up-to-date state when a subscriber is notified', async () => {
410411
const store = createStore(reducers.todos)
412+
413+
let resolve: (value: unknown) => void = () => {}
414+
let promise = new Promise(_resolve => {
415+
resolve = _resolve
416+
})
411417
store.subscribe(() => {
412-
expect(store.getState()).toEqual([
413-
{
414-
id: 1,
415-
text: 'Hello'
416-
}
417-
])
418-
done()
418+
resolve(store.getState())
419419
})
420420
store.dispatch(addTodo('Hello'))
421+
const state = await promise
422+
423+
expect(state).toEqual([
424+
{
425+
id: 1,
426+
text: 'Hello'
427+
}
428+
])
421429
})
422430

423-
it('does not leak private listeners array', done => {
431+
it('does not leak private listeners array', async () => {
424432
const store = createStore(reducers.todos)
433+
let resolve: (value: unknown) => void = () => {}
434+
let promise = new Promise(_resolve => {
435+
resolve = _resolve
436+
})
437+
425438
store.subscribe(function (this: any) {
426-
expect(this).toBe(undefined)
427-
done()
439+
resolve(this)
428440
})
429441
store.dispatch(addTodo('Hello'))
442+
const result = await promise
443+
444+
expect(result).toBe(undefined)
430445
})
431446

432447
it('only accepts plain object actions', () => {
@@ -575,7 +590,7 @@ describe('createStore', () => {
575590
const vanillaStore = vanillaCreateStore(...args)
576591
return {
577592
...vanillaStore,
578-
dispatch: jest.fn(vanillaStore.dispatch)
593+
dispatch: vi.fn(vanillaStore.dispatch)
579594
}
580595
}
581596

@@ -601,7 +616,7 @@ describe('createStore', () => {
601616
const vanillaStore = vanillaCreateStore(...args)
602617
return {
603618
...vanillaStore,
604-
dispatch: jest.fn(vanillaStore.dispatch)
619+
dispatch: vi.fn(vanillaStore.dispatch)
605620
}
606621
}
607622

@@ -827,7 +842,7 @@ describe('createStore', () => {
827842

828843
it('does not log an error if parts of the current state will be ignored by a nextReducer using combineReducers', () => {
829844
const originalConsoleError = console.error
830-
console.error = jest.fn()
845+
console.error = vi.fn()
831846

832847
const store = createStore(
833848
combineReducers<{ x?: number; y: { z: number; w?: number } }>({

test/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"skipLibCheck": true,
1515
"noImplicitReturns": false,
1616
"noUnusedLocals": false,
17+
"types": ["vitest/globals"],
1718
"paths": {
1819
"redux": ["../src/index.ts"], // @remap-prod-remove-line
1920
"@internal/*": ["../src/*"]

test/typescript/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"noImplicitReturns": false,
1616
"noUnusedLocals": false,
1717
"noUnusedParameters": false,
18+
"types": ["vitest/globals"],
1819
"paths": {
1920
"redux": ["../../src/index.ts"], // @remap-prod-remove-line
2021
"@internal/*": ["../../src/*"]

test/utils/isPlainObject.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { expect } from 'expect'
21
import isPlainObject from '@internal/utils/isPlainObject'
32
import vm from 'vm'
43

test/utils/warning.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/* eslint-disable no-console */
2+
import { vi } from 'vitest'
23
import warning from '@internal/utils/warning'
34

45
describe('Utils', () => {
56
describe('warning', () => {
67
it('calls console.error when available', () => {
78
const preSpy = console.error
8-
const spy = jest.fn()
9+
const spy = vi.fn()
910
console.error = spy
1011
try {
1112
warning('Test')

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"noUnusedLocals": true,
1616
"noUnusedParameters": true,
1717
"baseUrl": "./",
18+
"types": ["vitest/globals"],
1819
"paths": {
1920
"redux": ["src/index.ts"], // @remap-prod-remove-line
2021
"@internal/*": ["src/*"]

0 commit comments

Comments
 (0)