From 417918fce08a8d9d9ae504aa22b23497375f24e3 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Sun, 15 Dec 2024 18:39:09 +0100 Subject: [PATCH] chore: pack/build before all e2e tests (#2395) --- test/e2e/auto-update-worker.node.test.ts | 22 +++---------- .../cli-init.node.test.ts} | 13 ++++---- test/e2e/tsconfig.json | 2 +- test/e2e/vitest.config.mts | 14 +-------- test/e2e/vitest.d.ts | 7 +++++ test/e2e/vitest.global.setup.ts | 31 +++++++++++++++++++ 6 files changed, 52 insertions(+), 37 deletions(-) rename test/{node/msw-api/cli/init.node.test.ts => e2e/cli-init.node.test.ts} (97%) create mode 100644 test/e2e/vitest.d.ts create mode 100644 test/e2e/vitest.global.setup.ts diff --git a/test/e2e/auto-update-worker.node.test.ts b/test/e2e/auto-update-worker.node.test.ts index 69d9fe4be..2de7917aa 100644 --- a/test/e2e/auto-update-worker.node.test.ts +++ b/test/e2e/auto-update-worker.node.test.ts @@ -1,8 +1,9 @@ import fs from 'node:fs' -import { execSync } from 'node:child_process' +import { inject } from 'vitest' import { createTeardown } from 'fs-teardown' import { fromTemp } from '../support/utils' -import * as packageJson from '../../package.json' + +const tarballPath = inject('tarballPath') const fsMock = createTeardown({ rootDir: fromTemp('worker-script-auto-update'), @@ -38,15 +39,8 @@ describe( }), }) - // Pack the current state of the "msw" package. - execSync(`pnpm pack --pack-destination ${fsMock.resolve('.')}`, { - stdio: [null, null, 'inherit'], - }) - // Install "msw" from the tarball into the dummy project. - const installCommand = await fsMock.exec( - `npm install msw-${packageJson.version}.tgz`, - ) + const installCommand = await fsMock.exec(`npm install ${tarballPath}`) expect(installCommand.stderr).toBe('') // Asset the worker script has been created/updated. @@ -65,13 +59,7 @@ describe( }), }) - execSync(`pnpm pack --pack-destination ${fsMock.resolve('.')}`, { - stdio: [null, null, 'inherit'], - }) - - const installCommand = await fsMock.exec( - `npm install msw-${packageJson.version}.tgz`, - ) + const installCommand = await fsMock.exec(`npm install ${tarballPath}`) /** * @note Cannot assert on the empty stderr because npm * writes to stderr if there's a new version of npm available. diff --git a/test/node/msw-api/cli/init.node.test.ts b/test/e2e/cli-init.node.test.ts similarity index 97% rename from test/node/msw-api/cli/init.node.test.ts rename to test/e2e/cli-init.node.test.ts index de8065945..b19b9d50c 100644 --- a/test/node/msw-api/cli/init.node.test.ts +++ b/test/e2e/cli-init.node.test.ts @@ -1,14 +1,14 @@ -// @vitest-environment node -import fs from 'fs' +import fs from 'node:fs' import path from 'node:path' +import { spawnSync } from 'node:child_process' import { createTeardown } from 'fs-teardown' -import { fromTemp } from '../../../support/utils' +import { fromTemp } from '../support/utils' const fsMock = createTeardown({ rootDir: fromTemp('cli/init'), }) -const cliPath = require.resolve('../../../../cli/index.js') +const cliPath = require.resolve('../../cli/index.js') function readJson(filePath: string) { const rawContent = fs.readFileSync(filePath, 'utf8') @@ -21,6 +21,7 @@ function readJson(filePath: string) { } beforeAll(async () => { + spawnSync('pnpm', ['build']) await fsMock.prepare() }) @@ -202,7 +203,7 @@ test('throws if creating a directory under path failed', async () => { * @note Require the "init" command source * so that the "fs" mocks could apply. */ - const init = require('../../../../cli/init') + const init = require('../../cli/init') // Mock the "mkdir" method throwing an error. const error = new Error('Failed to create directory') @@ -333,7 +334,7 @@ test('prints the list of failed paths to copy', async () => { } }) - const init = require('../../../../cli/init') + const init = require('../../cli/init') const copyFileError = new Error('Failed to copy file') const consoleLogSpy = vi diff --git a/test/e2e/tsconfig.json b/test/e2e/tsconfig.json index 2afc6e588..54a5f4098 100644 --- a/test/e2e/tsconfig.json +++ b/test/e2e/tsconfig.json @@ -1,6 +1,6 @@ { "extends": "../../tsconfig.base.json", - "include": ["./**/*.test.ts"], + "include": ["./**/*.test.ts", "./vitest.d.ts", "./vitest.global.setup.ts"], "compilerOptions": { "types": ["node", "vitest/globals"], "resolveJsonModule": true, diff --git a/test/e2e/vitest.config.mts b/test/e2e/vitest.config.mts index f1c19c102..6274e76df 100644 --- a/test/e2e/vitest.config.mts +++ b/test/e2e/vitest.config.mts @@ -1,22 +1,10 @@ import { defineConfig } from 'vitest/config' -import { mswExports, customViteEnvironments } from '../support/alias' export default defineConfig({ test: { dir: './test/e2e', globals: true, environment: 'node', - poolOptions: { - threads: { - /** - * @note Run Node.js integration tests in sequence. - * There's a test that involves building the library, - * which results in the "lib" directory being deleted. - * If any tests attempt to run during that window, - * they will fail, unable to resolve the "msw" import alias. - */ - singleThread: true, - }, - }, + globalSetup: './test/e2e/vitest.global.setup.ts', }, }) diff --git a/test/e2e/vitest.d.ts b/test/e2e/vitest.d.ts new file mode 100644 index 000000000..5bc8c41e9 --- /dev/null +++ b/test/e2e/vitest.d.ts @@ -0,0 +1,7 @@ +declare module 'vitest' { + export interface ProvidedContext { + tarballPath: string + } +} + +export {} diff --git a/test/e2e/vitest.global.setup.ts b/test/e2e/vitest.global.setup.ts new file mode 100644 index 000000000..bc573cf3c --- /dev/null +++ b/test/e2e/vitest.global.setup.ts @@ -0,0 +1,31 @@ +import fs from 'node:fs' +import { fileURLToPath } from 'node:url' +import { spawnSync } from 'node:child_process' +import { invariant } from 'outvariant' +import type { GlobalSetupContext } from 'vitest/node' +import * as packageJson from '../../package.json' + +export default function setup({ provide }: GlobalSetupContext) { + const tarballPath = fileURLToPath( + new URL(`../../msw-${packageJson.version}.tgz`, import.meta.url), + ) + + if (fs.existsSync(tarballPath)) { + return + } + + // Pack the library before all E2E tests. + spawnSync('pnpm', ['pack'], { + stdio: 'inherit', + }) + + invariant( + fs.existsSync(tarballPath), + 'Failed to set up e2e tests: library tarball not found at "%s"', + tarballPath, + ) + + console.log('Library built at "%s"!', tarballPath) + + provide('tarballPath', tarballPath) +}