Skip to content

Commit 8a22b1d

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 965531a commit 8a22b1d

File tree

3 files changed

+74
-74
lines changed

3 files changed

+74
-74
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { configureStore } from '@reduxjs/toolkit'
22
import { createApi, fakeBaseQuery } from '@reduxjs/toolkit/query'
3-
import '../../tests/utils/helpers'
43

54
type CustomErrorType = { type: 'Custom' }
65

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

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ import type {
88
import { configureStore } from '@reduxjs/toolkit'
99
import { setupListeners } from '@reduxjs/toolkit/query'
1010
import { act, cleanup } from '@testing-library/react'
11-
import {
12-
createConsole,
13-
getLog,
14-
mockConsole,
15-
} from 'console-testing-library/pure'
1611
import { useCallback, useEffect, useRef } from 'react'
1712
import { Provider } from 'react-redux'
1813
import type { AnyObject } from '../../tsHelpers'
@@ -97,80 +92,13 @@ export const useRenderCounter = () => {
9792
return useCallback(() => countRef.current, [])
9893
}
9994

100-
expect.extend({
101-
toMatchSequence(
102-
_actions: UnknownAction[],
103-
...matchers: Array<(arg: any) => boolean>
104-
) {
105-
const actions = _actions.concat()
106-
actions.shift() // remove INIT
107-
108-
for (let i = 0; i < matchers.length; i++) {
109-
if (!matchers[i](actions[i])) {
110-
return {
111-
message: () =>
112-
`Action ${actions[i].type} does not match sequence at position ${i}.
113-
All actions:
114-
${actions.map((a) => a.type).join('\n')}`,
115-
pass: false,
116-
}
117-
}
118-
}
119-
return {
120-
message: () => `All actions match the sequence.`,
121-
pass: true,
122-
}
123-
},
124-
})
125-
126-
declare global {
127-
namespace jest {
128-
interface Matchers<R> {
129-
toHaveConsoleOutput(expectedOutput: string): Promise<R>
130-
}
131-
}
132-
}
133-
134-
function normalize(str: string) {
95+
export function normalize(str: string) {
13596
return str
13697
.normalize()
13798
.replace(/\s*\r?\n\r?\s*/g, '')
13899
.trim()
139100
}
140101

141-
expect.extend({
142-
async toHaveConsoleOutput(
143-
fn: () => void | Promise<void>,
144-
expectedOutput: string,
145-
) {
146-
const restore = mockConsole(createConsole())
147-
await fn()
148-
const { log } = getLog()
149-
restore()
150-
151-
if (normalize(log) === normalize(expectedOutput))
152-
return {
153-
message: () => `Console output matches
154-
===
155-
${expectedOutput}
156-
===`,
157-
pass: true,
158-
}
159-
else
160-
return {
161-
message: () => `Console output
162-
===
163-
${log}
164-
===
165-
does not match
166-
===
167-
${expectedOutput}
168-
===`,
169-
pass: false,
170-
}
171-
},
172-
})
173-
174102
export const actionsReducer = {
175103
actions: (state: UnknownAction[] = [], action: UnknownAction) => {
176104
// As of 2.0-beta.4, we are going to ignore all `subscriptionsUpdated` actions in tests

packages/toolkit/vitest.setup.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import type { UnknownAction } from '@reduxjs/toolkit'
2+
import {
3+
createConsole,
4+
getLog,
5+
mockConsole,
6+
} from 'console-testing-library/pure'
17
import nodeFetch, { Headers, Request } from 'node-fetch'
28
import { server } from './src/query/tests/mocks/server'
9+
import { normalize } from './src/tests/utils/helpers'
310

411
vi.stubGlobal('fetch', nodeFetch)
512
vi.stubGlobal('Request', Request)
@@ -16,3 +23,69 @@ afterEach(() => {
1623
afterAll(() => {
1724
server.close()
1825
})
26+
27+
declare global {
28+
namespace jest {
29+
interface Matchers<R> {
30+
toHaveConsoleOutput(expectedOutput: string): Promise<R>
31+
toMatchSequence(...matchers: Array<(arg: any) => boolean>): R
32+
}
33+
}
34+
}
35+
36+
expect.extend({
37+
async toHaveConsoleOutput(
38+
fn: () => void | Promise<void>,
39+
expectedOutput: string,
40+
) {
41+
const restore = mockConsole(createConsole())
42+
await fn()
43+
const { log } = getLog()
44+
restore()
45+
46+
if (normalize(log) === normalize(expectedOutput))
47+
return {
48+
message: () => `Console output matches
49+
===
50+
${expectedOutput}
51+
===`,
52+
pass: true,
53+
}
54+
else
55+
return {
56+
message: () => `Console output
57+
===
58+
${log}
59+
===
60+
does not match
61+
===
62+
${expectedOutput}
63+
===`,
64+
pass: false,
65+
}
66+
},
67+
68+
toMatchSequence(
69+
_actions: UnknownAction[],
70+
...matchers: Array<(arg: any) => boolean>
71+
) {
72+
const actions = _actions.concat()
73+
actions.shift() // remove INIT
74+
75+
for (let i = 0; i < matchers.length; i++) {
76+
if (!matchers[i](actions[i])) {
77+
return {
78+
message: () =>
79+
`Action ${actions[i].type} does not match sequence at position ${i}.
80+
All actions:
81+
${actions.map((a) => a.type).join('\n')}`,
82+
pass: false,
83+
}
84+
}
85+
}
86+
return {
87+
message: () => `All actions match the sequence.`,
88+
pass: true,
89+
}
90+
},
91+
})

0 commit comments

Comments
 (0)