Skip to content

Commit 6c75273

Browse files
committed
Move custom matchers into vitest.setup.ts
- This was done so that we won't have to import `helpers.tsx` anywhere we use `toHaveConsoleOutput`
1 parent 75a3786 commit 6c75273

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,32 +92,6 @@ export const useRenderCounter = () => {
9292
return useCallback(() => countRef.current, [])
9393
}
9494

95-
expect.extend({
96-
toMatchSequence(
97-
_actions: UnknownAction[],
98-
...matchers: Array<(arg: any) => boolean>
99-
) {
100-
const actions = _actions.concat()
101-
actions.shift() // remove INIT
102-
103-
for (let i = 0; i < matchers.length; i++) {
104-
if (!matchers[i](actions[i])) {
105-
return {
106-
message: () =>
107-
`Action ${actions[i].type} does not match sequence at position ${i}.
108-
All actions:
109-
${actions.map((a) => a.type).join('\n')}`,
110-
pass: false,
111-
}
112-
}
113-
}
114-
return {
115-
message: () => `All actions match the sequence.`,
116-
pass: true,
117-
}
118-
},
119-
})
120-
12195
export const actionsReducer = {
12296
actions: (state: UnknownAction[] = [], action: UnknownAction) => {
12397
// As of 2.0-beta.4, we are going to ignore all `subscriptionsUpdated` actions in tests

packages/toolkit/vitest.setup.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { UnknownAction } from '@reduxjs/toolkit'
12
import nodeFetch, { Headers, Request } from 'node-fetch'
23
import { server } from './src/query/tests/mocks/server'
34

@@ -58,3 +59,37 @@ afterEach(() => {
5859
afterAll(() => {
5960
server.close()
6061
})
62+
63+
declare global {
64+
namespace jest {
65+
interface Matchers<R> {
66+
toMatchSequence(...matchers: Array<(arg: any) => boolean>): R
67+
}
68+
}
69+
}
70+
71+
expect.extend({
72+
toMatchSequence(
73+
_actions: UnknownAction[],
74+
...matchers: Array<(arg: any) => boolean>
75+
) {
76+
const actions = _actions.concat()
77+
actions.shift() // remove INIT
78+
79+
for (let i = 0; i < matchers.length; i++) {
80+
if (!matchers[i](actions[i])) {
81+
return {
82+
message: () =>
83+
`Action ${actions[i].type} does not match sequence at position ${i}.
84+
All actions:
85+
${actions.map((a) => a.type).join('\n')}`,
86+
pass: false,
87+
}
88+
}
89+
}
90+
return {
91+
message: () => `All actions match the sequence.`,
92+
pass: true,
93+
}
94+
},
95+
})

0 commit comments

Comments
 (0)