2022-11-10 14:55:47 +00:00
|
|
|
import { resolve } from 'pathe'
|
|
|
|
import { distDir } from './dirs'
|
|
|
|
|
2022-03-16 12:34:27 +00:00
|
|
|
export interface RunTestOptions {
|
|
|
|
rootDir: string,
|
2022-03-17 21:31:06 +00:00
|
|
|
dev?: boolean,
|
|
|
|
watch?: boolean
|
2022-03-16 12:34:27 +00:00
|
|
|
runner?: 'vitest'
|
2022-11-10 14:55:47 +00:00
|
|
|
globalSetup?: boolean
|
2022-03-16 12:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const RunTestDefaults: Partial<RunTestOptions> = {
|
2022-11-10 14:55:47 +00:00
|
|
|
runner: 'vitest',
|
|
|
|
globalSetup: true
|
2022-03-16 12:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function runTests (opts: RunTestOptions) {
|
|
|
|
opts = { ...RunTestDefaults, ...opts }
|
|
|
|
|
|
|
|
if (opts.runner !== 'vitest') {
|
|
|
|
throw new Error(`Unsupported runner: ${opts.runner}. Currently only vitest runner is supported.`)
|
|
|
|
}
|
2022-03-17 21:31:06 +00:00
|
|
|
|
|
|
|
if (opts.dev) {
|
|
|
|
// Set default dev option for @nuxt/test-utils
|
|
|
|
process.env.NUXT_TEST_DEV = 'true'
|
|
|
|
}
|
|
|
|
|
2022-11-10 14:55:47 +00:00
|
|
|
// Consumed by recoverContextFromEnv()
|
|
|
|
process.env.NUXT_TEST_OPTIONS = JSON.stringify(opts)
|
|
|
|
|
2022-11-10 09:27:56 +00:00
|
|
|
const { startVitest } = await import('vitest/node')
|
2022-10-19 13:10:51 +00:00
|
|
|
const succeeded = await startVitest(
|
|
|
|
'test',
|
2022-03-16 12:34:27 +00:00
|
|
|
[] /* argv */,
|
|
|
|
// Vitest options
|
|
|
|
{
|
|
|
|
root: opts.rootDir,
|
2022-09-21 10:20:29 +00:00
|
|
|
run: !opts.watch,
|
|
|
|
deps: {
|
|
|
|
inline: [/@nuxt\/test-utils/]
|
|
|
|
}
|
2022-03-16 12:34:27 +00:00
|
|
|
},
|
|
|
|
// Vite options
|
|
|
|
{
|
|
|
|
esbuild: {
|
|
|
|
tsconfigRaw: '{}'
|
2022-11-10 14:55:47 +00:00
|
|
|
},
|
|
|
|
test: {
|
|
|
|
dir: opts.rootDir,
|
|
|
|
deps: {
|
|
|
|
inline: [
|
|
|
|
distDir,
|
|
|
|
'@nuxt/test-utils',
|
|
|
|
'@nuxt/test-utils-edge'
|
|
|
|
]
|
|
|
|
},
|
|
|
|
globals: true,
|
|
|
|
globalSetup: [
|
|
|
|
...opts.globalSetup ? [resolve(distDir, './runtime/global-setup')] : []
|
|
|
|
]
|
2022-03-16 12:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-19 13:10:51 +00:00
|
|
|
)
|
2022-03-16 12:34:27 +00:00
|
|
|
|
|
|
|
if (!succeeded) {
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|