2022-03-16 12:34:27 +00:00
|
|
|
import { resolve } from 'pathe'
|
|
|
|
import { defineNuxtCommand } from './index'
|
|
|
|
|
|
|
|
export default defineNuxtCommand({
|
|
|
|
meta: {
|
|
|
|
name: 'test',
|
2022-03-17 21:31:06 +00:00
|
|
|
usage: 'npx nuxi test [--dev] [--watch] [rootDir]',
|
2022-03-16 12:34:27 +00:00
|
|
|
description: 'Run tests'
|
|
|
|
},
|
|
|
|
async invoke (args) {
|
|
|
|
process.env.NODE_ENV = process.env.NODE_ENV || 'test'
|
|
|
|
const rootDir = resolve(args._[0] || '.')
|
|
|
|
const { runTests } = await importTestUtils()
|
|
|
|
await runTests({
|
2022-03-17 21:31:06 +00:00
|
|
|
rootDir,
|
|
|
|
dev: !!args.dev,
|
|
|
|
watch: !!args.watch
|
2022-03-16 12:34:27 +00:00
|
|
|
})
|
2022-04-09 10:02:56 +00:00
|
|
|
|
|
|
|
if (args.watch) {
|
2022-04-29 09:38:22 +00:00
|
|
|
return 'wait' as const
|
2022-04-09 10:02:56 +00:00
|
|
|
}
|
2022-03-16 12:34:27 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
async function importTestUtils (): Promise<typeof import('@nuxt/test-utils')> {
|
|
|
|
let err
|
|
|
|
for (const pkg of ['@nuxt/test-utils-edge', '@nuxt/test-utils']) {
|
|
|
|
try {
|
|
|
|
const exports = await import(pkg)
|
|
|
|
// Detect old @nuxt/test-utils
|
|
|
|
if (!exports.runTests) {
|
|
|
|
throw new Error('Invalid version of `@nuxt/test-utils` is installed!')
|
|
|
|
}
|
|
|
|
return exports
|
2022-11-10 14:55:47 +00:00
|
|
|
} catch (_err) {
|
|
|
|
err = _err
|
|
|
|
}
|
2022-03-16 12:34:27 +00:00
|
|
|
}
|
|
|
|
console.error(err)
|
|
|
|
throw new Error('`@nuxt/test-utils-edge` seems missing. Run `npm i -D @nuxt/test-utils-edge` or `yarn add -D @nuxt/test-utils-edge` to install.')
|
|
|
|
}
|