Skip to content

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

@alexjeffburke

Description

@alexjeffburke

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
    `
  );
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions