A fixture named `task` breaks `expect` from the test context
A fixture named task replaces context.task. The expect from the test context is created lazily from context.task, so after fixtures run it gets the fixture value instead of the test:
- a failed
expect.softpasses toMatchInlineSnapshotthrowsTypeError: Cannot read properties of undefined (reading 'filepath')
The imported expect is not affected.
Reproduction
Vitest 5.1.0:
import { test as baseTest } from 'vitest'
const test = baseTest.extend('task', () => ({ title: 'write docs', done: false }))
test('soft assertion fails', ({ task, expect }) => {
// passes
expect.soft(task.done).toBe(true)
})
test('inline snapshot passes', ({ task, expect }) => {
// TypeError: Cannot read properties of undefined (reading 'filepath')
expect(task.title).toMatchInlineSnapshot(`"write docs"`)
})
The expect and bench getters in extendTaskContext (packages/vitest/src/runtime/runners/test.ts) read context.task when they are first accessed. They should use the test that the context was created for.
Related: a fixture named expect or bench throws TypeError: Cannot set property expect of function() {...}, because both are getters without a setter. The types accept both fixtures.
I'd like to work on this issue.
I've looked into extendTaskContext() and understand that the lazy getters for expect and bench rely on context.task, which can be overwritten by a fixture.
I'd like to investigate a fix that preserves the original test reference and add regression tests covering fixtures named task, expect, and bench.
Could this issue please be labelled approved so I can submit a PR?
Looked into extendTaskContext in packages/vitest/src/runtime/runners/test.ts:
taskfixture overriding: Capturingconst task = context.taskin the outer function scope ensurescreateExpect(task)andcreateBench(task)bind to the originalTaskinstance regardless of whether a fixture later shadowscontext.task.expectandbenchfixtures: Markingexpectandbenchdescriptors withconfigurable: trueand a setter (or allowing assignment) resolves theTypeError: Cannot set property expectwhen users define fixtures with those names.
I have a branch with this fix and regression tests covering fixtures named task, expect, and bench. Happy to open the PR directly.