Skip to content

Commit 58d39a0

Browse files
vezaynkairhorns
authored andcommitted
Jest to Vitest migration
1 parent b8bc330 commit 58d39a0

29 files changed

+990
-2564
lines changed

jest.config.js

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

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"release": "pnpm -F=fastify-renderer publish",
1212
"preinstall": "npx only-allow pnpm",
1313
"prerelease": "pnpm -F=fastify-renderer run gitpkg publish",
14-
"test": "jest --forceExit -w=1",
15-
"test:debug": "cross-env FR_DEBUG_SERVE=1 node --inspect-brk ./node_modules/.bin/jest --forceExit"
14+
"test": "vitest --no-threads"
1615
},
1716
"repository": {
1817
"type": "git",
@@ -44,9 +43,8 @@
4443
"eslint-plugin-prettier": "^4.2.1",
4544
"eslint-plugin-react": "^7.33.2",
4645
"eslint-plugin-react-hooks": "^4.6.0",
47-
"expect-playwright": "^0.8.0",
4846
"fs-extra": "^11.1.0",
49-
"jest": "^29.7.0",
47+
"vitest": "^0.34.6",
5048
"playwright-chromium": "^1.39.0",
5149
"prettier": "^2.8.8",
5250
"prettier-plugin-organize-imports": "^3.2.3",

packages/fastify-renderer/jest.config.js

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

packages/fastify-renderer/package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"lint:fix": "prettier --loglevel warn --write \"{src,test}/**/*.{ts,tsx}\" && eslint \"{src,test}/**/*.{ts,tsx}\" --quiet --fix",
3030
"prepublishOnly": "npm run build",
3131
"test": "run-s build test:unit lint",
32-
"test:unit": "jest"
32+
"test:unit": "vitest"
3333
},
3434
"repository": {
3535
"type": "git",
@@ -77,9 +77,7 @@
7777
},
7878
"devDependencies": {
7979
"@swc/core": "^1.3.95",
80-
"@swc/jest": "^0.2.29",
8180
"@types/connect": "^3.4.35",
82-
"@types/jest": "^29.5.6",
8381
"@types/node": "^18.11.9",
8482
"@types/react": "^17.0.43",
8583
"@types/react-dom": "^17.0.11",
@@ -95,7 +93,7 @@
9593
"eslint-plugin-react-hooks": "^4.6.0",
9694
"fastify": "^4.24.2",
9795
"gitpkg": "^1.0.0-beta.2",
98-
"jest": "^29.7.0",
96+
"vitest": "^0.34.6",
9997
"npm-run-all": "^4.1.5",
10098
"pino-pretty": "^8.1.0",
10199
"prettier": "^2.7.1",
@@ -111,4 +109,4 @@
111109
"README.md",
112110
"LICENSE"
113111
]
114-
}
112+
}

packages/fastify-renderer/src/node/Plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @typescript-eslint/require-await */
22
import '@fastify/middie'
3-
import fs from 'fs'
3+
import fs from 'node:fs'
44
import path from 'path'
55
import { InlineConfig } from 'vite'
66
import { Template } from './DocumentTemplate'
@@ -39,7 +39,7 @@ export class FastifyRendererPlugin {
3939
registeredComponents: Record<ImperativeRenderable, RenderableRegistration> = {}
4040

4141
constructor(incomingOptions: FastifyRendererOptions) {
42-
this.devMode = incomingOptions.devMode ?? process.env.NODE_ENV != 'production'
42+
this.devMode = incomingOptions.devMode ?? (process.env.NODE_ENV != 'production' || process.env.TEST == 'true')
4343

4444
this.vite = incomingOptions.vite || {}
4545
this.vite.base ??= '/.vite/'

packages/fastify-renderer/src/node/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import '@fastify/middie'
44
import fastifyStatic from '@fastify/static'
55
import { FastifyInstance, FastifyPluginAsync, FastifyReply } from 'fastify'
66
import fp from 'fastify-plugin'
7-
import { promises as fs } from 'fs'
7+
import { promises as fs } from 'node:fs'
88
import path from 'path'
99
import { resolveConfig, build as viteBuild, createServer, ResolvedConfig, ViteDevServer } from 'vite'
1010
import { DefaultDocumentTemplate } from './DocumentTemplate'
@@ -53,7 +53,7 @@ const plugin: FastifyPluginAsync<FastifyRendererOptions> = async (fastify, incom
5353

5454
fastify.setRenderConfig({
5555
base: incomingOptions.base || '',
56-
layout: incomingOptions.layout || require.resolve('./renderers/react/DefaultLayout'),
56+
layout: incomingOptions.layout || require.resolve('./renderers/react/DefaultLayout.tsx'),
5757
document: incomingOptions.document || DefaultDocumentTemplate,
5858
})
5959

packages/fastify-renderer/src/node/renderers/react/ReactRenderer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class ReactRenderer implements Renderer {
4646
clientModulePath: string
4747

4848
constructor(readonly plugin: FastifyRendererPlugin, readonly options: ReactRendererOptions) {
49-
this.clientModulePath = require.resolve('../../../client/react')
49+
this.clientModulePath = require.resolve('../../../client/react/index.ts')
5050
}
5151

5252
vitePlugins() {

packages/fastify-renderer/test/FastifyRenderer.spec.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { promises as fs } from 'fs'
1+
import { promises as fs } from 'node:fs'
22
import path from 'path'
33
import * as Vite from 'vite'
44
import FastifyRenderer, { build } from '../src/node'
55
import { FastifyRendererPlugin } from '../src/node/Plugin'
66
import { kRenderOptions } from '../src/node/symbols'
77
import { newFastify } from './helpers'
8+
import { describe, beforeEach, vi, test, expect } from 'vitest'
89

910
const testComponent = require.resolve(path.join(__dirname, 'fixtures', 'test-module.tsx'))
1011
const testLayoutComponent = require.resolve(path.join(__dirname, 'fixtures', 'test-layout.tsx'))
@@ -13,7 +14,7 @@ const options = { vite: { root: __dirname, logLevel: (process.env.LOG_LEVEL ?? '
1314
describe('FastifyRenderer', () => {
1415
let server
1516
beforeEach(async () => {
16-
jest.clearAllMocks()
17+
vi.clearAllMocks()
1718

1819
server = await newFastify()
1920
await server.register(FastifyRenderer, options)
@@ -62,8 +63,8 @@ describe('FastifyRenderer', () => {
6263

6364
test('should close vite devServer when fastify server is closing in dev mode', async () => {
6465
const devServer = await Vite.createServer()
65-
const closeSpy = jest.spyOn(devServer, 'close')
66-
jest.spyOn(Vite, 'createServer').mockImplementation(async () => devServer)
66+
const closeSpy = vi.spyOn(devServer, 'close')
67+
vi.spyOn(Vite, 'createServer').mockImplementation(async () => devServer)
6768

6869
server = await newFastify()
6970
await server.register(FastifyRenderer, { ...options, devMode: true })
@@ -74,7 +75,7 @@ describe('FastifyRenderer', () => {
7475
})
7576

7677
test('should do nothing if the registered route is not renderable', async () => {
77-
const registerRouteSpy = jest.spyOn(FastifyRendererPlugin.prototype, 'register')
78+
const registerRouteSpy = vi.spyOn(FastifyRendererPlugin.prototype, 'register')
7879

7980
server.get('/', async (_request, reply) => reply.send('Hello'))
8081
await server.inject({ method: 'GET', url: '/' })
@@ -83,7 +84,9 @@ describe('FastifyRenderer', () => {
8384
})
8485

8586
test("should register the route in the plugin if it's renderable", async () => {
86-
const registerRouteSpy = jest.spyOn(FastifyRendererPlugin.prototype, 'register').mockImplementation(jest.fn())
87+
const registerRouteSpy = vi
88+
.spyOn(FastifyRendererPlugin.prototype, 'register')
89+
.mockImplementation(vi.fn(() => null as any))
8790

8891
server.get('/', { render: testComponent }, async (request, reply) => reply.send('Hello'))
8992
const response = await server.inject({ method: 'GET', url: '/' })
@@ -104,9 +107,9 @@ describe('build()', () => {
104107
await server.register(FastifyRenderer, options)
105108
await server.listen({ port: 0 })
106109

107-
jest.spyOn(fs, 'writeFile').mockImplementation(jest.fn())
108-
jest.spyOn(path, 'join').mockImplementation(jest.fn())
109-
const viteBuildSpy = jest.spyOn(Vite, 'build').mockImplementation(jest.fn())
110+
vi.spyOn(fs, 'writeFile').mockImplementation(vi.fn(() => null as any))
111+
vi.spyOn(path, 'join').mockImplementation(vi.fn(() => null as any))
112+
const viteBuildSpy = vi.spyOn(Vite, 'build').mockImplementation(vi.fn(() => null as any))
110113

111114
await build(server)
112115

0 commit comments

Comments
 (0)