Skip to content

Commit

Permalink
feat: findRelatedTests flag, to run only changed tests (#363)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
sheremet-va and antfu committed Dec 29, 2021
1 parent 97abcb9 commit 2a271b5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/guide/index.md
Expand Up @@ -95,6 +95,7 @@ Run vitest in development mode.
| `--run` | Do not watch |
| `--global` | Inject APIs globally |
| `--dom` | Mock browser api with happy-dom |
| `--findRelatedTests <filepath>` | Run only tests that import specified file |
| `--environment <env>` | Runner environment (default: node) |
| `--passWithNoTests` | Pass when no tests found |
| `-h, --help` | Display available CLI options |
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/cli.ts
Expand Up @@ -27,6 +27,7 @@ cli
.option('--run', 'do not watch')
.option('--global', 'inject apis globally')
.option('--dom', 'mock browser api with happy-dom')
.option('--findRelatedTests <filepath>', 'run only tests that import specified file')
.option('--environment <env>', 'runner environment', { default: 'node' })
.option('--passWithNoTests', 'pass when no tests found')
.help()
Expand Down
5 changes: 4 additions & 1 deletion packages/vitest/src/node/config.ts
Expand Up @@ -3,7 +3,7 @@ import type { ResolvedConfig as ResolvedViteConfig } from 'vite'
import type { ResolvedConfig, UserConfig } from '../types'
import { defaultExclude, defaultInclude, defaultPort } from '../constants'
import { resolveC8Options } from '../coverage'
import { deepMerge } from '../utils'
import { deepMerge, toArray } from '../utils'

export function resolveConfig(
options: UserConfig,
Expand Down Expand Up @@ -68,5 +68,8 @@ export function resolveConfig(
if (resolved.api === true)
resolved.api = defaultPort

if (options.findRelatedTests)
resolved.findRelatedTests = toArray(options.findRelatedTests).map(file => resolve(resolved.root, file))

return resolved
}
7 changes: 7 additions & 0 deletions packages/vitest/src/runtime/collect.ts
Expand Up @@ -15,6 +15,10 @@ function hash(str: string, length = 10) {
.slice(0, length)
}

function inModuleGraph(files: string[]) {
return files.some(file => process.__vitest_worker__.moduleCache.has(file))
}

export async function collectTests(paths: string[], config: ResolvedConfig) {
const files: File[] = []

Expand All @@ -35,6 +39,9 @@ export async function collectTests(paths: string[], config: ResolvedConfig) {
await runSetupFiles(config)
await import(filepath)

if (config.findRelatedTests && !inModuleGraph(config.findRelatedTests))
continue

const defaultTasks = await defaultSuite.collect(file)

setHooks(file, getHooks(defaultTasks))
Expand Down
8 changes: 7 additions & 1 deletion packages/vitest/src/types/config.ts
Expand Up @@ -220,12 +220,18 @@ export interface UserConfig extends InlineConfig {
* Pass with no tests
*/
passWithNoTests?: boolean

/**
* Run tests that cover a list of source files
*/
findRelatedTests?: string[] | string
}

export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'filters' | 'coverage' | 'testNamePattern'> {
export interface ResolvedConfig extends Omit<Required<UserConfig>, 'config' | 'filters' | 'coverage' | 'testNamePattern' | 'findRelatedTests'> {
config?: string
filters?: string[]
testNamePattern?: RegExp
findRelatedTests?: string[]

depsInline: (string | RegExp)[]
depsExternal: (string | RegExp)[]
Expand Down

0 comments on commit 2a271b5

Please sign in to comment.