Skip to content

expect.getState().testPath only works when called inside of test function #6367

@yannbf

Description

@yannbf

Describe the bug

Based on this reproduction: https://stackblitz.com/edit/vitest-dev-vitest-w9khjv?file=test%2Fbasic.test.ts,test%2Fsuite.test.ts,vitest.config.ts&initialPath=__vitest__/

The following code:

import { assert, expect, test } from 'vitest';

console.log({ testPathOutside: expect.getState().testPath });
test('Math.sqrt()', () => {
  console.log({ testPathInside: expect.getState().testPath });
  // ...
});

yields:

stdout | test/basic.test.ts
{
  "testPathOutside": undefined,
}
stdout | test/basic.test.ts > Math.sqrt()
{
  "testPathInside": "/home/projects/vitest-dev-vitest-qq8ndy/test/basic.test.ts",
}

I'd expect that expect.getState().testPath would always work when invoked in the file, regardless of its position. Seems like it also yields undefined when called inside of describe calls.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-w9khjv?file=test%2Fbasic.test.ts,test%2Fsuite.test.ts,vitest.config.ts&initialPath=__vitest__/

System Info

-

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions