Skip to content

Commit 5abe691

Browse files
renovate[bot]ndhoule
authored andcommitted
refactor: update Vitest to v3
This involved fixing the incorrect use of the global `expect` in concurrent tests and replacing the deprecated `TaskContext` type with `TestContext`.
1 parent 11a8bfd commit 5abe691

File tree

8 files changed

+998
-1136
lines changed

8 files changed

+998
-1136
lines changed

package-lock.json

+942-1,109
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
"@types/through2-map": "3.0.4",
196196
"@types/write-file-atomic": "4.0.3",
197197
"@types/ws": "8.18.0",
198-
"@vitest/coverage-v8": "1.6.1",
198+
"@vitest/coverage-v8": "3.0.8",
199199
"@vitest/eslint-plugin": "^1.1.36",
200200
"c8": "10.1.3",
201201
"cheerio": "1.0.0",
@@ -217,7 +217,7 @@
217217
"typescript": "5.7.3",
218218
"typescript-eslint": "^8.26.0",
219219
"verdaccio": "6.0.5",
220-
"vitest": "1.6.1"
220+
"vitest": "3.0.8"
221221
},
222222
"ava": {
223223
"files": [

tests/integration/commands/build/build.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path, { join } from 'path'
22
import process from 'process'
33

44
import execa from 'execa'
5-
import { describe, expect, test, type TaskContext, type TestContext } from 'vitest'
5+
import { describe, expect, test, type TestContext } from 'vitest'
66

77
import { callCli } from '../../utils/call-cli.js'
88
import { cliPath } from '../../utils/cli-path.js'
@@ -22,7 +22,7 @@ const defaultEnvs = {
2222
* - that its output contains `output`
2323
*/
2424
const runBuildCommand = async function (
25-
t: TaskContext & TestContext,
25+
t: TestContext,
2626
cwd: string,
2727
options: Partial<{
2828
exitCode: number

tests/integration/commands/dev/edge-functions.test.ts

+30-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { join } from 'path'
44

55
import execa from 'execa'
66
import fetch from 'node-fetch'
7-
import { describe, expect, test } from 'vitest'
7+
import { describe, test } from 'vitest'
88

99
import { withDevServer } from '../../utils/dev-server.js'
1010
import { FixtureTestContext, setupFixtureTests } from '../../utils/fixture.js'
@@ -51,7 +51,7 @@ describe.skipIf(isWindows)('edge functions', async () => {
5151
'dev-server-with-edge-functions',
5252
{ devServer: true, mockApi: { routes }, setupAfterDev: recreateEdgeFunctions },
5353
() => {
54-
test<FixtureTestContext>('should run edge functions in correct order', async ({ devServer }) => {
54+
test<FixtureTestContext>('should run edge functions in correct order', async ({ devServer, expect }) => {
5555
const response = await fetch(`http://localhost:${devServer!.port}/ordertest`)
5656
const body = await response.text()
5757

@@ -71,7 +71,7 @@ describe.skipIf(isWindows)('edge functions', async () => {
7171
])
7272
})
7373

74-
test<FixtureTestContext>('should provide context properties', async ({ devServer }) => {
74+
test<FixtureTestContext>('should provide context properties', async ({ devServer, expect }) => {
7575
const response = await fetch(`http://localhost:${devServer!.port}/context`)
7676

7777
const body = await response.json()
@@ -87,7 +87,7 @@ describe.skipIf(isWindows)('edge functions', async () => {
8787
})
8888
})
8989

90-
test<FixtureTestContext>('should expose URL parameters', async ({ devServer }) => {
90+
test<FixtureTestContext>('should expose URL parameters', async ({ devServer, expect }) => {
9191
const response = await fetch(`http://localhost:${devServer!.port}/categories/foo/products/bar`)
9292

9393
const body = await response.json()
@@ -99,6 +99,7 @@ describe.skipIf(isWindows)('edge functions', async () => {
9999

100100
test<FixtureTestContext>('should expose URL parameters to edge functions with `cache: "manual"`', async ({
101101
devServer,
102+
expect,
102103
}) => {
103104
const response = await fetch(`http://localhost:${devServer!.port}/categories-after-cache/foo/products/bar`)
104105

@@ -109,7 +110,7 @@ describe.skipIf(isWindows)('edge functions', async () => {
109110
})
110111
})
111112

112-
test<FixtureTestContext>('should respect config.methods field', async ({ devServer }) => {
113+
test<FixtureTestContext>('should respect config.methods field', async ({ devServer, expect }) => {
113114
const responseGet = await fetch(`http://localhost:${devServer!.port}/products/really-bad-product`, {
114115
method: 'GET',
115116
})
@@ -125,6 +126,7 @@ describe.skipIf(isWindows)('edge functions', async () => {
125126

126127
test<FixtureTestContext>('should show an error page when an edge function has an uncaught exception', async ({
127128
devServer,
129+
expect,
128130
}) => {
129131
const [plainTextResponse, htmlResponse] = await Promise.all([
130132
fetch(`http://localhost:${devServer!.port}/uncaught-exception`, {
@@ -148,6 +150,7 @@ describe.skipIf(isWindows)('edge functions', async () => {
148150

149151
test<FixtureTestContext>('should set the `URL`, `SITE_ID`, and `SITE_NAME` environment variables', async ({
150152
devServer,
153+
expect,
151154
}) => {
152155
const body = (await fetch(`http://localhost:${devServer!.port}/echo-env`).then((res) => res.json())) as Record<
153156
string,
@@ -171,6 +174,7 @@ describe.skipIf(isWindows)('edge functions', async () => {
171174
() => {
172175
test<FixtureTestContext>('skips edge functions when --internal-disable-edge-functions is passed', async ({
173176
devServer,
177+
expect,
174178
}) => {
175179
const response = await fetch(`http://localhost:${devServer!.port}/ordertest`)
176180
const body = await response.text()
@@ -183,7 +187,11 @@ describe.skipIf(isWindows)('edge functions', async () => {
183187
)
184188

185189
await setupFixtureTests('dev-server-with-edge-functions', { devServer: true, mockApi: { routes } }, () => {
186-
test<FixtureTestContext>('should not remove other edge functions on change', async ({ devServer, fixture }) => {
190+
test<FixtureTestContext>('should not remove other edge functions on change', async ({
191+
devServer,
192+
expect,
193+
fixture,
194+
}) => {
187195
// we need to wait till file watchers are loaded
188196
await pause(500)
189197

@@ -202,6 +210,8 @@ describe.skipIf(isWindows)('edge functions', async () => {
202210
})
203211

204212
test('should reload on change to transitive dependency', async (t) => {
213+
const { expect } = t
214+
205215
await withSiteBuilder(t, async (builder) => {
206216
await builder
207217
.withContentFile({
@@ -223,8 +233,9 @@ describe.skipIf(isWindows)('edge functions', async () => {
223233
.build()
224234

225235
await withDevServer({ cwd: builder.directory }, async (server) => {
226-
const response = await fetch(server.url, {}).then((res) => res.text())
227-
t.expect(response).toEqual('foo')
236+
const response = await fetch(server.url, {})
237+
const text = await response.text()
238+
expect(text).toEqual('foo')
228239

229240
// update file
230241
await builder
@@ -236,13 +247,16 @@ describe.skipIf(isWindows)('edge functions', async () => {
236247

237248
await pause(500)
238249

239-
const response2 = await fetch(server.url, {}).then((res) => res.text())
240-
t.expect(response2).toEqual('bar')
250+
const response2 = await fetch(server.url, {})
251+
const text2 = await response2.text()
252+
expect(text2).toEqual('bar')
241253
})
242254
})
243255
})
244256

245257
test('functions and edge functions should receive url-encoded search params in the same way', async (t) => {
258+
const { expect } = t
259+
246260
await withSiteBuilder(t, async (builder) => {
247261
await builder
248262
.withContentFile({
@@ -264,8 +278,8 @@ describe.skipIf(isWindows)('edge functions', async () => {
264278
await withDevServer({ cwd: builder.directory }, async (server) => {
265279
const funcResponse = await fetch(new URL('/func?1,2,3', server.url), {})
266280
const efResponse = await fetch(new URL('/ef?1,2,3', server.url), {})
267-
t.expect(await funcResponse.text()).toEqual('?1,2,3')
268-
t.expect(await efResponse.text()).toEqual('?1,2,3')
281+
expect(await funcResponse.text()).toEqual('?1,2,3')
282+
expect(await efResponse.text()).toEqual('?1,2,3')
269283
})
270284
})
271285
})
@@ -274,7 +288,10 @@ describe.skipIf(isWindows)('edge functions', async () => {
274288
'dev-server-with-edge-functions-and-npm-modules',
275289
{ devServer: true, mockApi: { routes }, setup },
276290
() => {
277-
test<FixtureTestContext>('should run an edge function that uses the Blobs npm module', async ({ devServer }) => {
291+
test<FixtureTestContext>('should run an edge function that uses the Blobs npm module', async ({
292+
devServer,
293+
expect,
294+
}) => {
278295
const res = await fetch(`http://localhost:${devServer!.port}/blobs`, {
279296
method: 'GET',
280297
})

tests/integration/commands/functions-create/functions-create.test.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { readFile } from 'fs/promises'
33
import { join } from 'path'
44

55
import execa from 'execa'
6-
import { describe, expect, test } from 'vitest'
6+
import { describe, test } from 'vitest'
77

88
import { fileExistsAsync } from '../../../../src/lib/fs.js'
99
import { cliPath } from '../../utils/cli-path.js'
@@ -36,6 +36,8 @@ describe.concurrent('functions:create command', async () => {
3636
]
3737

3838
test('should create a new function directory when none is found', async (t) => {
39+
const { expect } = t
40+
3941
await withSiteBuilder(t, async (builder) => {
4042
await builder.build()
4143
await withMockApi(routes, async ({ apiUrl }) => {
@@ -72,6 +74,8 @@ describe.concurrent('functions:create command', async () => {
7274
})
7375

7476
test('should create a new edge function directory when none is found', async (t) => {
77+
const { expect } = t
78+
7579
await withSiteBuilder(t, async (builder) => {
7680
await builder.build()
7781
await withMockApi(routes, async ({ apiUrl }) => {
@@ -108,6 +112,8 @@ describe.concurrent('functions:create command', async () => {
108112
})
109113

110114
test('should use specified edge function directory when found', async (t) => {
115+
const { expect } = t
116+
111117
await withSiteBuilder(t, async (builder) => {
112118
builder.withNetlifyToml({ config: { build: { edge_functions: 'somethingEdgy' } } })
113119
await builder.build()
@@ -144,6 +150,8 @@ describe.concurrent('functions:create command', async () => {
144150
})
145151

146152
test('should not create a new function directory when one is found', async (t) => {
153+
const { expect } = t
154+
147155
await withSiteBuilder(t, async (builder) => {
148156
builder.withNetlifyToml({ config: { build: { functions: 'functions' } } })
149157

@@ -181,6 +189,8 @@ describe.concurrent('functions:create command', async () => {
181189
})
182190

183191
test('should only show function templates for the language specified via the --language flag, if one is present', async (t) => {
192+
const { expect } = t
193+
184194
const createWithLanguageTemplate = async (language: string, outputPath: string) => {
185195
await withSiteBuilder(t, async (builder) => {
186196
await builder.build()
@@ -225,6 +235,8 @@ describe.concurrent('functions:create command', async () => {
225235
})
226236

227237
test('throws an error when the --language flag contains an unsupported value', async (t) => {
238+
const { expect } = t
239+
228240
await withSiteBuilder(t, async (builder) => {
229241
await builder.build()
230242

@@ -264,7 +276,7 @@ describe.concurrent('functions:create command', async () => {
264276
})
265277

266278
await setupFixtureTests('nx-integrated-monorepo', () => {
267-
test<FixtureTestContext>('should create a new edge function', async ({ fixture, expect }) => {
279+
test<FixtureTestContext>('should create a new edge function', async ({ expect, fixture }) => {
268280
await withMockApi(routes, async ({ apiUrl }) => {
269281
const childProcess = execa(
270282
cliPath,
@@ -319,7 +331,7 @@ describe.concurrent('functions:create command', async () => {
319331
// await devServer.waitForLogMatching('Loaded edge function new')
320332
// expect(devServer.output).not.toContain('Removed edge function')
321333
})
322-
test<FixtureTestContext>('should create a new serverless function', async ({ fixture }) => {
334+
test<FixtureTestContext>('should create a new serverless function', async ({ expect, fixture }) => {
323335
await withMockApi(routes, async ({ apiUrl }) => {
324336
const childProcess = execa(
325337
cliPath,

tests/integration/commands/init/init.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { readFile } from 'node:fs/promises'
44
import cleanDeep from 'clean-deep'
55
import execa from 'execa'
66
import toml from 'toml'
7-
import { describe, test, type TaskContext, type TestContext } from 'vitest'
7+
import { describe, test, type TestContext } from 'vitest'
88

99
import { cliPath } from '../../utils/cli-path.js'
1010
import { CONFIRM, DOWN, answerWithValue, handleQuestions } from '../../utils/handle-questions.js'
@@ -14,7 +14,7 @@ import { withSiteBuilder } from '../../utils/site-builder.js'
1414
const defaultFunctionsDirectory = 'netlify/functions'
1515

1616
const assertNetlifyToml = async (
17-
t: TaskContext & TestContext,
17+
t: TestContext,
1818
tomlDir: string,
1919
{ command, functions, publish }: { command: string; functions: string; publish: string },
2020
) => {

tests/integration/utils/fixture.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { MockApi, Route, getCLIOptions, startMockApi } from './mock-api-vitest.j
1212
import { SiteBuilder } from './site-builder.js'
1313

1414
const FIXTURES_DIRECTORY = fileURLToPath(new URL('../__fixtures__/', import.meta.url))
15-
const HOOK_TIMEOUT = 30_000
15+
const HOOK_TIMEOUT = 60_000
1616

1717
interface MockApiOptions {
1818
routes: Route[]

tests/integration/utils/site-builder.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import serializeJS from 'serialize-javascript'
1313
import tempDirectory from 'temp-dir'
1414
import tomlify from 'tomlify-j0.4'
1515
import { v4 as uuidv4 } from 'uuid'
16-
import type { TaskContext } from 'vitest'
16+
import type { TestContext } from 'vitest'
1717

1818
const ensureDir = (directory: string) => mkdir(directory, { recursive: true })
1919

@@ -331,15 +331,15 @@ export const createSiteBuilder = ({ siteName }: { siteName: string }) => {
331331
}
332332

333333
/**
334-
* @param taskContext used to infer directory name from test name
334+
* @param context used to infer directory name from test name
335335
*/
336336
export async function withSiteBuilder<T>(
337-
taskContext: TaskContext,
337+
context: TestContext,
338338
testHandler: (builder: SiteBuilder) => Promise<T>,
339339
): Promise<T> {
340340
let builder: SiteBuilder | undefined
341341
try {
342-
builder = createSiteBuilder({ siteName: slugify(taskContext.task.name) })
342+
builder = createSiteBuilder({ siteName: slugify(context.task.name) })
343343
return await testHandler(builder)
344344
} finally {
345345
if (builder) await builder.cleanup()

0 commit comments

Comments
 (0)