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

Issues using snapshots with expect.it() in a nested structure #57

Open
alexjeffburke opened this issue Apr 13, 2020 · 3 comments
Open

Comments

@alexjeffburke
Copy link
Member

alexjeffburke commented Apr 13, 2020

I had a case where I tried to check that a function was called with an object that contained a key whose value was a snapshotted string, so I used one of the "satisfying" assertions in -sinon. The following is a simplified reproduction case:

const expect = require("unexpected")
  .clone()
  .use(require("unexpected-snapshot"));

const makeSomeStuff = () => "foo bar baz\nbaz bar foo";

it("should work", () => {
  expect(
    [
      {
        stuff: makeSomeStuff()
      }
    ],
    "to satisfy",
    [
      {
        stuff: expect.it("to equal snapshot")
      }
    ]
  );
});

Running mocha on the above in snapshot update mode as-is, I receive the following TypeError:

     TypeError: Cannot read property 'replace' of undefined
      at fixUnexpected (node_modules/unexpected-snapshot/lib/applyFixes.js:87:62)
      at Traverser.enter [as _enter] (node_modules/unexpected-snapshot/lib/applyFixes.js:209:26)

If I force the insertion point by modifying the expect.it to read expect.it('to equal snapshot', '') it gets further, but the injection of the snapshot removes the nested spec structure on the RHS and instead injects at the top level resulting in:

it("should work", () => {
  expect(
    [
      {
        stuff: makeSomeStuff()
      }
    ],
    "to satisfy",
    expect.unindent`
      foo bar baz
      baz bar foo
    `
  );
});
@alexjeffburke alexjeffburke changed the title Issues using snapshots with expect.it() in a nested structure Issues using snapshots with expect.it() in a nested structure Apr 13, 2020
@papandreou
Copy link
Member

unexpected-snapshot does not support expect.it at the moment. It'll be rather tricky, so we decided to leave it out initially. Would be nice if it failed a bit more gracefully, though.

@papandreou
Copy link
Member

I added a nicer warning in 1.0.2.

@alexjeffburke
Copy link
Member Author

@papandreou I confess that I wasn't aware this wasn't intended to work per se, and choosing to show a clear message in this case is a really good shout - keep the user informed etc.

I'll try the change in situ so I get some perspective on encountering it in person but otherwise I think we can say this is addressed. And we can already revisit if/when we tackle it.

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

No branches or pull requests

2 participants