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

[Inline Snapshots] Add test cases for #6744 #6772

Merged
merged 2 commits into from
Aug 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions e2e/__tests__/__snapshots__/to_match_inline_snapshot.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,14 @@ exports[`supports async tests 1`] = `
});
"
`;

exports[`writes snapshots with non-literals in expect(...) 1`] = `
"it('works with inline snapshots', () => {
expect({a: 1}).toMatchInlineSnapshot(\`
Object {
\\"a\\": 1,
}
\`);
});
"
`;
17 changes: 17 additions & 0 deletions e2e/__tests__/to_match_inline_snapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ test('supports async tests', () => {
expect(fileAfter).toMatchSnapshot();
});

test('writes snapshots with non-literals in expect(...)', () => {
const filename = 'async.test.js';
const test = `
it('works with inline snapshots', () => {
expect({a: 1}).toMatchInlineSnapshot();
});
`;

writeFiles(TESTS_DIR, {[filename]: test});
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);

const fileAfter = readFile(filename);
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
expect(status).toBe(0);
expect(fileAfter).toMatchSnapshot();
});

// issue: https://github.com/facebook/jest/issues/6702
test('handles mocking native modules prettier relies on', () => {
const filename = 'mockFail.test.js';
Expand Down
27 changes: 27 additions & 0 deletions packages/jest-snapshot/src/__tests__/inline_snapshots.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,30 @@ test('saveInlineSnapshots() uses escaped backticks', () => {
'expect("`").toMatchInlineSnapshot(`\\``);\n',
);
});

test('saveInlineSnapshots() works with non-literals in expect call', () => {
const filename = path.join(__dirname, 'my.test.js');
fs.readFileSync = (jest.fn(
() => `expect({a: 'a'}).toMatchInlineSnapshot();\n`,
): any);
prettier.resolveConfig.sync.mockReturnValue({
bracketSpacing: false,
singleQuote: true,
});

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

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