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

fix: find tests if globals are enabled in vitest.experimentalStaticAstCollect #541

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions src/worker/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export function astParseFile(filepath: string, code: string) {
return getName(callee.tag)
}
if (callee.type === 'MemberExpression') {
if (
callee.object?.type === 'Identifier'
&& ['it', 'test', 'describe', 'suite'].includes(callee.object.name)
) {
return callee.object?.name
}
// direct call as `__vite_ssr_exports_0__.test()`
if (callee.object?.name?.startsWith('__vite_ssr_')) {
return getName(callee.property)
Expand Down
63 changes: 63 additions & 0 deletions test-e2e/discovery.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { resolve } from 'node:path'
import type { RunnerTestCase, RunnerTestSuite } from 'vitest'
import { describe, expect, it } from 'vitest'
import { createVitest } from 'vitest/node'
import { astCollectTests } from '../src/worker/collect'

describe('can discover tests', () => {
it.for([
'todo-import-suite.ts',
'todo-globals-suite.ts',
])('can discover todo tests inside a suite in %s', async (fixture) => {
const vitest = await createVitest('test', { config: false })
const file = await astCollectTests(
vitest.getCoreWorkspaceProject(),
resolve(`test-e2e/fixtures/collect/${fixture}`),
'web',
)
expect(file.filepath).toBe(resolve(`test-e2e/fixtures/collect/${fixture}`))
expect(file.name).toBe(`test-e2e/fixtures/collect/${fixture}`)

expect(file.tasks).toHaveLength(1)
const suite = file.tasks[0] as RunnerTestSuite
expect(suite.name).toBe('TicketDetailBottomBar')
expect(suite.mode).toBe('run')
expect(suite.location).toMatchObject({
line: 3,
column: 0,
})
expect(suite.tasks).toHaveLength(2)

const [testTask, suiteTask] = suite.tasks as [RunnerTestCase, RunnerTestSuite]
expect(testTask.name).toBe('emits %s event when button is clicked')
expect((testTask as any).dynamic).toBe(true)
expect(testTask.mode).toBe('run')
expect(testTask.location).toMatchObject({
line: 4,
column: 31, // TODO: should it be 5 instead?
})

expect(suiteTask.name).toBe('Drafts')
expect(suiteTask.mode).toBe('skip')
expect(suiteTask.location).toMatchObject({
line: 10,
column: 2,
})
expect(suiteTask.tasks).toHaveLength(2)

const [todo1, todo2] = suiteTask.tasks as RunnerTestCase[]
expect(todo1.name).toBe('should not display draft information if ticket has no draft')
expect(todo1.mode).toBe('todo')
expect(todo1.location).toMatchObject({
line: 11,
column: 4,
})

expect(todo2.name).toBe('should display draft information if ticket has a draft')
expect(todo2.mode).toBe('todo')
expect(todo2.location).toMatchObject({
line: 12,
column: 4,
})
})
})
14 changes: 14 additions & 0 deletions test-e2e/fixtures/collect/todo-globals-suite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @ts-nocheck

describe('TicketDetailBottomBar', () => {
it.each(['submit', 'discard'])(
'emits %s event when button is clicked',
async (eventName) => {
},
)

describe('Drafts', () => {
it.todo('should not display draft information if ticket has no draft')
it.todo('should display draft information if ticket has a draft')
})
})
14 changes: 14 additions & 0 deletions test-e2e/fixtures/collect/todo-import-suite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, it } from 'vitest'

describe('TicketDetailBottomBar', () => {
it.each(['submit', 'discard'])(
'emits %s event when button is clicked',
async (eventName) => {
},
)

describe('Drafts', () => {
it.todo('should not display draft information if ticket has no draft')
it.todo('should display draft information if ticket has a draft')
})
})
Loading