Skip to content

Custom Task Functions that call .extend have their wrapped handlers ignored #8117

@bravely

Description

@bravely

Describe the bug

Lets say you create a Custom Task Function with createTaskCollector, the same way as demonstrated both in the docs and in the original PR.

If you then want to add test fixtures via .extend, whatever you did to wrap the handler will not be used, instead using the original fn passed in.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-yw1snj5a?file=test%2Fsuite.test.ts

And just in case that link doesn't work, this is an example test file:

import { assert, describe, expect } from 'vitest'
import { createTaskCollector, getCurrentSuite } from 'vitest/suite';

let shouldBeTrueAfterTest: boolean = false;

const wrappedInLogs = createTaskCollector(function(
  this: object,
  name: string,
  fn: () => void,
  timeout: number
) {
  const handler = async () => {
    shouldBeTrueAfterTest = false;
    const ret = await fn();
    assert(shouldBeTrueAfterTest);
    return ret;
  }
  getCurrentSuite().task(name, {
    ...this,
    meta: {
      ignoreme: true,
    },
    handler,
    timeout
  })
})

describe('suite name', () => {
  wrappedInLogs('foo', () => {
    assert.equal(Math.sqrt(4), 2)
    shouldBeTrueAfterTest = true;
  })

  wrappedInLogs('bar', () => {
    expect(1 + 1).eq(2)
    shouldBeTrueAfterTest = true;
  })

  wrappedInLogs('snapshot', () => {
    expect({ foo: 'bar' }).toMatchSnapshot()
    shouldBeTrueAfterTest = true;
  })
})

type User = {
  id: number;
  email: string;
}

type UserContext = {
  user: User
}

const userTest = wrappedInLogs.extend({
  user: async ({}, use) => {
    await use({
      id: 1,
      email: 'foo@example.com'
    })
  }
})

describe('suite name', () => {
  userTest('foo', () => {
    assert.equal(Math.sqrt(4), 2)
  })

  userTest('bar', () => {
    expect(1 + 1).eq(2)
  })

  userTest('snapshot', () => {
    expect({ foo: 'bar' }).toMatchSnapshot()
  })
})

System Info

❯ npx envinfo --system --npmPackages '{vitest*,@vitest/*,vite,@vitejs/*,playwright,webdriverio}' --binaries --browser
Need to install the following packages:
envinfo@7.14.0
Ok to proceed? (y) y


  System:
    OS: macOS 15.5
    CPU: (12) arm64 Apple M3 Pro
    Memory: 134.72 MB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.13.0 - ~/.asdf/installs/nodejs/22.13.0/bin/node
    Yarn: 1.22.22 - ~/.asdf/installs/nodejs/22.13.0/bin/yarn
    npm: 10.9.2 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 9.12.2 - ~/.asdf/installs/nodejs/22.13.0/bin/pnpm


~ took 10s

Used Package Manager

yarn

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Projects

    Status

    Approved

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions