feat: cli name filter

This commit is contained in:
Anthony Fu 2021-12-05 02:22:58 +08:00
parent be710234a1
commit be060c1c25
5 changed files with 28 additions and 15 deletions

View File

@ -28,8 +28,10 @@ const testOptions = viteConfig.test || {}
await run({
...testOptions,
server,
updateSnapshot: argv.update,
rootDir: argv.root || process.cwd(),
nameFilters: argv._,
})
function help() {

View File

@ -8,4 +8,4 @@ export * from './chai'
export { beforeAll, afterAll, beforeEach, afterEach, beforeFile, afterFile, beforeSuite, afterSuite } from './hooks'
export { sinon }
export const { mock, spy } = sinon
export const { mock, spy, stub } = sinon

View File

@ -3,7 +3,7 @@ import fg from 'fast-glob'
import SinonChai from 'sinon-chai'
import { clearContext, defaultSuite } from './suite'
import { context } from './context'
import { File, Options, Task, Reporter, RunnerContext } from './types'
import { File, Config, Task, Reporter, RunnerContext } from './types'
import { afterEachHook, afterFileHook, afterAllHook, afterSuiteHook, beforeEachHook, beforeFileHook, beforeAllHook, beforeSuiteHook } from './hooks'
import { SnapshotPlugin } from './snapshot'
import { DefaultReporter } from './reporters/default'
@ -89,26 +89,29 @@ export async function runFile(file: File, ctx: RunnerContext) {
await reporter.onFileEnd?.(file, ctx)
}
export async function run(options: Options = {}) {
const { rootDir = process.cwd() } = options
export async function run(config: Config) {
const { rootDir = process.cwd() } = config
// setup chai
chai.use(await SnapshotPlugin({
rootDir,
update: options.updateSnapshot,
update: config.updateSnapshot,
}))
chai.use(SinonChai)
// collect files
const paths = await fg(
options.includes || defaultIncludes,
let paths = await fg(
config.includes || defaultIncludes,
{
absolute: true,
cwd: options.rootDir,
ignore: options.excludes || defaultExcludes,
cwd: config.rootDir,
ignore: config.excludes || defaultExcludes,
},
)
if (config.nameFilters?.length)
paths = paths.filter(i => config.nameFilters!.some(f => i.includes(f)))
if (!paths.length) {
console.error('No test files found')
process.exitCode = 1
@ -117,14 +120,14 @@ export async function run(options: Options = {}) {
const reporter: Reporter = new DefaultReporter()
await reporter.onStart?.(options)
await reporter.onStart?.(config)
const files = await collectFiles(paths)
const ctx: RunnerContext = {
files,
mode: isOnlyMode(files) ? 'only' : 'all',
userOptions: options,
config,
reporter,
}

View File

@ -1,4 +1,5 @@
/* eslint-disable no-use-before-define */
import { ViteDevServer } from 'vite'
export type Awaitable<T> = Promise<T> | T
@ -7,9 +8,16 @@ export interface UserOptions {
excludes?: string[]
}
export interface Options extends UserOptions {
export interface Config extends UserOptions {
rootDir?: string
updateSnapshot?: boolean
nameFilters?: string[]
// Internal
server: ViteDevServer
// TODO:
watch?: boolean
}
export type RunMode = 'run' | 'skip' | 'only' | 'todo'
@ -61,7 +69,7 @@ export interface File {
export interface RunnerContext {
files: File[]
mode: 'all' | 'only'
userOptions: Options
config: Config
reporter: Reporter
}
@ -71,7 +79,7 @@ export interface GlobalContext {
}
export interface Reporter {
onStart: (userOptions: Options) => Awaitable<void>
onStart: (userOptions: Config) => Awaitable<void>
onCollected: (ctx: RunnerContext) => Awaitable<void>
onFinished: (ctx: RunnerContext) => Awaitable<void>

View File

@ -22,6 +22,6 @@ test('async', async() => {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 1000)
}, 200)
})
})