Skip to content

test_runner: add inlineSnapshot #59463

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pmarchini
Copy link
Member

@pmarchini pmarchini commented Aug 13, 2025

This is a draft implementation related to this issue: #59231.

I'm opening this PR mainly to collect some feedback regarding this feature and potential implementations, solutions, or ideas!

Note: The proposed solution is working, but it still needs refinement.

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/test_runner

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem. labels Aug 13, 2025
@pmarchini pmarchini force-pushed the issue/59231-toMatchInlineSnapshot branch from bd403c8 to 475eca6 Compare August 13, 2025 21:46
added: REPLACEME
-->

> Stability: 1.0 - Early development

Choose a reason for hiding this comment

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

These docs rock!

});
```

When first run without the `--test-update-snapshots` flag, the test will fail

Choose a reason for hiding this comment

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

In my opinion, the ideal DX is to always assume a missing second parameter "wants to be updated" whether or not the --test-update-snapshots flag is passed. You can then get a really nice micro workflow on a given test without ever needing to pass the flag just by repeatedly deleting the param (in my IDE, I often delete the parameter just by hitting undo: ctrl-z, save, test / update, ctrl-z, save, test / update, ... until I get the shape I want).

const user = { name: 'Alice', age: 30 };

// Initially call without second argument
t.assert.inlineSnapshot(user);

Choose a reason for hiding this comment

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

Great name! I think this aligns really well to the current TestContext.assert.snapshot() API. Also, I'm pleasantly surprised I don't need to await here.

test('inline snapshot test', (t) => {
const user = { name: 'Alice', age: 30 };

t.assert.inlineSnapshot(user, `
Copy link

@niedzielski niedzielski Aug 14, 2025

Choose a reason for hiding this comment

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

It would be really helpful if inlineSnapshot() supported wrapping. For example:

/**
 * Asserts bitmap is the expected hex value. The `expectedHex` arg is replaced
 * automatically when tests are run with `--test-update-snapshots`.
 */
function assertBitmapHex(ctx: TestContext, bmp: Uint8Array, expectedHex: string): string {
  const actualHex = [...bmp]
    .map(v => v.toString(16).padStart(2, '0'))
    .join('')
  ctx.assert.inlineSnapshot(actualHex, expectedHex) // <-- don't update me inline here!
}
test('', () => {
  // ...
  assertBitmapHex(bmp, '000000000000000000000000') // <-- I get updated inline here since I'm called at the top level test function.
})

It's possible to not use wrapping but then it gets pretty clumsy trying to fill out all the args in every test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants