Skip to content

Commit 417918f

Browse files
authored
chore: pack/build before all e2e tests (#2395)
1 parent 41f00e1 commit 417918f

6 files changed

+52
-37
lines changed

test/e2e/auto-update-worker.node.test.ts

+5-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import fs from 'node:fs'
2-
import { execSync } from 'node:child_process'
2+
import { inject } from 'vitest'
33
import { createTeardown } from 'fs-teardown'
44
import { fromTemp } from '../support/utils'
5-
import * as packageJson from '../../package.json'
5+
6+
const tarballPath = inject('tarballPath')
67

78
const fsMock = createTeardown({
89
rootDir: fromTemp('worker-script-auto-update'),
@@ -38,15 +39,8 @@ describe(
3839
}),
3940
})
4041

41-
// Pack the current state of the "msw" package.
42-
execSync(`pnpm pack --pack-destination ${fsMock.resolve('.')}`, {
43-
stdio: [null, null, 'inherit'],
44-
})
45-
4642
// Install "msw" from the tarball into the dummy project.
47-
const installCommand = await fsMock.exec(
48-
`npm install msw-${packageJson.version}.tgz`,
49-
)
43+
const installCommand = await fsMock.exec(`npm install ${tarballPath}`)
5044
expect(installCommand.stderr).toBe('')
5145

5246
// Asset the worker script has been created/updated.
@@ -65,13 +59,7 @@ describe(
6559
}),
6660
})
6761

68-
execSync(`pnpm pack --pack-destination ${fsMock.resolve('.')}`, {
69-
stdio: [null, null, 'inherit'],
70-
})
71-
72-
const installCommand = await fsMock.exec(
73-
`npm install msw-${packageJson.version}.tgz`,
74-
)
62+
const installCommand = await fsMock.exec(`npm install ${tarballPath}`)
7563
/**
7664
* @note Cannot assert on the empty stderr because npm
7765
* writes to stderr if there's a new version of npm available.

test/node/msw-api/cli/init.node.test.ts test/e2e/cli-init.node.test.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
// @vitest-environment node
2-
import fs from 'fs'
1+
import fs from 'node:fs'
32
import path from 'node:path'
3+
import { spawnSync } from 'node:child_process'
44
import { createTeardown } from 'fs-teardown'
5-
import { fromTemp } from '../../../support/utils'
5+
import { fromTemp } from '../support/utils'
66

77
const fsMock = createTeardown({
88
rootDir: fromTemp('cli/init'),
99
})
1010

11-
const cliPath = require.resolve('../../../../cli/index.js')
11+
const cliPath = require.resolve('../../cli/index.js')
1212

1313
function readJson(filePath: string) {
1414
const rawContent = fs.readFileSync(filePath, 'utf8')
@@ -21,6 +21,7 @@ function readJson(filePath: string) {
2121
}
2222

2323
beforeAll(async () => {
24+
spawnSync('pnpm', ['build'])
2425
await fsMock.prepare()
2526
})
2627

@@ -202,7 +203,7 @@ test('throws if creating a directory under path failed', async () => {
202203
* @note Require the "init" command source
203204
* so that the "fs" mocks could apply.
204205
*/
205-
const init = require('../../../../cli/init')
206+
const init = require('../../cli/init')
206207

207208
// Mock the "mkdir" method throwing an error.
208209
const error = new Error('Failed to create directory')
@@ -333,7 +334,7 @@ test('prints the list of failed paths to copy', async () => {
333334
}
334335
})
335336

336-
const init = require('../../../../cli/init')
337+
const init = require('../../cli/init')
337338
const copyFileError = new Error('Failed to copy file')
338339

339340
const consoleLogSpy = vi

test/e2e/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"extends": "../../tsconfig.base.json",
3-
"include": ["./**/*.test.ts"],
3+
"include": ["./**/*.test.ts", "./vitest.d.ts", "./vitest.global.setup.ts"],
44
"compilerOptions": {
55
"types": ["node", "vitest/globals"],
66
"resolveJsonModule": true,

test/e2e/vitest.config.mts

+1-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
import { defineConfig } from 'vitest/config'
2-
import { mswExports, customViteEnvironments } from '../support/alias'
32

43
export default defineConfig({
54
test: {
65
dir: './test/e2e',
76
globals: true,
87
environment: 'node',
9-
poolOptions: {
10-
threads: {
11-
/**
12-
* @note Run Node.js integration tests in sequence.
13-
* There's a test that involves building the library,
14-
* which results in the "lib" directory being deleted.
15-
* If any tests attempt to run during that window,
16-
* they will fail, unable to resolve the "msw" import alias.
17-
*/
18-
singleThread: true,
19-
},
20-
},
8+
globalSetup: './test/e2e/vitest.global.setup.ts',
219
},
2210
})

test/e2e/vitest.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare module 'vitest' {
2+
export interface ProvidedContext {
3+
tarballPath: string
4+
}
5+
}
6+
7+
export {}

test/e2e/vitest.global.setup.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import fs from 'node:fs'
2+
import { fileURLToPath } from 'node:url'
3+
import { spawnSync } from 'node:child_process'
4+
import { invariant } from 'outvariant'
5+
import type { GlobalSetupContext } from 'vitest/node'
6+
import * as packageJson from '../../package.json'
7+
8+
export default function setup({ provide }: GlobalSetupContext) {
9+
const tarballPath = fileURLToPath(
10+
new URL(`../../msw-${packageJson.version}.tgz`, import.meta.url),
11+
)
12+
13+
if (fs.existsSync(tarballPath)) {
14+
return
15+
}
16+
17+
// Pack the library before all E2E tests.
18+
spawnSync('pnpm', ['pack'], {
19+
stdio: 'inherit',
20+
})
21+
22+
invariant(
23+
fs.existsSync(tarballPath),
24+
'Failed to set up e2e tests: library tarball not found at "%s"',
25+
tarballPath,
26+
)
27+
28+
console.log('Library built at "%s"!', tarballPath)
29+
30+
provide('tarballPath', tarballPath)
31+
}

0 commit comments

Comments
 (0)