|
Is there a way to annotate something from test.extend(
"random",
async ({ task }) =>
{
const seed = crypto.randomInt(Number.MAX_SAFE_INTEGER);
return new Randomiser(seed);
},
);And would like to be able to see which seed was used somewhere in the reported result. Any ideas? Annotate from within the builder callback doesn't seem to do anything so I'm guessing that's not supported. |
Replies: 3 comments 2 replies
|
This is supported. export const randomTest = test.extend("random", async ({ annotate }) => {
const seed = crypto.randomInt(Number.MAX_SAFE_INTEGER)
await annotate(`Random seed: ${seed}`, "random-seed")
return new Randomiser(seed)
})Use One easily missed detail: the default terminal reporter only shows annotations for failed tests. Run with |
|
The fixture call can be valid, but the display surface is the catch here. What differs is reporting: the default terminal reporter only prints annotations for failed tests, and the HTML/UI path currently says annotations are only visible when the call was made from a test file. For a random seed that you always need to see, I would return the seed from the fixture and annotate from the test body or a per-test hook in the test file, or use a reporter that serializes annotations such as const randomTest = test.extend('random', async () => {
const seed = crypto.randomInt(Number.MAX_SAFE_INTEGER)
return { seed, value: new Randomiser(seed) }
})
randomTest('uses a random seed', async ({ random, annotate }) => {
await annotate(`seed=${random.seed}`, 'random-seed')
// use random.value here
})Docs note the reporter behavior here: https://vitest.dev/guide/test-annotations#built-in-reporters |
|
Annotating from the fixture does reach the default reporter on a failing test. Ran this on 4.1.11: const randomTest = test.extend("random", async ({ annotate }) => {
const seed = 1234567;
await annotate(`Random seed: ${seed}`, "random-seed");
return seed;
});
randomTest("failing case", ({ random }) => {
expect(random).toBe(-1);
});The line number in the annotation is the fixture, not the test body, so it is definitely the fixture's call being reported. The thing that produces exactly your screenshot is a failing test that doesn't pull the fixture out of the context. Fixtures are lazy, so nothing runs and there's nothing to annotate: randomTest("failing without using the fixture", () => { // no ({ random })
expect(1).toBe(2);
});No annotation, no warning. Worth checking that the failing test in your screenshot actually destructures For passing tests the default reporter stays quiet by design, |
Annotating from the fixture does reach the default reporter on a failing test. Ran this on 4.1.11:
The line number in the annotation is the fixture, not the test body, so it is definitely the fixture's call being reported.
The thing that produces exactly your screenshot is a failing test that doesn't pull the fixture out of the context. Fixtures are lazy, so nothing run…