Skip to content

Commit 95df436

Browse files
PuppoTony133
authored andcommitted
refactor(types): migrate from tsd to tstyche
1 parent 583b069 commit 95df436

3 files changed

Lines changed: 155 additions & 99 deletions

File tree

package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"lint:fix": "eslint --fix",
1111
"test": "npm run test:unit && npm run test:typescript",
1212
"test:unit": "c8 --100 node --test",
13-
"test:typescript": "tsd"
13+
"test:typescript": "tstyche"
1414
},
1515
"repository": {
1616
"type": "git",
@@ -62,12 +62,7 @@
6262
"c8": "^11.0.0",
6363
"eslint": "^9.17.0",
6464
"neostandard": "^0.13.0",
65-
"tsd": "^0.33.0"
66-
},
67-
"tsd": {
68-
"compilerOptions": {
69-
"esModuleInterop": true
70-
}
65+
"tstyche": "^7.1.0"
7166
},
7267
"publishConfig": {
7368
"access": "public"

types/index.test-d.ts

Lines changed: 0 additions & 92 deletions
This file was deleted.

types/index.tst.ts

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { expect } from 'tstyche'
2+
import createError, { FastifyError, FastifyErrorConstructor } from '..'
3+
4+
const CustomError = createError('ERROR_CODE', 'message')
5+
expect(CustomError).type.toBe<
6+
FastifyErrorConstructor<{ code: 'ERROR_CODE' }>
7+
>()
8+
const err = new CustomError()
9+
expect(err).type.toBe<FastifyError & { code: 'ERROR_CODE' }>()
10+
expect(err.code).type.toBe<'ERROR_CODE'>()
11+
expect(err.message).type.toBe<string>()
12+
expect(err.statusCode).type.toBe<number | undefined>()
13+
14+
const CustomErrorNoStackTrace = createError(
15+
'ERROR_CODE',
16+
'message',
17+
undefined,
18+
undefined,
19+
false
20+
)
21+
expect(CustomErrorNoStackTrace).type.toBe<
22+
FastifyErrorConstructor<{ code: 'ERROR_CODE' }>
23+
>()
24+
const errNoStackTrace = new CustomErrorNoStackTrace()
25+
expect(errNoStackTrace).type.toBe<FastifyError & { code: 'ERROR_CODE' }>()
26+
expect(errNoStackTrace.code).type.toBe<'ERROR_CODE'>()
27+
expect(errNoStackTrace.message).type.toBe<string>()
28+
expect(errNoStackTrace.statusCode).type.toBe<number | undefined>()
29+
30+
const CustomTypedError = createError('OTHER_CODE', 'message', 400)
31+
expect(CustomTypedError).type.toBe<
32+
FastifyErrorConstructor<{ code: 'OTHER_CODE'; statusCode: 400 }>
33+
>()
34+
const typed = new CustomTypedError()
35+
expect(typed).type.toBe<
36+
FastifyError & { code: 'OTHER_CODE'; statusCode: 400 }
37+
>()
38+
expect(typed.code).type.toBe<'OTHER_CODE'>()
39+
expect(typed.message).type.toBe<string>()
40+
expect(typed.statusCode).type.toBe<400>()
41+
42+
/* eslint-disable no-new */
43+
const CustomTypedArgError = createError<[string]>(
44+
'OTHER_CODE',
45+
'expect %s message',
46+
400
47+
)
48+
CustomTypedArgError('a')
49+
expect(CustomTypedArgError).type.not.toBeCallableWith('a', 'b')
50+
expect(CustomTypedArgError).type.not.toBeConstructableWith('a', 'b')
51+
expect(CustomTypedArgError).type.not.toBeCallableWith(1)
52+
expect(CustomTypedArgError).type.not.toBeConstructableWith(1)
53+
54+
const CustomTypedArgError2 = createError<string, number, [string]>(
55+
'OTHER_CODE',
56+
'expect %s message',
57+
400
58+
)
59+
CustomTypedArgError2('a')
60+
expect(CustomTypedArgError2).type.not.toBeCallableWith('a', 'b')
61+
expect(CustomTypedArgError2).type.not.toBeConstructableWith('a', 'b')
62+
expect(CustomTypedArgError2).type.not.toBeCallableWith(1)
63+
expect(CustomTypedArgError2).type.not.toBeConstructableWith(1)
64+
65+
const CustomTypedArgError3 = createError<string, number, [string, string]>(
66+
'OTHER_CODE',
67+
'expect %s message but got %s',
68+
400
69+
)
70+
expect(CustomTypedArgError3).type.not.toBeCallableWith('a')
71+
CustomTypedArgError3('a', 'b')
72+
new CustomTypedArgError3('a', 'b')
73+
expect(CustomTypedArgError3).type.not.toBeCallableWith(1)
74+
expect(CustomTypedArgError3).type.not.toBeConstructableWith(1)
75+
expect(CustomTypedArgError3).type.not.toBeConstructableWith(1, 2)
76+
expect(CustomTypedArgError3).type.not.toBeConstructableWith('1', 2)
77+
expect(CustomTypedArgError3).type.not.toBeConstructableWith(1, '2')
78+
79+
const CustomTypedArgError4 = createError<string, number, [string, string]>(
80+
'OTHER_CODE',
81+
'expect %s message but got %s',
82+
400
83+
)
84+
expect(CustomTypedArgError4).type.not.toBeCallableWith('a')
85+
CustomTypedArgError4('a', 'b')
86+
new CustomTypedArgError4('a', 'b')
87+
expect(CustomTypedArgError4).type.not.toBeCallableWith(1)
88+
expect(CustomTypedArgError4).type.not.toBeConstructableWith(1)
89+
expect(CustomTypedArgError4).type.not.toBeConstructableWith(1, 2)
90+
expect(CustomTypedArgError4).type.not.toBeConstructableWith('1', 2)
91+
expect(CustomTypedArgError4).type.not.toBeConstructableWith(1, '2')
92+
93+
const CustomTypedArgError5 = createError<[string, string, string, string]>(
94+
'OTHER_CODE',
95+
'expect %s message but got %s. Please contact %s by emailing to %s',
96+
400
97+
)
98+
expect(CustomTypedArgError5).type.not.toBeCallableWith('a')
99+
expect(CustomTypedArgError5).type.not.toBeConstructableWith('a', 'b')
100+
expect(CustomTypedArgError5).type.not.toBeConstructableWith('a', 'b', 'c')
101+
CustomTypedArgError5('a', 'b', 'c', 'd')
102+
expect(CustomTypedArgError5).type.not.toBeConstructableWith(
103+
'a',
104+
'b',
105+
'c',
106+
'd',
107+
'e'
108+
)
109+
110+
const CustomTypedArgError6 = createError<
111+
string,
112+
number,
113+
[string, string, string, string]
114+
>(
115+
'OTHER_CODE',
116+
'expect %s message but got %s. Please contact %s by emailing to %s',
117+
400
118+
)
119+
expect(CustomTypedArgError6).type.not.toBeCallableWith('a')
120+
expect(CustomTypedArgError6).type.not.toBeConstructableWith('a', 'b')
121+
expect(CustomTypedArgError6).type.not.toBeConstructableWith('a', 'b', 'c')
122+
CustomTypedArgError6('a', 'b', 'c', 'd')
123+
expect(CustomTypedArgError6).type.not.toBeConstructableWith(
124+
'a',
125+
'b',
126+
'c',
127+
'd',
128+
'e'
129+
)
130+
131+
const CustomErrorWithErrorConstructor = createError(
132+
'ERROR_CODE',
133+
'message',
134+
500,
135+
TypeError
136+
)
137+
expect(CustomErrorWithErrorConstructor).type.toBe<
138+
FastifyErrorConstructor<{ code: 'ERROR_CODE'; statusCode: 500 }>
139+
>()
140+
CustomErrorWithErrorConstructor({ cause: new Error('Error') })
141+
const customErrorWithErrorConstructor = CustomErrorWithErrorConstructor()
142+
if (customErrorWithErrorConstructor instanceof FastifyError) {
143+
expect(customErrorWithErrorConstructor.code).type.toBe<'ERROR_CODE'>()
144+
expect(customErrorWithErrorConstructor.message).type.toBe<string>()
145+
expect(customErrorWithErrorConstructor.statusCode).type.toBe<500>()
146+
}
147+
148+
const error = new FastifyError('ERROR_CODE', 'message', 500)
149+
if (error instanceof FastifyError) {
150+
expect(error.code).type.toBe<string>()
151+
expect(error.message).type.toBe<string>()
152+
expect(error.statusCode).type.toBe<number | undefined>()
153+
}

0 commit comments

Comments
 (0)