Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support using function/class as describe/test name #3497

Merged
merged 6 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions docs/api/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ In Jest, `TestFunction` can also be of type `(done: DoneCallback) => void`. If t

## test

- **Type:** `(name: string, fn: TestFunction, timeout?: number | TestOptions) => void`
- **Type:** `(name: string | Function, fn: TestFunction, timeout?: number | TestOptions) => void`
- **Alias:** `it`

`test` defines a set of related expectations. It receives the test name and a function that holds the expectations to test.
Expand All @@ -53,7 +53,7 @@ In Jest, `TestFunction` can also be of type `(done: DoneCallback) => void`. If t

### test.skip

- **Type:** `(name: string, fn: TestFunction, timeout?: number | TestOptions) => void`
- **Type:** `(name: string | Function, fn: TestFunction, timeout?: number | TestOptions) => void`
- **Alias:** `it.skip`

If you want to skip running certain tests, but you don't want to delete the code due to any reason, you can use `test.skip` to avoid running them.
Expand Down Expand Up @@ -111,7 +111,7 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t

### test.only

- **Type:** `(name: string, fn: TestFunction, timeout?: number) => void`
- **Type:** `(name: string | Function, fn: TestFunction, timeout?: number) => void`
- **Alias:** `it.only`

Use `test.only` to only run certain tests in a given suite. This is useful when debugging.
Expand All @@ -136,7 +136,7 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t

### test.concurrent

- **Type:** `(name: string, fn: TestFunction, timeout?: number) => void`
- **Type:** `(name: string | Function, fn: TestFunction, timeout?: number) => void`
- **Alias:** `it.concurrent`

`test.concurrent` marks consecutive tests to be run in parallel. It receives the test name, an async function with the tests to collect, and an optional timeout (in milliseconds).
Expand Down Expand Up @@ -179,7 +179,7 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t

### test.todo

- **Type:** `(name: string) => void`
- **Type:** `(name: string | Function) => void`
- **Alias:** `it.todo`

Use `test.todo` to stub tests to be implemented later. An entry will be shown in the report for the tests so you know how many tests you still need to implement.
Expand All @@ -191,7 +191,7 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t

### test.fails

- **Type:** `(name: string, fn: TestFunction, timeout?: number) => void`
- **Type:** `(name: string | Function, fn: TestFunction, timeout?: number) => void`
- **Alias:** `it.fails`

Use `test.fails` to indicate that an assertion will fail explicitly.
Expand Down Expand Up @@ -502,7 +502,7 @@ When you use `test` or `bench` in the top level of file, they are collected as p

### describe.skip

- **Type:** `(name: string, fn: TestFunction, options?: number | TestOptions) => void`
- **Type:** `(name: string | Function, fn: TestFunction, options?: number | TestOptions) => void`

Use `describe.skip` in a suite to avoid running a particular describe block.

Expand Down Expand Up @@ -539,7 +539,7 @@ You cannot use this syntax when using Vitest as [type checker](/guide/testing-ty

### describe.only

- **Type:** `(name: string, fn: TestFunction, options?: number | TestOptions) => void`
- **Type:** `(name: string | Function, fn: TestFunction, options?: number | TestOptions) => void`

Use `describe.only` to only run certain suites

Expand All @@ -565,7 +565,7 @@ You cannot use this syntax when using Vitest as [type checker](/guide/testing-ty

### describe.concurrent

- **Type:** `(name: string, fn: TestFunction, options?: number | TestOptions) => void`
- **Type:** `(name: string | Function, fn: TestFunction, options?: number | TestOptions) => void`

`describe.concurrent` in a suite marks every tests as concurrent

Expand Down Expand Up @@ -606,7 +606,7 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t

### describe.shuffle

- **Type:** `(name: string, fn: TestFunction, options?: number | TestOptions) => void`
- **Type:** `(name: string | Function, fn: TestFunction, options?: number | TestOptions) => void`

Vitest provides a way to run all tests in random order via CLI flag [`--sequence.shuffle`](/guide/cli) or config option [`sequence.shuffle`](/config/#sequence-shuffle), but if you want to have only part of your test suite to run tests in random order, you can mark it with this flag.

Expand All @@ -627,7 +627,7 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t

### describe.todo

- **Type:** `(name: string) => void`
- **Type:** `(name: string | Function) => void`

Use `describe.todo` to stub suites to be implemented later. An entry will be shown in the report for the tests so you know how many tests you still need to implement.

Expand All @@ -638,7 +638,7 @@ You cannot use this syntax, when using Vitest as [type checker](/guide/testing-t

### describe.each

- **Type:** `(cases: ReadonlyArray<T>, ...args: any[]): (name: string, fn: (...args: T[]) => void, options?: number | TestOptions) => void`
- **Type:** `(cases: ReadonlyArray<T>, ...args: any[]): (name: string | Function, fn: (...args: T[]) => void, options?: number | TestOptions) => void`

Use `describe.each` if you have more than one test that depends on the same data.

Expand Down
30 changes: 18 additions & 12 deletions packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { getHooks, setFn, setHooks } from './map'
// apis
export const suite = createSuite()
export const test = createTest(
function (name: string, fn?: TestFunction, options?: number | TestOptions) {
getCurrentSuite().test.fn.call(this, name, fn, options)
function (name: string | Function, fn?: TestFunction, options?: number | TestOptions) {
getCurrentSuite().test.fn.call(this, formatName(name), fn, options)
},
)

Expand Down Expand Up @@ -59,7 +59,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m

initSuite()

const test = createTest(function (name: string, fn = noop, options) {
const test = createTest(function (name: string | Function, fn = noop, options) {
const mode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'

if (typeof options === 'number')
Expand All @@ -78,7 +78,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
const test: Test = {
id: '',
type: 'test',
name,
name: formatName(name),
each: this.each,
mode,
suite: undefined!,
Expand Down Expand Up @@ -189,7 +189,7 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
}

function createSuite() {
function suiteFn(this: Record<string, boolean | undefined>, name: string, factory?: SuiteFactory, options?: number | TestOptions) {
function suiteFn(this: Record<string, boolean | undefined>, name: string | Function, factory?: SuiteFactory, options?: number | TestOptions) {
const mode: RunMode = this.only ? 'only' : this.skip ? 'skip' : this.todo ? 'todo' : 'run'
const currentSuite = getCurrentSuite()

Expand All @@ -200,7 +200,7 @@ function createSuite() {
if (currentSuite?.options)
options = { ...currentSuite.options, ...options }

return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle, this.each, options)
return createSuiteCollector(formatName(name), factory, mode, this.concurrent, this.shuffle, this.each, options)
}

suiteFn.each = function<T>(this: { withContext: () => SuiteAPI; setContext: (key: string, value: boolean | undefined) => SuiteAPI }, cases: ReadonlyArray<T>, ...args: any[]) {
Expand All @@ -210,13 +210,14 @@ function createSuite() {
if (Array.isArray(cases) && args.length)
cases = formatTemplateString(cases, args)

return (name: string, fn: (...args: T[]) => void, options?: number | TestOptions) => {
return (name: string | Function, fn: (...args: T[]) => void, options?: number | TestOptions) => {
const _name = formatName(name)
const arrayOnlyCases = cases.every(Array.isArray)
cases.forEach((i, idx) => {
const items = Array.isArray(i) ? i : [i]
arrayOnlyCases
? suite(formatTitle(name, items, idx), () => fn(...items), options)
: suite(formatTitle(name, items, idx), () => fn(i), options)
? suite(formatTitle(_name, items, idx), () => fn(...items), options)
: suite(formatTitle(_name, items, idx), () => fn(i), options)
})

this.setContext('each', undefined)
Expand Down Expand Up @@ -249,14 +250,15 @@ function createTest(fn: (
if (Array.isArray(cases) && args.length)
cases = formatTemplateString(cases, args)

return (name: string, fn: (...args: T[]) => void, options?: number | TestOptions) => {
return (name: string | Function, fn: (...args: T[]) => void, options?: number | TestOptions) => {
const _name = formatName(name)
const arrayOnlyCases = cases.every(Array.isArray)
cases.forEach((i, idx) => {
const items = Array.isArray(i) ? i : [i]

arrayOnlyCases
? test(formatTitle(name, items, idx), () => fn(...items), options)
: test(formatTitle(name, items, idx), () => fn(i), options)
? test(formatTitle(_name, items, idx), () => fn(...items), options)
: test(formatTitle(_name, items, idx), () => fn(i), options)
})

this.setContext('each', undefined)
Expand All @@ -272,6 +274,10 @@ function createTest(fn: (
) as TestAPI
}

function formatName(name: string | Function) {
return typeof name === 'string' ? name : name instanceof Function ? name.name : String(name)
}

function formatTitle(template: string, items: any[], idx: number) {
if (template.includes('%#')) {
// '%#' match index of the test case
Expand Down
24 changes: 12 additions & 12 deletions packages/runner/src/types/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,49 +112,49 @@ type ExtractEachCallbackArgs<T extends ReadonlyArray<any>> = {

interface SuiteEachFunction {
<T extends any[] | [any]>(cases: ReadonlyArray<T>): (
name: string,
name: string | Function,
fn: (...args: T) => Awaitable<void>,
) => void
<T extends ReadonlyArray<any>>(cases: ReadonlyArray<T>): (
name: string,
name: string | Function,
fn: (...args: ExtractEachCallbackArgs<T>) => Awaitable<void>,
) => void
<T>(cases: ReadonlyArray<T>): (
name: string,
name: string | Function,
fn: (...args: T[]) => Awaitable<void>,
) => void
}

interface TestEachFunction {
<T extends any[] | [any]>(cases: ReadonlyArray<T>): (
name: string,
name: string | Function,
fn: (...args: T) => Awaitable<void>,
options?: number | TestOptions,
) => void
<T extends ReadonlyArray<any>>(cases: ReadonlyArray<T>): (
name: string,
name: string | Function,
fn: (...args: ExtractEachCallbackArgs<T>) => Awaitable<void>,
options?: number | TestOptions,
) => void
<T>(cases: ReadonlyArray<T>): (
name: string,
name: string | Function,
fn: (...args: T[]) => Awaitable<void>,
options?: number | TestOptions,
) => void
(...args: [TemplateStringsArray, ...any]): (
name: string,
name: string | Function,
fn: (...args: any[]) => Awaitable<void>,
options?: number | TestOptions,
) => void
}

type ChainableTestAPI<ExtraContext = {}> = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'fails',
[name: string, fn?: TestFunction<ExtraContext>, options?: number | TestOptions],
[name: string | Function, fn?: TestFunction<ExtraContext>, options?: number | TestOptions],
void,
{
each: TestEachFunction
<T extends ExtraContext>(name: string, fn?: TestFunction<T>, options?: number | TestOptions): void
<T extends ExtraContext>(name: string | Function, fn?: TestFunction<T>, options?: number | TestOptions): void
}
>

Expand Down Expand Up @@ -188,11 +188,11 @@ export type TestAPI<ExtraContext = {}> = ChainableTestAPI<ExtraContext> & {

type ChainableSuiteAPI<ExtraContext = {}> = ChainableFunction<
'concurrent' | 'only' | 'skip' | 'todo' | 'shuffle',
[name: string, factory?: SuiteFactory<ExtraContext>, options?: number | TestOptions],
[name: string | Function, factory?: SuiteFactory<ExtraContext>, options?: number | TestOptions],
SuiteCollector<ExtraContext>,
{
each: TestEachFunction
<T extends ExtraContext>(name: string, factory?: SuiteFactory<T>): SuiteCollector<T>
<T extends ExtraContext>(name: string | Function, factory?: SuiteFactory<T>): SuiteCollector<T>
}
>

Expand Down Expand Up @@ -226,7 +226,7 @@ export interface SuiteCollector<ExtraContext = {}> {
on: <T extends keyof SuiteHooks<ExtraContext>>(name: T, ...fn: SuiteHooks<ExtraContext>[T]) => void
}

export type SuiteFactory<ExtraContext = {}> = (test: (name: string, fn: TestFunction<ExtraContext>) => void) => Awaitable<void>
export type SuiteFactory<ExtraContext = {}> = (test: (name: string | Function, fn: TestFunction<ExtraContext>) => void) => Awaitable<void>

export interface RuntimeContext {
tasks: (SuiteCollector | Test)[]
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions test/describe-test-name/fixtures/test/example.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, expect, test } from 'vitest'

function foo() {}
class Bar {}

describe(foo, () => {
test(Bar, () => {
expect(0).toBe(1)
})
})

describe(Bar, () => {
test(foo, () => {
expect(0).toBe(1)
})
})

describe.each([1])(foo, () => {
test.each([1])(foo, () => {
expect(0).toBe(1)
})
})

describe.each([1])(Bar, () => {
test.each([1])(Bar, () => {
expect(0).toBe(1)
})
})
10 changes: 10 additions & 0 deletions test/describe-test-name/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@vitest/test-describe-test-name",
"private": true,
"scripts": {
"test": "vitest run"
},
"devDependencies": {
"vitest": "workspace:*"
}
}
14 changes: 14 additions & 0 deletions test/describe-test-name/test/runner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, test } from 'vitest'
import { resolve } from 'pathe'
import { runVitest } from '../../test-utils'

test('should print function name', async () => {
const filename = resolve('./fixtures/test/example.test.ts')
const { stderr } = await runVitest({ root: './fixtures' }, [filename])
fenghan34 marked this conversation as resolved.
Show resolved Hide resolved

expect(stderr).toBeTruthy()
expect(stderr).toContain('FAIL test/example.test.ts > foo > Bar')
expect(stderr).toContain('FAIL test/example.test.ts > Bar > foo')
expect(stderr).toContain('FAIL test/example.test.ts > foo > foo')
expect(stderr).toContain('FAIL test/example.test.ts > Bar > Bar')
})
7 changes: 7 additions & 0 deletions test/describe-test-name/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include: ['test/**/*.test.*'],
},
})