A fixture named `task` breaks `expect` from the test context

Open Beginner friendly
#11,929 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@hrishikeshpathak1009 is already working on this.

Since Nov 10, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript, vite
Domain
testing

Research direction

Start in packages/vitest/src/runtime/runners/test.ts, in extendTaskContext, where the expect and bench getters are described. Reproduce the fixture collision with the supplied Vitest example, then inspect the context setup to identify the test instance it was created for. Done means the getters use that test instance and the regression cases for task, expect, and bench behave correctly.

Written by the indexing model from the issue text.

Description

p2-edge-case

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.soft passes
  • toMatchInlineSnapshot throws TypeError: 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.

Dominant language
TypeScript
Stars
17.4k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
148

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vitest-dev/vitest

All issues in vitest-dev/vitest

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.