Skip to content

Inline Snapshots with non literal can't be foundΒ #6744

Description

@elicwhite

πŸ› Bug Report

When I try to add a test with an inline snapshot:

it('works with inline snapshots', () => {
  expect({a: 1}).toMatchInlineSnapshot();
});

I get this error:

Jest: Couldn't locate all inline snapshots.

I can't repro this in repl.it because that version does not have inline snapshots yet:

TypeError: expect(...).toMatchInlineSnapshot is not a function

I cloned the github repo and added a test to inline_snapshots.test.js

test('saveInlineSnapshots() replaces empty function call with a template literal for objects', () => {
  const filename = path.join(__dirname, 'my.test.js');
  fs.readFileSync = (jest.fn(
    () => `expect({a: 'a'}).toMatchInlineSnapshot();\n`,
  ): any);

  saveInlineSnapshots(
    [
      {
        frame: {column: 11, file: filename, line: 1},
        snapshot: `{a: 'a'}`,
      },
    ],
    prettier,
    babelTraverse,
  );

  expect(fs.writeFileSync).toHaveBeenCalledWith(
    filename,
    "expect({a: 'a'}).toMatchInlineSnapshot(`{a: 'a'}`);\n",
  );
});

and this reproes the issue:

 FAIL  packages/jest-snapshot/src/__tests__/inline_snapshots.test.js
  βœ“ saveInlineSnapshots() replaces empty function call with a template literal (67ms)
  βœ• saveInlineSnapshots() replaces empty function call with a template literal for objects (19ms)
  βœ“ saveInlineSnapshots() replaces existing template literal - babylon parser (4ms)
  βœ“ saveInlineSnapshots() replaces existing template literal - flow parser (2ms)
  βœ“ saveInlineSnapshots() replaces existing template literal - typescript parser (2ms)
  βœ“ saveInlineSnapshots() replaces existing template literal with property matchers (3ms)
  βœ“ saveInlineSnapshots() throws if frame does not match (2ms)
  βœ“ saveInlineSnapshots() throws if multiple calls to to the same location (2ms)
  βœ“ saveInlineSnapshots() uses escaped backticks (2ms)

  ● saveInlineSnapshots() replaces empty function call with a template literal for objects

    Jest: Couldn't locate all inline snapshots.

      at Object.parse (packages/jest-snapshot/src/inline_snapshots.js:168:11)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions