Skip to content

Commit 5ea7fb5

Browse files
chore: Migrate workspaces that use jest to vitest (#998)
* docs(contributing): add section on testing Fixes #994 * test(clerk-auth): replace jest with vitest * test(graphql-server): replace jest with vitest * test(medley-router): replace jest with vitest * test(oauth-providers): replace jest with vitest * test(oidc-auth): replace jest with vitest * test(sentry): replace jest with vitest * test(trpc-server): replace jest with vitest * test(typebox-validator): replace jest with vitest * test(typia-validator): replace jest with vitest * test(valibot-validator): replace jest with vitest * chore: cleanup remaining references to jest * chore: fix lint errors * test(typia-validator): fix exclude directory * test(oauth-providers): use vitest environment miniflare * chore: update `yarn.lock` * chore(zod-openapi): pin zod-validator to 0.4.2 * test(graphql-server): remove console filtering --------- Co-authored-by: Yusuke Wada <[email protected]>
1 parent 0f8d2f3 commit 5ea7fb5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+608
-7922
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ The specific flow is as follows
3636
2. Write your middleware. Refer to [hello Middleware](https://github.com/honojs/middleware/tree/main/packages/hello).
3737
3. Create the pull request.
3838

39+
We use [Vitest](https://vitest.dev/) to write and run tests.
40+
Test files should be added to the middleware `src` directory,
41+
and include `.test.` in their file name, for example; `index.test.ts`.
42+
Run the following command in the middleware directory to run tests.
43+
44+
```plain
45+
yarn test
46+
```
47+
3948
We use [changesets](https://github.com/changesets/changesets) to manage releases and CHANGELOG.
4049
Run the following command at the top level to describe any changes.
4150

eslint.config.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import baseConfig from './packages/eslint-config/index.js'
33
export default [
44
...baseConfig,
55
{
6-
ignores: ['**/dist/*'],
6+
ignores: ['**/dist/*', '**/test-generated/*'],
77
},
88
]

jest.config.js

-7
This file was deleted.

package.json

-4
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,12 @@
5858
"@changesets/changelog-github": "^0.4.8",
5959
"@changesets/cli": "^2.26.0",
6060
"@cloudflare/workers-types": "^4.20230307.0",
61-
"@types/jest": "^29.5.11",
6261
"@types/node": "^20.10.4",
6362
"@typescript-eslint/eslint-plugin": "^8.7.0",
6463
"@typescript-eslint/parser": "^8.7.0",
6564
"eslint": "^9.17.0",
66-
"jest": "^29.5.0",
67-
"jest-environment-miniflare": "^2.14.1",
6865
"npm-run-all2": "^6.2.2",
6966
"prettier": "^2.7.1",
70-
"ts-jest": "^29.1.1",
7167
"typescript": "^5.2.2"
7268
},
7369
"packageManager": "[email protected]"

packages/ajv-validator/test/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('With Hook', () => {
193193
{
194194
keyword: 'required',
195195
instancePath: '',
196-
message: "must have required property 'title'",
196+
message: 'must have required property \'title\'',
197197
},
198198
])
199199
})

packages/auth-js/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"@auth/core": "^0.35.3",
6161
"@types/react": "^18",
6262
"hono": "^3.11.7",
63-
"jest": "^29.7.0",
6463
"react": "^18.2.0",
6564
"tsup": "^8.0.1",
6665
"typescript": "^5.3.3",

packages/auth-js/tsconfig.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
"rootDir": "./",
88
"outDir": "./dist",
99
"jsx": "react",
10-
"types": ["jest","node","vitest/globals"]
10+
"types": ["node", "vitest/globals"]
1111
},
12-
"include": [
13-
"src/**/*.ts","src/**/*.tsx"
14-
],
15-
16-
}
12+
"include": ["src/**/*.ts", "src/**/*.tsx"]
13+
}

packages/clerk-auth/jest.config.cjs

-8
This file was deleted.

packages/clerk-auth/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dist"
1111
],
1212
"scripts": {
13-
"test": "jest",
13+
"test": "vitest",
1414
"build": "tsup ./src/index.ts --format esm,cjs --dts",
1515
"prerelease": "yarn build && yarn test",
1616
"release": "yarn publish"
@@ -45,10 +45,10 @@
4545
"@clerk/backend": "^1.0.0",
4646
"@types/react": "^18",
4747
"hono": "^3.11.7",
48-
"jest": "^29.7.0",
4948
"node-fetch-native": "^1.4.0",
5049
"react": "^18.2.0",
51-
"tsup": "^8.0.1"
50+
"tsup": "^8.0.1",
51+
"vitest": "^2.1.8"
5252
},
5353
"engines": {
5454
"node": ">=16.x.x"

packages/clerk-auth/test/index.test.ts packages/clerk-auth/src/index.test.ts

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,31 @@
1-
/* eslint-disable @typescript-eslint/no-explicit-any */
21
import { Hono } from 'hono'
3-
import { clerkMiddleware, getAuth } from '../src'
2+
import { clerkMiddleware, getAuth } from '.'
43

54
const EnvVariables = {
65
CLERK_SECRET_KEY: 'TEST_API_KEY',
76
CLERK_PUBLISHABLE_KEY: 'TEST_API_KEY',
87
}
98

10-
const authenticateRequestMock = jest.fn()
9+
const authenticateRequestMock = vi.fn()
10+
11+
vi.mock(import('@clerk/backend'), async (importOriginal) => {
12+
const original = await importOriginal()
1113

12-
jest.mock('@clerk/backend', () => {
1314
return {
14-
...jest.requireActual('@clerk/backend'),
15-
createClerkClient: () => {
16-
return {
17-
authenticateRequest: (...args: any) => authenticateRequestMock(...args),
18-
}
15+
...original,
16+
createClerkClient(options) {
17+
const client = original.createClerkClient(options)
18+
vi.spyOn(client, 'authenticateRequest').mockImplementation(authenticateRequestMock)
19+
return client
1920
},
2021
}
2122
})
2223

23-
// Test are based on Clerk's test suite for Fastify plugin - https://github.com/clerkinc/javascript/blob/main/packages/fastify/src/withClerkMiddleware.test.ts
24+
// Test are based on Clerk's test suite for Fastify plugin - https://github.com/clerk/javascript/blob/main/packages/fastify/src/__tests__/withClerkMiddleware.test.ts
2425
describe('clerkMiddleware()', () => {
2526
beforeEach(() => {
26-
process.env.CLERK_SECRET_KEY = EnvVariables.CLERK_SECRET_KEY
27-
process.env.CLERK_PUBLISHABLE_KEY = EnvVariables.CLERK_PUBLISHABLE_KEY
28-
jest.clearAllMocks()
29-
jest.restoreAllMocks()
30-
})
31-
32-
afterEach(() => {
33-
jest.clearAllMocks()
27+
vi.stubEnv('CLERK_SECRET_KEY', EnvVariables.CLERK_SECRET_KEY)
28+
vi.stubEnv('CLERK_PUBLISHABLE_KEY', EnvVariables.CLERK_PUBLISHABLE_KEY)
3429
})
3530

3631
test('handles signin with Authorization Bearer', async () => {

packages/clerk-auth/tsconfig.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"compilerOptions": {
44
"rootDir": "./src",
55
"outDir": "./dist",
6+
"types": ["vitest/globals"]
67
},
7-
"include": [
8-
"src/**/*.ts"
9-
],
10-
}
8+
"include": ["src/**/*.ts"]
9+
}

packages/clerk-auth/vitest.config.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
globals: true,
6+
restoreMocks: true,
7+
unstubEnvs: true,
8+
},
9+
})

packages/cloudflare-access/src/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ describe('Cloudflare Access middleware', async () => {
279279
expect(res).not.toBeNull()
280280
expect(res.status).toBe(500)
281281
expect(await res.json()).toEqual({
282-
err: "Error: Authentication error: The Access Organization 'my-cool-team-name' does not exist",
282+
err: 'Error: Authentication error: The Access Organization \'my-cool-team-name\' does not exist',
283283
})
284284
})
285285

packages/graphql-server/jest.config.js

-1
This file was deleted.

packages/graphql-server/package.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"registry": "https://registry.npmjs.org/"
1515
},
1616
"scripts": {
17-
"test": "jest",
17+
"test": "vitest",
1818
"test:all": "yarn test",
1919
"build": "rimraf dist && tsc",
2020
"lint": "eslint --ext js,ts src .eslintrc.js",
@@ -31,7 +31,6 @@
3131
"devDependencies": {
3232
"@cloudflare/workers-types": "^3.14.0",
3333
"@eslint-community/eslint-plugin-eslint-comments": "^4.4.0",
34-
"@types/jest": "^28.1.4",
3534
"@typescript-eslint/eslint-plugin": "^5.21.0",
3635
"@typescript-eslint/parser": "^5.21.0",
3736
"eslint": "^8.57.0",
@@ -42,13 +41,11 @@
4241
"eslint-plugin-import-x": "^4.1.1",
4342
"eslint-plugin-n": "^17.10.2",
4443
"hono": "^4.0.2",
45-
"jest": "^28.1.2",
46-
"jest-environment-miniflare": "^2.6.0",
4744
"np": "^7.6.2",
4845
"prettier": "^2.7.1",
4946
"rimraf": "^3.0.2",
50-
"ts-jest": "^28.0.5",
51-
"typescript": "^4.7.4"
47+
"typescript": "^4.7.4",
48+
"vitest": "^2.1.8"
5249
},
5350
"engines": {
5451
"node": ">=16.0.0"

packages/graphql-server/test/index.test.ts packages/graphql-server/src/index.test.ts

+14-15
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@ import {
77
} from 'graphql'
88
import { Hono } from 'hono'
99
import type { Context, Next } from 'hono'
10-
import { errorMessages, graphqlServer } from '../src'
11-
import type { RootResolver } from '../src'
12-
13-
// Do not show `console.error` messages
14-
jest.spyOn(console, 'error').mockImplementation()
10+
import { errorMessages, graphqlServer } from '.'
11+
import type { RootResolver } from '.'
1512

1613
describe('errorMessages', () => {
17-
const messages = errorMessages(['message a', 'message b'])
18-
expect(messages).toEqual({
19-
errors: [
20-
{
21-
message: 'message a',
22-
},
23-
{
24-
message: 'message b',
25-
},
26-
],
14+
it('Should handle messages', () => {
15+
const messages = errorMessages(['message a', 'message b'])
16+
expect(messages).toEqual({
17+
errors: [
18+
{
19+
message: 'message a',
20+
},
21+
{
22+
message: 'message b',
23+
},
24+
],
25+
})
2726
})
2827
})
2928

packages/graphql-server/test/parse-body.test.ts packages/graphql-server/src/parse-body.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseBody } from '../src/parse-body'
1+
import { parseBody } from './parse-body'
22

33
describe('parseBody', () => {
44
it('Should return a blank JSON object', async () => {

packages/graphql-server/tsconfig.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"compilerOptions": {
44
"rootDir": "./src",
55
"outDir": "./dist",
6+
"types": ["vitest/globals"]
67
},
7-
"include": [
8-
"src/**/*.ts"
9-
],
10-
}
8+
"include": ["src/**/*.ts"]
9+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { defaultExclude, defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
exclude: [...defaultExclude, 'bun_test'],
6+
globals: true,
7+
restoreMocks: true,
8+
unstubEnvs: true,
9+
},
10+
})

packages/medley-router/jest.config.js

-1
This file was deleted.

packages/medley-router/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dist"
99
],
1010
"scripts": {
11-
"test": "jest",
11+
"test": "vitest",
1212
"build": "rimraf dist && tsc",
1313
"prerelease": "yarn build && yarn test",
1414
"release": "yarn publish"
@@ -28,8 +28,8 @@
2828
},
2929
"devDependencies": {
3030
"hono": "^3.11.7",
31-
"jest": "^29.7.0",
32-
"rimraf": "^5.0.5"
31+
"rimraf": "^5.0.5",
32+
"vitest": "^2.1.8"
3333
},
3434
"dependencies": {
3535
"@medley/router": "^0.2.1"

packages/medley-router/tsconfig.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"compilerOptions": {
44
"rootDir": "./src",
55
"outDir": "./dist",
6+
"types": ["vitest/globals"]
67
},
7-
"include": [
8-
"src/**/*.ts"
9-
],
10-
}
8+
"include": ["src/**/*.ts"]
9+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
globals: true,
6+
},
7+
})

packages/oauth-providers/jest.config.js

-7
This file was deleted.

packages/oauth-providers/package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"./dist"
88
],
99
"scripts": {
10-
"test": "jest",
10+
"test": "vitest",
1111
"build": "tsup && publint",
1212
"watch": "tsup --watch",
1313
"publint": "publint",
@@ -112,16 +112,14 @@
112112
},
113113
"devDependencies": {
114114
"@cloudflare/workers-types": "^4.20231025.0",
115-
"@types/jest": "^29.5.7",
116115
"hono": "^4.5.1",
117-
"jest": "^29.7.0",
118-
"jest-environment-miniflare": "^2.14.1",
119116
"msw": "^2.0.11",
120117
"patch-package": "^8.0.0",
121118
"publint": "^0.2.6",
122-
"ts-jest": "^29.1.1",
123119
"tsup": "^8.0.0",
124-
"typescript": "^5.2.2"
120+
"typescript": "^5.2.2",
121+
"vitest": "^2.1.8",
122+
"vitest-environment-miniflare": "^2.14.4"
125123
},
126124
"engines": {
127125
"node": ">=18.4.0"

packages/oauth-providers/tsconfig.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"compilerOptions": {
44
"rootDir": "./src",
55
"outDir": "./dist",
6+
"types": ["vitest/globals"]
67
},
7-
"include": [
8-
"src/**/*.ts"
9-
],
10-
}
8+
"include": ["src/**/*.ts"]
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
environment: 'miniflare',
6+
globals: true,
7+
},
8+
})

0 commit comments

Comments
 (0)