Skip to content

Commit d00e08f

Browse files
authored
Merge pull request #2460 from reduxjs/bugfix/broken-tests
2 parents 5406333 + 5b145b2 commit d00e08f

17 files changed

+222
-747
lines changed

.github/workflows/test-codegen.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131

3232
strategy:
3333
matrix:
34-
node-version: [12.x]
34+
node-version: ['16.x']
3535

3636
steps:
3737
- uses: actions/checkout@v2

.github/workflows/tests.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
runs-on: ubuntu-latest
2929
strategy:
3030
matrix:
31-
node: ['14.x']
31+
node: ['16.x']
3232

3333
steps:
3434
- name: Checkout repo
@@ -58,7 +58,7 @@ jobs:
5858
strategy:
5959
fail-fast: false
6060
matrix:
61-
node: ['14.x']
61+
node: ['16.x']
6262
steps:
6363
- name: Checkout repo
6464
uses: actions/checkout@v2
@@ -95,7 +95,7 @@ jobs:
9595
strategy:
9696
fail-fast: false
9797
matrix:
98-
node: ['14.x']
98+
node: ['16.x']
9999
ts: ['4.1', '4.2', '4.3', '4.4', '4.5', '4.6', '4.7']
100100
steps:
101101
- name: Checkout repo

packages/rtk-query-codegen-openapi/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@reduxjs/toolkit": "^1.6.0",
3636
"@types/commander": "^2.12.2",
3737
"@types/glob-to-regexp": "^0.4.0",
38-
"@types/jest": "^26.0.20",
38+
"@types/jest": "^27",
3939
"@types/lodash": "^4.14.165",
4040
"@types/node": "^14.14.12",
4141
"@types/prettier": "^2.1.6",
@@ -46,11 +46,11 @@
4646
"esbuild": "^0.13.10",
4747
"esbuild-runner": "^2.2.1",
4848
"husky": "^4.3.6",
49-
"jest": "^26.6.3",
49+
"jest": "^27",
5050
"msw": "^0.41.1",
5151
"openapi-types": "^9.1.0",
5252
"pretty-quick": "^3.1.0",
53-
"ts-jest": "^26.4.4",
53+
"ts-jest": "^27",
5454
"ts-node": "^10.4.0",
5555
"yalc": "^1.0.0-pre.47"
5656
},

packages/rtk-query-codegen-openapi/test/cli.test.ts

+5-13
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ afterEach(() => {
3333

3434
describe('CLI options testing', () => {
3535
test('generation with `config.example.js`', async () => {
36-
jest.setTimeout(10000);
37-
3836
const out = await cli([`./config.example.js`], __dirname);
3937

4038
expect(out).toEqual({
@@ -46,11 +44,9 @@ Done
4644
});
4745

4846
expect(fs.readFileSync(path.resolve(tmpDir, 'example.ts'), 'utf-8')).toMatchSnapshot();
49-
});
47+
}, 25000);
5048

5149
test('paths are relative to configfile, not to cwd', async () => {
52-
jest.setTimeout(10000);
53-
5450
const out = await cli([`../test/config.example.js`], path.resolve(__dirname, '../src'));
5551

5652
expect(out).toEqual({
@@ -62,11 +58,9 @@ Done
6258
});
6359

6460
expect(fs.readFileSync(path.resolve(tmpDir, 'example.ts'), 'utf-8')).toMatchSnapshot();
65-
});
61+
}, 25000);
6662

6763
test('ts, js and json all work the same', async () => {
68-
jest.setTimeout(25000);
69-
7064
await cli([`./config.example.js`], __dirname);
7165
const fromJs = fs.readFileSync(path.resolve(tmpDir, 'example.ts'), 'utf-8');
7266
await cli([`./config.example.ts`], __dirname);
@@ -76,12 +70,10 @@ Done
7670

7771
expect(fromTs).toEqual(fromJs);
7872
expect(fromJson).toEqual(fromJs);
79-
});
73+
}, 25000);
8074

8175
test('missing parameters doesnt fail', async () => {
82-
jest.setTimeout(25000);
83-
8476
const out = await cli([`./config.invalid-example.json`], __dirname);
85-
expect(out.stderr).toContain("Error: path parameter petId does not seem to be defined in '/pet/{petId}'!")
86-
});
77+
expect(out.stderr).toContain("Error: path parameter petId does not seem to be defined in '/pet/{petId}'!");
78+
}, 25000);
8779
});

packages/toolkit/jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
testEnvironment: 'jest-environment-jsdom',
23
setupFilesAfterEnv: ['./jest.setup.js'],
34
testMatch: ['<rootDir>/src/**/*.(spec|test).[jt]s?(x)'],
45
moduleNameMapper: {

packages/toolkit/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"@testing-library/react": "^13.3.0",
3333
"@testing-library/user-event": "^14.2.0",
3434
"@types/convert-source-map": "^1.5.1",
35-
"@types/jest": "^24.0.11",
35+
"@types/jest": "^27",
3636
"@types/json-stringify-safe": "^5.0.0",
3737
"@types/nanoid": "^2.1.0",
3838
"@types/node": "^10.14.4",
@@ -57,7 +57,7 @@
5757
"eslint-plugin-react-hooks": "^4.2.0",
5858
"fs-extra": "^9.1.0",
5959
"invariant": "^2.2.4",
60-
"jest": "^26.6.3",
60+
"jest": "^27",
6161
"json-stringify-safe": "^5.0.1",
6262
"magic-string": "^0.25.7",
6363
"merge-source-map": "^1.1.0",
@@ -71,7 +71,7 @@
7171
"size-limit": "^4.11.0",
7272
"source-map": "^0.7.3",
7373
"terser": "^5.6.1",
74-
"ts-jest": "^26.5.5",
74+
"ts-jest": "^27",
7575
"tslib": "^1.10.0",
7676
"typescript": "~4.2.4",
7777
"yargs": "^15.3.1"

packages/toolkit/src/entities/tests/utils.spec.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { selectIdValue } from '../utils'
21
import { AClockworkOrange } from './fixtures/book'
32

43
describe('Entity utils', () => {
@@ -12,26 +11,29 @@ describe('Entity utils', () => {
1211

1312
afterEach(() => {
1413
process.env = OLD_ENV
14+
jest.resetAllMocks()
1515
})
1616

1717
it('should not warn when key does exist', () => {
18-
const spy = spyOn(console, 'warn')
19-
20-
selectIdValue(AClockworkOrange, (book) => book.id)
18+
const { selectIdValue } = require('../utils')
19+
const spy = jest.spyOn(console, 'warn')
2120

21+
selectIdValue(AClockworkOrange, (book: any) => book.id)
2222
expect(spy).not.toHaveBeenCalled()
2323
})
2424

2525
it('should warn when key does not exist in dev mode', () => {
26-
const spy = spyOn(console, 'warn')
26+
const { selectIdValue } = require('../utils')
27+
const spy = jest.spyOn(console, 'warn')
2728

2829
selectIdValue(AClockworkOrange, (book: any) => book.foo)
2930

3031
expect(spy).toHaveBeenCalled()
3132
})
3233

3334
it('should warn when key is undefined in dev mode', () => {
34-
const spy = spyOn(console, 'warn')
35+
const { selectIdValue } = require('../utils')
36+
const spy = jest.spyOn(console, 'warn')
3537

3638
const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }
3739
selectIdValue(undefinedAClockworkOrange, (book: any) => book.id)
@@ -41,7 +43,8 @@ describe('Entity utils', () => {
4143

4244
it('should not warn when key does not exist in prod mode', () => {
4345
process.env.NODE_ENV = 'production'
44-
const spy = spyOn(console, 'warn')
46+
const { selectIdValue } = require('../utils')
47+
const spy = jest.spyOn(console, 'warn')
4548

4649
selectIdValue(AClockworkOrange, (book: any) => book.foo)
4750

@@ -50,7 +53,8 @@ describe('Entity utils', () => {
5053

5154
it('should not warn when key is undefined in prod mode', () => {
5255
process.env.NODE_ENV = 'production'
53-
const spy = spyOn(console, 'warn')
56+
const { selectIdValue } = require('../utils')
57+
const spy = jest.spyOn(console, 'warn')
5458

5559
const undefinedAClockworkOrange = { ...AClockworkOrange, id: undefined }
5660
selectIdValue(undefinedAClockworkOrange, (book: any) => book.id)

packages/toolkit/src/query/tests/cacheCollection.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { waitMs } from './helpers'
44
import type { Middleware, Reducer } from 'redux'
55

66
beforeAll(() => {
7-
jest.useFakeTimers()
7+
jest.useFakeTimers('legacy')
88
})
99

1010
const onCleanup = jest.fn()

packages/toolkit/src/query/tests/cacheLifecycle.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { fetchBaseQuery } from '@reduxjs/toolkit/query'
44
import { expectType, fakeTimerWaitFor, setupApiStore, waitMs } from './helpers'
55

66
beforeAll(() => {
7-
jest.useFakeTimers()
7+
jest.useFakeTimers('legacy')
88
})
99

1010
const api = createApi({

packages/toolkit/src/query/tests/cleanup.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function UsingAB() {
3535
}
3636

3737
beforeAll(() => {
38-
jest.useFakeTimers()
38+
jest.useFakeTimers('legacy')
3939
})
4040

4141
test('data stays in store when component stays rendered', async () => {

packages/toolkit/src/query/tests/createApi.test.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,14 @@ describe('endpoint definition typings', () => {
332332
})
333333
}
334334
let api = getNewApi()
335-
let storeRef = setupApiStore(api)
336335
beforeEach(() => {
337336
api = getNewApi()
338-
storeRef = setupApiStore(api)
339337
})
340338

341339
test('pre-modification behaviour', async () => {
340+
const storeRef = setupApiStore(api, undefined, {
341+
withoutTestLifecycles: true,
342+
})
342343
storeRef.store.dispatch(api.endpoints.query1.initiate('in1'))
343344
storeRef.store.dispatch(api.endpoints.query2.initiate('in2'))
344345
storeRef.store.dispatch(api.endpoints.mutation1.initiate('in1'))
@@ -397,6 +398,9 @@ describe('endpoint definition typings', () => {
397398
})
398399

399400
test('warn on wrong tagType', async () => {
401+
const storeRef = setupApiStore(api, undefined, {
402+
withoutTestLifecycles: true,
403+
})
400404
// only type-test this part
401405
if (2 > 1) {
402406
api.enhanceEndpoints({
@@ -455,6 +459,9 @@ describe('endpoint definition typings', () => {
455459
})
456460

457461
test('modify', () => {
462+
const storeRef = setupApiStore(api, undefined, {
463+
withoutTestLifecycles: true,
464+
})
458465
api.enhanceEndpoints({
459466
endpoints: {
460467
query1: {
@@ -751,7 +758,9 @@ test('providesTags and invalidatesTags can use baseQueryMeta', async () => {
751758
}),
752759
})
753760

754-
const storeRef = setupApiStore(api)
761+
const storeRef = setupApiStore(api, undefined, {
762+
withoutTestLifecycles: true,
763+
})
755764

756765
await storeRef.store.dispatch(api.endpoints.query.initiate())
757766
expect('request' in _meta! && 'response' in _meta!).toBe(true)

packages/toolkit/src/query/tests/helpers.tsx

+22-16
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ export function setupApiStore<
173173
util: { resetApiState(): any }
174174
},
175175
R extends Record<string, Reducer<any, any>> = Record<never, never>
176-
>(api: A, extraReducers?: R, withoutListeners?: boolean) {
176+
>(
177+
api: A,
178+
extraReducers?: R,
179+
options: { withoutListeners?: boolean; withoutTestLifecycles?: boolean } = {}
180+
) {
177181
const getStore = () =>
178182
configureStore({
179183
reducer: { api: api.reducer, ...extraReducers },
@@ -203,21 +207,23 @@ export function setupApiStore<
203207
}
204208
let cleanupListeners: () => void
205209

206-
beforeEach(() => {
207-
const store = getStore() as StoreType
208-
refObj.store = store
209-
refObj.wrapper = withProvider(store)
210-
if (!withoutListeners) {
211-
cleanupListeners = setupListeners(store.dispatch)
212-
}
213-
})
214-
afterEach(() => {
215-
cleanup()
216-
if (!withoutListeners) {
217-
cleanupListeners()
218-
}
219-
refObj.store.dispatch(api.util.resetApiState())
220-
})
210+
if (!options.withoutTestLifecycles) {
211+
beforeEach(() => {
212+
const store = getStore() as StoreType
213+
refObj.store = store
214+
refObj.wrapper = withProvider(store)
215+
if (!options.withoutListeners) {
216+
cleanupListeners = setupListeners(store.dispatch)
217+
}
218+
})
219+
afterEach(() => {
220+
cleanup()
221+
if (!options.withoutListeners) {
222+
cleanupListeners()
223+
}
224+
refObj.store.dispatch(api.util.resetApiState())
225+
})
226+
}
221227

222228
return refObj
223229
}

packages/toolkit/src/query/tests/invalidation.test.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ test.each(caseMatrix)(
100100
invalidatesTags,
101101
}),
102102
}),
103-
})
103+
}),
104+
undefined,
105+
{ withoutTestLifecycles: true }
104106
)
105107

106108
store.dispatch(providing.initiate())

packages/toolkit/src/query/tests/refetchingBehaviors.test.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('refetchOnFocus tests', () => {
7272
expect(screen.getByTestId('amount').textContent).toBe('1')
7373
)
7474

75-
act(() => {
75+
await act(async () => {
7676
fireEvent.focus(window)
7777
})
7878

@@ -366,7 +366,9 @@ describe('refetchOnReconnect tests', () => {
366366
})
367367

368368
describe('customListenersHandler', () => {
369-
const storeRef = setupApiStore(defaultApi, undefined, true)
369+
const storeRef = setupApiStore(defaultApi, undefined, {
370+
withoutListeners: true,
371+
})
370372

371373
test('setupListeners accepts a custom callback and executes it', async () => {
372374
const consoleSpy = jest.spyOn(console, 'log')

0 commit comments

Comments
 (0)