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

Proposal: include snapshot name in error message #6003

Closed
hroemer opened this issue Apr 16, 2018 · 3 comments
Closed

Proposal: include snapshot name in error message #6003

hroemer opened this issue Apr 16, 2018 · 3 comments

Comments

@hroemer
Copy link

hroemer commented Apr 16, 2018

Current behavior

with a jest configuration like this:

module.exports = {
    transform: {
        "^.+\\.tsx?$": "ts-jest"
    },
    testRegex: "(./specs/.*/.*(\\.test|\\.spec))\\.(ts?|tsx?)$",
    moduleFileExtensions: [
        "ts",
        "js",
        "json",
    ]
};

Named snapshot test

Consider a snapshot based test including snapshot names for differentiation, e.g.:

test("Test1", () => {
    expect("VALUE1").toMatchSnapshot();
    expect("VALUE2").toMatchSnapshot("name");
    expect("VALUE3").toMatchSnapshot("another");
});

It will produce a snapshot file including the snapshot names (name, another) as well:

// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test1 1`] = `"VALUE1"`;

exports[`Test1: another 1`] = `"VALUE3"`;

exports[`Test1: name 1`] = `"VALUE2"`;

If in a subsequent run the test fails due to a failed expectation it will render this:

✕ Test1 (10ms)

  ● Test1

    expect(value).toMatchSnapshot()
    
    Received value does not match stored snapshot 1.
    
    - "VALUE3"
    + "VALUEX"

      2 |     expect("VALUE1").toMatchSnapshot();
      3 |     expect("VALUE2").toMatchSnapshot("name");
    > 4 |     expect("VALUEX").toMatchSnapshot("another");
      5 | });
      6 | 
      
      at Object.<anonymous>.test (specs/lib/jest/snap.spec.ts:4:22)

 › 1 snapshot test failed.

The failing snapshot line number is marked accordingly.

Wrapped expect hides origin

If the snapshot expect code is wrapped in some library function:

function testWrap(actual: string, snapshotName: string){
    expect(actual).toMatchSnapshot(snapshotName);
}

test("Test2", () => {
    testWrap("VALUE1", null);
    testWrap("VALUE2", "name");
    testWrap("VALUE3", "another");
});

and the test fails due to a failed expectation it will render this:

✕ Test2 (4ms)

  ● Test2

    expect(value).toMatchSnapshot()
    
    Received value does not match stored snapshot 1.
    
    - "VALUE2"
    + "VALUEX"

       6 | 
       7 | function testWrap(actual: string, snapshotName: string){
    >  8 |     expect(actual).toMatchSnapshot(snapshotName);
       9 | }
      10 | 
      11 | test("Test2", () => {
      
      at testWrap (specs/lib/jest/snap.spec.ts:8:20)
      at Object.<anonymous>.test (specs/lib/jest/snap.spec.ts:13:5)

 › 1 snapshot test failed.

Based on the originating line it is now hard to figure out which "sub" snapshot actually failed.

Proposal

Could you please add the snapshot name to the first error message? I.e. like this:

● Test2

    expect(value).toMatchSnapshot()
    
    Received value does not match stored snapshot "name" 1 .
    
    - "VALUE2"
    + "VALUEX"
@SimenB
Copy link
Member

SimenB commented Apr 16, 2018

I think that's a great idea. It's a breaking change, but we're gearing up for version 23 anyways, so that's OK. Wanna send a PR for it?

hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
@hroemer
Copy link
Author

hroemer commented Apr 17, 2018

I just added a PR #6015 for it.

hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
hroemer pushed a commit to hroemer/jest that referenced this issue Apr 17, 2018
cpojer pushed a commit that referenced this issue Apr 18, 2018
* #6003: include snapshot name in error message

* #6003: added changelog message

* #6003: wrapped snapshot name and count in double quotes

* #6003: wrapped snapshot name and count in double quotes -> updated snap

* #6003: fixed lint issues in changelog

* #6003: writing complete snapshot key to output
@cpojer cpojer closed this as completed Apr 18, 2018
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants