Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate all test cases to jest #1355

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
module.exports = {
presets: [
[
"@babel/preset-env"
"@babel/preset-env",
{
targets: {
esmodules: true,
},
},
],
'@babel/preset-typescript'
],
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ I ([@ckalika](https://github.com/ckalika)) spoke with [@rt2zz](https://github.co

5. Improve testing and automation
- [x] Move to GitHub Actions
- [ ] Move from Ava to Jest
- [x] Move from Ava to Jest

There's a lot to do here, so I'll ask your patience and understanding as I work through it. If you have ideas for how to improve the library, the documentation, or the community, I'd love to hear them, and if you're submitting pull requests (or have submitted some previously), please reach out and help me understand what you're aiming to do with it.

Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testTimeout: 10000,
}
19 changes: 4 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"README.md"
],
"scripts": {
"ava": "ava",
"build": "npm run build:commonjs && npm run build:es && npm run build:umd",
"build:commonjs": "tsc --module commonjs --outDir lib",
"build:es": "tsc --module es2015 --outDir es",
Expand All @@ -24,7 +23,7 @@
"prepare": "npm run build",
"precommit": "lint-staged",
"stats:size": "node ./scripts/size-estimator.js",
"test": "ava",
"test": "jest",
"version": "npm run clean && npm run build && npm run stats:size | tail -1 >> LIBSIZE.md && git add LIBSIZE.md"
},
"lint-staged": {
Expand All @@ -36,33 +35,23 @@
"author": "",
"license": "MIT",
"homepage": "https://github.com/rt2zz/redux-persist#readme",
"ava": {
"files": [
"tests/**/*.spec.ts"
],
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
},
"devDependencies": {
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@babel/preset-typescript": "^7.16.7",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^20.0.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@rollup/plugin-typescript": "^8.2.5",
"@types/jest": "^27.4.0",
"@types/react": "^17.0.16",
"@types/redux-mock-store": "^1.0.3",
"@types/sinon": "^10.0.2",
"@typescript-eslint/eslint-plugin": "^4.29.0",
"@typescript-eslint/parser": "^4.29.0",
"ava": "^3.15.0",
"eslint": "^7.32.0",
"eslint-plugin-import": "^2.23.4",
"husky": "^7.0.1",
"jest": "^27.4.7",
"lint-staged": "^11.1.2",
"prettier": "^2.3.2",
"redux": "^4.1.1",
Expand Down
23 changes: 11 additions & 12 deletions tests/complete.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import test from 'ava'
import { combineReducers, createStore } from 'redux'

import persistReducer from '../src/persistReducer'
Expand All @@ -15,43 +14,43 @@ const config = {
timeout: 5,
}

test('multiple persistReducers work together', t => {
return new Promise((resolve) => {
test('multiple persistReducers work together', async () => {
return new Promise<void>((resolve) => {
const r1 = persistReducer(config, reducer)
const r2 = persistReducer(config, reducer)
const rootReducer = combineReducers({ r1, r2 })
const store = createStore(rootReducer)
const persistor = persistStore(store, {}, () => {
t.is(persistor.getState().bootstrapped, true)
resolve()
expect(persistor.getState().bootstrapped).toBe(true)
resolve()
})
})
})

test('persistStore timeout 0 never bootstraps', t => {
return new Promise((resolve, reject) => {
test('persistStore timeout 0 never bootstraps', async () => {
return new Promise<void>((resolve, reject) => {
const r1 = persistReducer({...config, storage: brokenStorage, timeout: 0}, reducer)
const rootReducer = combineReducers({ r1 })
const store = createStore(rootReducer)
const persistor = persistStore(store, undefined, () => {
console.log('resolve')
reject()
reject()
})
setTimeout(() => {
t.is(persistor.getState().bootstrapped, false)
expect(persistor.getState().bootstrapped).toBe(false)
resolve()
}, 10)
})
})


test('persistStore timeout forces bootstrap', t => {
return new Promise((resolve, reject) => {
test('persistStore timeout forces bootstrap', async () => {
return new Promise<void>((resolve, reject) => {
const r1 = persistReducer({...config, storage: brokenStorage}, reducer)
const rootReducer = combineReducers({ r1 })
const store = createStore(rootReducer)
const persistor = persistStore(store, undefined, () => {
t.is(persistor.getState().bootstrapped, true)
expect(persistor.getState().bootstrapped).toBe(true)
resolve()
})
setTimeout(() => {
Expand Down
53 changes: 25 additions & 28 deletions tests/createPersistor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import test from 'ava'
import sinon from 'sinon'
import createMemoryStorage from './utils/createMemoryStorage'
import createPersistoid from '../src/createPersistoid'
const memoryStorage = createMemoryStorage()
Expand All @@ -11,49 +9,48 @@ const config = {
debug: true
}

let spy: sinon.SinonSpy;
let clock: sinon.SinonFakeTimers;
let spy: jest.SpyInstance;
let clock: typeof jest;

test.beforeEach(() => {
spy = sinon.spy(memoryStorage, 'setItem')
clock = sinon.useFakeTimers()
beforeEach(() => {
spy = jest.spyOn(memoryStorage, 'setItem')
clock = jest.useFakeTimers()
});

test.afterEach(() => {
spy.restore()
clock.restore()
afterEach(() => {
spy.mockRestore()
clock.useRealTimers()
});

// @NOTE these tests broke when updating sinon
test.skip('it updates changed state', t => {
test('it updates changed state', () => {
const { update } = createPersistoid(config)
update({ a: 1 })
clock.tick(1);
clock.runAllTimers()
update({ a: 2 })
clock.tick(1);
t.true(spy.calledTwice);
t.true(spy.withArgs('persist:persist-reducer-test', '{"a":"1"}').calledOnce);
t.true(spy.withArgs('persist:persist-reducer-test', '{"a":"2"}').calledOnce);
clock.runAllTimers()
expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenNthCalledWith(1, 'persist:persist-reducer-test', '{"a":"1"}');
expect(spy).toHaveBeenNthCalledWith(2, 'persist:persist-reducer-test', '{"a":"2"}');
})

test.skip('it does not update unchanged state', t => {
test('it does not update unchanged state', () => {
const { update } = createPersistoid(config)
update({ a: undefined, b: 1 })
clock.tick(1);
clock.runAllTimers()
// This update should not cause a write.
update({ a: undefined, b: 1 })
clock.tick(1);
t.true(spy.calledOnce);
t.true(spy.withArgs('persist:persist-reducer-test', '{"b":"1"}').calledOnce);
clock.runAllTimers()
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('persist:persist-reducer-test', '{"b":"1"}');
})

test.skip('it updates removed keys', t => {
test('it updates removed keys', () => {
const { update } = createPersistoid(config)
update({ a: undefined, b: 1 })
clock.tick(1);
clock.runAllTimers();
update({ a: undefined, b: undefined })
clock.tick(1);
t.true(spy.calledTwice);
t.true(spy.withArgs('persist:persist-reducer-test', '{"b":"1"}').calledOnce);
t.true(spy.withArgs('persist:persist-reducer-test', '{}').calledOnce);
clock.runAllTimers();
expect(spy).toHaveBeenCalledTimes(2)
expect(spy).toHaveBeenNthCalledWith(1,'persist:persist-reducer-test', '{"b":"1"}')
expect(spy).toHaveBeenNthCalledWith(2,'persist:persist-reducer-test', '{}')
})
8 changes: 3 additions & 5 deletions tests/flush.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import test from 'ava'
import { createStore } from 'redux'

import getStoredState from '../src/getStoredState'
Expand Down Expand Up @@ -35,18 +33,18 @@ const config = {
throttle: 1000,
}

test('state before flush is not updated, after flush is', t => {
test('state before flush is not updated, after flush is', () => {
return new Promise((resolve) => {
const rootReducer = persistReducer(config, reducer)
const store = createStore(rootReducer)
const persistor = persistStore(store, {}, async () => {
store.dispatch({ type: INCREMENT })
const state = store.getState()
const storedPreFlush = await getStoredState(config)
t.not(storedPreFlush && storedPreFlush.c, state.c)
expect(storedPreFlush && storedPreFlush.c).not.toBe(state.c)
await persistor.flush()
const storedPostFlush = await getStoredState(config)
resolve(t.is(storedPostFlush && storedPostFlush.c, state.c))
resolve(expect(storedPostFlush && storedPostFlush.c).toBe(state.c))
})
})
})
8 changes: 3 additions & 5 deletions tests/persistCombineReducers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import persistCombineReducers from '../src/persistCombineReducers'
import createMemoryStorage from './utils/createMemoryStorage'

import test from 'ava'

const config = {
key: 'TestConfig',
storage: createMemoryStorage()
}

test('persistCombineReducers returns a function', t => {
test('persistCombineReducers returns a function', () => {
const reducer = persistCombineReducers(config, {
foo: () => ({})
})

t.is(typeof reducer, 'function')
expect(typeof reducer).toBe('function');
})

/*
test.skip('persistCombineReducers merges two levels deep of state', t => {

})
*/
25 changes: 11 additions & 14 deletions tests/persistReducer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import test from 'ava'
import sinon from 'sinon'

import persistReducer from '../src/persistReducer'
import createMemoryStorage from './utils/createMemoryStorage'
import { PERSIST } from '../src/constants'
Expand All @@ -13,27 +10,27 @@ const config = {
storage: createMemoryStorage()
}

test('persistedReducer does not automatically set _persist state', t => {
test('persistedReducer does not automatically set _persist state', () => {
const persistedReducer = persistReducer(config, reducer)
const state = persistedReducer({}, {type: "UNDEFINED"})
console.log('state', state)
t.is(undefined, state._persist)
expect(state._persist).toBeUndefined()
})

test('persistedReducer does returns versioned, rehydrate tracked _persist state upon PERSIST', t => {
test('persistedReducer does returns versioned, rehydrate tracked _persist state upon PERSIST', () => {
const persistedReducer = persistReducer(config, reducer)
const register = sinon.spy()
const rehydrate = sinon.spy()
const register = jest.fn()
const rehydrate = jest.fn()
const state = persistedReducer({}, { type: PERSIST, register, rehydrate })
t.deepEqual({ version: 1, rehydrated: false}, state._persist)
expect(state._persist).toEqual({ version: 1, rehydrated: false})
})

test('persistedReducer calls register and rehydrate after PERSIST', async (t) => {
test('persistedReducer calls register and rehydrate after PERSIST', async () => {
const persistedReducer = persistReducer(config, reducer)
const register = sinon.spy()
const rehydrate = sinon.spy()
const register = jest.fn()
const rehydrate = jest.fn()
persistedReducer({}, { type: PERSIST, register, rehydrate })
await sleep(5000)
t.is(register.callCount, 1)
t.is(rehydrate.callCount, 1)
expect(register).toHaveBeenCalledTimes(1)
expect(rehydrate).toHaveBeenCalledTimes(1)
})
Loading