Skip to content

fix(core): surface testPath with OS-native separators#1467

Open
fi3ework wants to merge 1 commit into
mainfrom
fix-testpath-native-path
Open

fix(core): surface testPath with OS-native separators#1467
fi3ework wants to merge 1 commit into
mainfrom
fix-testpath-native-path

Conversation

@fi3ework

@fi3ework fi3ework commented Jun 23, 2026

Copy link
Copy Markdown
Member

Summary

expect.getState().testPath and the lifecycle hook ctx.filepath are real on-disk paths surfaced to users. They were emitted in POSIX form (forward slashes), so on Windows they diverged from import.meta.filename / __filename (which the rspack plugin injects in OS-native form) and from any path a user builds with node:path. A user-side comparison like expect.getState().testPath === import.meta.filename therefore failed on Windows. A testPath is a real filesystem path, so blanket-forcing POSIX onto it is semantically wrong — these user-facing surfaces should carry OS-native separators.

Internally testPath must stay POSIX: the snapshot client, the reporter relative paths, and the related-graph lookup all run it through pathe, which only understands /. So the conversion happens only at the two user-facing boundaries — the setState testPath and the hook context filepath — via a small toNativePath helper, leaving every internal consumer untouched. The change is a no-op on POSIX platforms (native separator is already /).

Related Links

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e2176e67bc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/core/src/utils/testFiles.ts Outdated
return [formatTestEntryName(relativePath), entry];
// The entry name (key) stays slash-free; the value is the runtime
// `testPath`, surfaced to users as an OS-native path (#1465).
return [formatTestEntryName(relativePath), toNativePath(entry)];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep __dirname aligned with native __filename

On Windows when the CommonJS/non-output.module loader is used, returning a native testPath here makes loadModule inject __filename with backslashes, but it still derives __dirname with pathe.dirname(testPath) in packages/core/src/runtime/worker/loadModule.ts, which normalizes back to C:/.... That breaks the normal Node invariant that __dirname === node:path.dirname(__filename) for CJS tests once this native path flows through; update the dirname injection to use native path handling when testPath is native.

Useful? React with 👍 / 👎.

@fi3ework fi3ework force-pushed the fix-testpath-native-path branch 2 times, most recently from 9891a16 to 7877a1f Compare June 24, 2026 03:46

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7877a1f03d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

// `expect.getState().testPath` is user-facing; expose the OS-native
// path (equal to `import.meta.filename`). Internal consumers
// (snapshot, reporter, related) keep the POSIX `test.testPath` (#1465).
testPath: toNativePath(test.testPath),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Normalize all expect test paths

This only pins the global expect state to the native path for the duration of a test. When a test uses the public per-test context.expect, createExpect immediately calls freshExpectState, whose getter still returns workerState.testPath in the internal POSIX form, so on Windows context.expect.getState().testPath remains forward-slashed while the global expect.getState().testPath matches import.meta.filename; the same getter is also what top-level or pre-test reads use before this line runs.

Useful? React with 👍 / 👎.

`expect.getState().testPath` and the lifecycle hook `ctx.filepath` are real
on-disk paths surfaced to users, but they were emitted in POSIX form (forward
slashes), so on Windows they diverged from `import.meta.filename` / `__filename`
(which the rspack plugin injects in native form). A user comparing them — e.g.
`expect.getState().testPath === import.meta.filename` — failed on Windows.

Internally `testPath` must stay POSIX: the snapshot client, reporter relative
paths, and the related-graph lookup all run it through `pathe`, which only
understands `/`. So convert to native separators only at the two user-facing
boundaries (the `setState` testPath and the hook context `filepath`) via a
small `toNativePath` helper, leaving every internal consumer untouched.

Closes #1465
@fi3ework fi3ework force-pushed the fix-testpath-native-path branch from 7877a1f to f33a39b Compare June 24, 2026 04:10

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f33a39be7e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +74 to +75
export const toNativePath = (filePath: string): string =>
nativeNormalize(filePath);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve native paths in browser runtime

When browser mode runs on Windows, this helper is bundled through @rstest/core/internal/browser-runtime (browserRuntime.ts -> runtime/api -> runner.ts), and that web build uses pluginNodePolyfill() in packages/core/rslib.config.ts, which maps node:path to path-browserify; its normalize() is POSIX-only and leaves C:/... unchanged. The browser client also builds workerState.testPath with pathe.normalize, so expect.getState().testPath/ctx.filepath in browser tests will still be forward-slashed while the Rstest plugin injects native __filename/import.meta.filename, leaving the Windows browser-mode case unfixed.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: expect.getState().testPath returns POSIX-style path on Windows

1 participant