Skip to content

Commit d8e4ed6

Browse files
committed
declare error shape once
1 parent 116db6e commit d8e4ed6

File tree

1 file changed

+12
-35
lines changed

1 file changed

+12
-35
lines changed

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

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,12 @@ describe('timeout behavior', () => {
11891189
})
11901190

11911191
describe('endpoint schemas', () => {
1192+
const serializedSchemaError = {
1193+
name: 'SchemaError',
1194+
message: expect.any(String),
1195+
stack: expect.any(String),
1196+
} satisfies SerializedError
1197+
11921198
test("can be used to validate the endpoint's arguments", async () => {
11931199
const api = createApi({
11941200
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }),
@@ -1215,11 +1221,7 @@ describe('endpoint schemas', () => {
12151221
api.endpoints.query.initiate({ id: '1' }),
12161222
)
12171223

1218-
expect(invalidResult?.error).toEqual<SerializedError>({
1219-
name: 'SchemaError',
1220-
message: expect.any(String),
1221-
stack: expect.any(String),
1222-
})
1224+
expect(invalidResult?.error).toEqual(serializedSchemaError)
12231225
})
12241226
test("can be used to validate the endpoint's raw result", async () => {
12251227
const api = createApi({
@@ -1235,18 +1237,9 @@ describe('endpoint schemas', () => {
12351237
withoutTestLifecycles: true,
12361238
})
12371239
const result = await storeRef.store.dispatch(api.endpoints.query.initiate())
1238-
expect(result?.error).toEqual<SerializedError>({
1239-
name: 'SchemaError',
1240-
message: expect.any(String),
1241-
stack: expect.any(String),
1242-
})
1240+
expect(result?.error).toEqual(serializedSchemaError)
12431241
})
12441242
test("can be used to validate the endpoint's final result", async () => {
1245-
server.use(
1246-
http.get('https://example.com/success/', () =>
1247-
HttpResponse.json({ success: true }),
1248-
),
1249-
)
12501243
const api = createApi({
12511244
baseQuery: fetchBaseQuery({ baseUrl: 'https://example.com' }),
12521245
endpoints: (build) => ({
@@ -1261,11 +1254,7 @@ describe('endpoint schemas', () => {
12611254
withoutTestLifecycles: true,
12621255
})
12631256
const result = await storeRef.store.dispatch(api.endpoints.query.initiate())
1264-
expect(result?.error).toEqual<SerializedError>({
1265-
name: 'SchemaError',
1266-
message: expect.any(String),
1267-
stack: expect.any(String),
1268-
})
1257+
expect(result?.error).toEqual(serializedSchemaError)
12691258
})
12701259
test("can be used to validate the endpoint's raw error result", async () => {
12711260
const api = createApi({
@@ -1284,11 +1273,7 @@ describe('endpoint schemas', () => {
12841273
withoutTestLifecycles: true,
12851274
})
12861275
const result = await storeRef.store.dispatch(api.endpoints.query.initiate())
1287-
expect(result?.error).toEqual<SerializedError>({
1288-
name: 'SchemaError',
1289-
message: expect.any(String),
1290-
stack: expect.any(String),
1291-
})
1276+
expect(result?.error).toEqual(serializedSchemaError)
12921277
})
12931278
test("can be used to validate the endpoint's final error result", async () => {
12941279
const api = createApi({
@@ -1313,11 +1298,7 @@ describe('endpoint schemas', () => {
13131298
withoutTestLifecycles: true,
13141299
})
13151300
const result = await storeRef.store.dispatch(api.endpoints.query.initiate())
1316-
expect(result?.error).toEqual<SerializedError>({
1317-
name: 'SchemaError',
1318-
message: expect.any(String),
1319-
stack: expect.any(String),
1320-
})
1301+
expect(result?.error).toEqual(serializedSchemaError)
13211302
})
13221303
test("can be used to validate the endpoint's meta result", async () => {
13231304
const api = createApi({
@@ -1337,10 +1318,6 @@ describe('endpoint schemas', () => {
13371318
withoutTestLifecycles: true,
13381319
})
13391320
const result = await storeRef.store.dispatch(api.endpoints.query.initiate())
1340-
expect(result?.error).toEqual<SerializedError>({
1341-
name: 'SchemaError',
1342-
message: expect.any(String),
1343-
stack: expect.any(String),
1344-
})
1321+
expect(result?.error).toEqual(serializedSchemaError)
13451322
})
13461323
})

0 commit comments

Comments
 (0)