Skip to content

Commit

Permalink
Switch tests to Vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Feb 13, 2023
1 parent a459be6 commit 4a35430
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 87 deletions.
13 changes: 4 additions & 9 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
name: Check for changes
runs-on: ubuntu-latest
outputs:
toolkit: ${{ steps.filter.outputs.toolkit }}
src: ${{ steps.filter.outputs.src }}
steps:
- uses: actions/checkout@v2
- uses: dorny/paths-filter@v2
Expand All @@ -15,10 +15,11 @@ jobs:
filters: |
src:
- 'src/**'
- 'test/**'
build:
needs: changes
#if: ${{ needs.changes.outputs.src == 'true' }}
if: ${{ needs.changes.outputs.src == 'true' }}

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

Expand Down Expand Up @@ -82,13 +83,7 @@ jobs:
- name: Install build artifact
run: yarn add ./package.tgz

- run: sed -i -e /@remap-prod-remove-line/d ./tsconfig.json ./jest.config.cjs ./test/tsconfig.json ./test/typescript/tsconfig.json

- run: cat ./tsconfig.json
- run: cat ./test/tsconfig.json
- run: cat ./test/typescript/tsconfig.json

- run: ls -lah ./node_modules/redux
- run: sed -i -e /@remap-prod-remove-line/d ./tsconfig.json ./vitest.config.ts ./test/tsconfig.json ./test/typescript/tsconfig.json

- name: Run tests, against dist
run: yarn test
Expand Down
19 changes: 0 additions & 19 deletions jest.config.cjs

This file was deleted.

41 changes: 20 additions & 21 deletions test/applyMiddleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Store,
Dispatch
} from 'redux'
import { vi } from 'vitest'
import * as reducers from './helpers/reducers'
import { addTodo, addTodoAsync, addTodoIfEmpty } from './helpers/actionCreators'
import { thunk } from './helpers/middleware'
Expand All @@ -34,7 +35,7 @@ describe('applyMiddleware', () => {
}
}

const spy = jest.fn()
const spy = vi.fn()
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos)

store.dispatch(addTodo('Use Redux'))
Expand All @@ -59,7 +60,7 @@ describe('applyMiddleware', () => {
}
}

const spy = jest.fn()
const spy = vi.fn()
const store = applyMiddleware(test(spy), thunk)(createStore)(reducers.todos)

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

it('works with thunk middleware', done => {
it('works with thunk middleware', async () => {
const store = applyMiddleware(thunk)(createStore)(reducers.todos)

store.dispatch(addTodoIfEmpty('Hello') as any)
Expand Down Expand Up @@ -106,27 +107,25 @@ describe('applyMiddleware', () => {
const dispatchedValue = store.dispatch(
addTodoAsync('Maybe') as any
) as unknown as Promise<void>
dispatchedValue.then(() => {
expect(store.getState()).toEqual([
{
id: 1,
text: 'Hello'
},
{
id: 2,
text: 'World'
},
{
id: 3,
text: 'Maybe'
}
])
done()
})
await dispatchedValue
expect(store.getState()).toEqual([
{
id: 1,
text: 'Hello'
},
{
id: 2,
text: 'World'
},
{
id: 3,
text: 'Maybe'
}
])
})

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

interface MultiDispatch<A extends Action = AnyAction> {
Expand Down
9 changes: 5 additions & 4 deletions test/combineReducers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AnyAction,
__DO_NOT_USE__ActionTypes as ActionTypes
} from 'redux'
import { vi } from 'vitest'

describe('Utils', () => {
describe('combineReducers', () => {
Expand Down Expand Up @@ -39,7 +40,7 @@ describe('Utils', () => {

it('warns if a reducer prop is undefined', () => {
const preSpy = console.error
const spy = jest.fn()
const spy = vi.fn()
console.error = spy

let isNotDefined: any
Expand Down Expand Up @@ -199,7 +200,7 @@ describe('Utils', () => {

it('warns if no reducers are passed to combineReducers', () => {
const preSpy = console.error
const spy = jest.fn()
const spy = vi.fn()
console.error = spy

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

it('warns if input state does not match reducer shape', () => {
const preSpy = console.error
const spy = jest.fn()
const spy = vi.fn()
const nullAction = undefined as unknown as AnyAction
console.error = spy

Expand Down Expand Up @@ -287,7 +288,7 @@ describe('Utils', () => {

it('only warns for unexpected keys once', () => {
const preSpy = console.error
const spy = jest.fn()
const spy = vi.fn()
console.error = spy
const nullAction = { type: '' }

Expand Down
79 changes: 47 additions & 32 deletions test/createStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Action,
Store
} from 'redux'
import { vi } from 'vitest'
import {
addTodo,
dispatchInMiddle,
Expand Down Expand Up @@ -205,8 +206,8 @@ describe('createStore', () => {

it('supports multiple subscriptions', () => {
const store = createStore(reducers.todos)
const listenerA = jest.fn()
const listenerB = jest.fn()
const listenerA = vi.fn()
const listenerB = vi.fn()

let unsubscribeA = store.subscribe(listenerA)
store.dispatch(unknownAction())
Expand Down Expand Up @@ -252,8 +253,8 @@ describe('createStore', () => {

it('only removes listener once when unsubscribe is called', () => {
const store = createStore(reducers.todos)
const listenerA = jest.fn()
const listenerB = jest.fn()
const listenerA = vi.fn()
const listenerB = vi.fn()

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

it('only removes relevant listener when unsubscribe is called', () => {
const store = createStore(reducers.todos)
const listener = jest.fn()
const listener = vi.fn()

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

it('supports removing a subscription within a subscription', () => {
const store = createStore(reducers.todos)
const listenerA = jest.fn()
const listenerB = jest.fn()
const listenerC = jest.fn()
const listenerA = vi.fn()
const listenerB = vi.fn()
const listenerC = vi.fn()

store.subscribe(listenerA)
const unSubB = store.subscribe(() => {
Expand All @@ -308,9 +309,9 @@ describe('createStore', () => {
const doUnsubscribeAll = () =>
unsubscribeHandles.forEach(unsubscribe => unsubscribe())

const listener1 = jest.fn()
const listener2 = jest.fn()
const listener3 = jest.fn()
const listener1 = vi.fn()
const listener2 = vi.fn()
const listener3 = vi.fn()

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

const listener1 = jest.fn()
const listener2 = jest.fn()
const listener3 = jest.fn()
const listener1 = vi.fn()
const listener2 = vi.fn()
const listener3 = vi.fn()

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

const listener1 = jest.fn()
const listener2 = jest.fn()
const listener3 = jest.fn()
const listener4 = jest.fn()
const listener1 = vi.fn()
const listener2 = vi.fn()
const listener3 = vi.fn()
const listener4 = vi.fn()

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

it('provides an up-to-date state when a subscriber is notified', done => {
it('provides an up-to-date state when a subscriber is notified', async () => {
const store = createStore(reducers.todos)

let resolve: (value: unknown) => void = () => {}
let promise = new Promise(_resolve => {
resolve = _resolve
})
store.subscribe(() => {
expect(store.getState()).toEqual([
{
id: 1,
text: 'Hello'
}
])
done()
resolve(store.getState())
})
store.dispatch(addTodo('Hello'))
const state = await promise

expect(state).toEqual([
{
id: 1,
text: 'Hello'
}
])
})

it('does not leak private listeners array', done => {
it('does not leak private listeners array', async () => {
const store = createStore(reducers.todos)
let resolve: (value: unknown) => void = () => {}
let promise = new Promise(_resolve => {
resolve = _resolve
})

store.subscribe(function (this: any) {
expect(this).toBe(undefined)
done()
resolve(this)
})
store.dispatch(addTodo('Hello'))
const result = await promise

expect(result).toBe(undefined)
})

it('only accepts plain object actions', () => {
Expand Down Expand Up @@ -575,7 +590,7 @@ describe('createStore', () => {
const vanillaStore = vanillaCreateStore(...args)
return {
...vanillaStore,
dispatch: jest.fn(vanillaStore.dispatch)
dispatch: vi.fn(vanillaStore.dispatch)
}
}

Expand All @@ -601,7 +616,7 @@ describe('createStore', () => {
const vanillaStore = vanillaCreateStore(...args)
return {
...vanillaStore,
dispatch: jest.fn(vanillaStore.dispatch)
dispatch: vi.fn(vanillaStore.dispatch)
}
}

Expand Down Expand Up @@ -827,7 +842,7 @@ describe('createStore', () => {

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

const store = createStore(
combineReducers<{ x?: number; y: { z: number; w?: number } }>({
Expand Down
1 change: 1 addition & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"skipLibCheck": true,
"noImplicitReturns": false,
"noUnusedLocals": false,
"types": ["vitest/globals"],
"paths": {
"redux": ["../src/index.ts"], // @remap-prod-remove-line
"@internal/*": ["../src/*"]
Expand Down
1 change: 1 addition & 0 deletions test/typescript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"noImplicitReturns": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"types": ["vitest/globals"],
"paths": {
"redux": ["../../src/index.ts"], // @remap-prod-remove-line
"@internal/*": ["../../src/*"]
Expand Down
1 change: 0 additions & 1 deletion test/utils/isPlainObject.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { expect } from 'expect'
import isPlainObject from '@internal/utils/isPlainObject'
import vm from 'vm'

Expand Down
3 changes: 2 additions & 1 deletion test/utils/warning.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable no-console */
import { vi } from 'vitest'
import warning from '@internal/utils/warning'

describe('Utils', () => {
describe('warning', () => {
it('calls console.error when available', () => {
const preSpy = console.error
const spy = jest.fn()
const spy = vi.fn()
console.error = spy
try {
warning('Test')
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"baseUrl": "./",
"types": ["vitest/globals"],
"paths": {
"redux": ["src/index.ts"], // @remap-prod-remove-line
"@internal/*": ["src/*"]
Expand Down
Loading

0 comments on commit 4a35430

Please sign in to comment.