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 1 commit
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
Next Next commit
feat: support using function as describe/test name
  • Loading branch information
fenghan34 committed Jun 2, 2023
commit 49be42dc94ca23b765aba435fb560a28048eddee
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
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.*'],
},
})