fix(test): allow test functions to override reported location#32732
Merged
bartlomieju merged 2 commits intodenoland:mainfrom Mar 17, 2026
Merged
fix(test): allow test functions to override reported location#32732bartlomieju merged 2 commits intodenoland:mainfrom
bartlomieju merged 2 commits intodenoland:mainfrom
Conversation
…bol.for("Deno.test.location")
Root cause: `Deno.test()` always records the call site of `Deno.test()`
itself as the test location. When BDD-style helper libraries (e.g.
`@std/testing/bdd`) call `Deno.test()` internally on behalf of the
user, the recorded location points to the library file rather than the
user's source file. This causes the JUnit reporter (and others) to group
tests under the library URL (e.g. `_test_suite.ts`) instead of the
user's file (fixes denoland#20047).
Fix: Before falling back to `core.currentUserCallSite()`, `testInner`
now checks whether the test function carries a
`Symbol.for("Deno.test.location")` property. If it does, that string
(in `"fileName:lineNumber:columnNumber"` format) is used as the test
location instead. Parsing is done from the right so that URL schemes
that contain `:` (such as `file://` and `https://`) are handled
correctly.
A follow-up change to `@std/testing/bdd` will set this symbol when
registering tests so that the JUnit reporter shows the user's test file.
- Type-check that the symbol value is a string
- Validate colon positions exist (not -1 or 0)
- Check parsed numbers aren't NaN
- Return null on any invalid input, falling back to currentUserCallSite()
- Use NumberIsNaN primordial instead of Number.isNaN
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #20047.
In
cli/js/40_test.js,testInnernow checks whether the registered test function carries aSymbol.for("Deno.test.location")property before falling back tocore.currentUserCallSite(). If the symbol is set, its string value (format:"fileName:lineNumber:columnNumber") is parsed and used as the test location. This allows test-helper libraries like@std/testing/bddto declare the user's source location instead of the library's own call site.Root Cause
In
cli/js/40_test.js:302(before this fix):core.currentUserCallSite()captures the stack frame whereDeno.test()is called. When a BDD library such as@std/testing/bddwraps user tests and callsDeno.test()internally, the captured location is inside the library file (e.g.https://jsr.io/@std/testing/.../bdd.ts), not the user's test file.The JUnit reporter uses
description.location.file_nameto both name the<testsuite>element and populate each<testcase classname="...">. With the wrong location, every test registered viabdd.tsis grouped under the library URL:This breaks CI test-reporting tools (e.g.
dorny/test-reporter,EnricoMi/publish-unit-test-result-action) that rely on the suite name to map results back to source files.Solution
Before reading
core.currentUserCallSite(), check for a well-known symbol on the test function:parseTestLocationsplits"fileName:lineNumber:columnNumber"from the right, so URL schemes containing:(e.g.file:///…,https://…) are handled correctly.A follow-up change to
@std/testing/bdd(in thestdrepo) will setSymbol.for("Deno.test.location")on each wrapped test function to point at the user's source file, completing the fix end-to-end.Testing
tests/specs/test/junit_location_override/— a new spec test that simulates a library-registered test (by settingSymbol.for("Deno.test.location")on the function) alongside an ordinary test, and asserts that the JUnit XML groups them into separate<testsuite>elements with the correct names and line/column numbers.Checklist
StringPrototypeLastIndexOf,StringPrototypeSlice,SymbolFor)