Skip to content

Commit

Permalink
feat(runner): describe/test name support anonymous function (#3562)
Browse files Browse the repository at this point in the history
  • Loading branch information
btea authored Jun 15, 2023
1 parent bc49bac commit 3d43638
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function createTest(fn: (
}

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

function formatTitle(template: string, items: any[], idx: number) {
Expand Down
12 changes: 12 additions & 0 deletions test/reporters/fixtures/function-as-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ describe(Bar, () => {
})
})

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

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

describe.each([1])(foo, () => {
test.each([1])(foo, () => {
expect(0).toBe(0)
Expand Down
2 changes: 2 additions & 0 deletions test/reporters/tests/function-as-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ test('should print function name', async () => {
expect(stdout).toBeTruthy()
expect(stdout).toContain('function-as-name.test.ts > foo > Bar')
expect(stdout).toContain('function-as-name.test.ts > Bar > foo')
expect(stdout).toContain('function-as-name.test.ts > <anonymous> > foo')
expect(stdout).toContain('function-as-name.test.ts > foo > <anonymous>')
expect(stdout).toContain('function-as-name.test.ts > foo > foo')
expect(stdout).toContain('function-as-name.test.ts > Bar > Bar')
})

0 comments on commit 3d43638

Please sign in to comment.