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

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

Closed
6 tasks done
yannbf opened this issue Aug 20, 2024 · 1 comment · Fixed by #6472
Closed
6 tasks done

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

yannbf opened this issue Aug 20, 2024 · 1 comment · Fixed by #6472
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@yannbf
Copy link
Contributor

yannbf commented Aug 20, 2024

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

@sheremet-va sheremet-va added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Aug 20, 2024
@yannbf
Copy link
Contributor Author

yannbf commented Aug 28, 2024

Additionally, it seems like expect.getState().testPath is not undefined when running tests with isolate: false, however the value is inconsistent and most of the times incorrect.

Repro:
https://stackblitz.com/edit/vitest-dev-vitest-qq8ndy?file=test%2Fbasic.test.ts,vitest.config.ts,test%2Fextratests%2Ffoo.test.ts,test%2Fextratests%2Fbar.test.ts,test%2Fextratests%2Fbaz.test.ts,test%2Fsuite.test.ts&initialPath=__vitest__/

How to repro:

  • Run tests in watch mode (pnpm run test:ui)
  • look at the logs succeeding. testPath is the value of expect.getState().testPath, vitestWorkerFilePath is the value of globalThis.__vitest_worker__.filepath and fileUrl is import.meta.url. _isRunningFromThisFile checks whether the paths match.
stdout | test/extratests/baz.test.ts
{
  testPath: undefined,
  vitestWorkerFilePath: '/home/projects/vitest-dev-vitest-qq8ndy/test/extratests/baz.test.ts',
  fileUrl: 'file:///home/projects/vitest-dev-vitest-qq8ndy/test/extratests/baz.test.ts',
  _isRunningFromThisFile: true
}
  • keep pressing R to rerun the tests until the tests fail, then look at the logs:
stdout | test/extratests/baz.test.ts
{
  testPath: '/home/projects/vitest-dev-vitest-qq8ndy/test/extratests/foo.test.ts',
  vitestWorkerFilePath: '/home/projects/vitest-dev-vitest-qq8ndy/test/extratests/baz.test.ts',
  fileUrl: 'file:///home/projects/vitest-dev-vitest-qq8ndy/test/extratests/baz.test.ts',
  _isRunningFromThisFile: false
}

Notice that the testPath (expect.getState().testPath) value here is test/extratests/foo.test.ts' while the actual file is test/extratests/baz.test.ts'

This is inconsistent, the values can change. Seems like it could be related to the order of which tests are running.
2024-08-28 13 18 12

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants