Skip to content

Commit

Permalink
chore: pack/build before all e2e tests (#2395)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Dec 15, 2024
1 parent 41f00e1 commit 417918f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 37 deletions.
22 changes: 5 additions & 17 deletions test/e2e/auto-update-worker.node.test.ts
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -21,6 +21,7 @@ function readJson(filePath: string) {
}

beforeAll(async () => {
spawnSync('pnpm', ['build'])
await fsMock.prepare()
})

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
14 changes: 1 addition & 13 deletions test/e2e/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -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',
},
})
7 changes: 7 additions & 0 deletions test/e2e/vitest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module 'vitest' {
export interface ProvidedContext {
tarballPath: string
}
}

export {}
31 changes: 31 additions & 0 deletions test/e2e/vitest.global.setup.ts
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 417918f

Please sign in to comment.