Skip to content

Commit

Permalink
Add test cases for #6744 (#6772)
Browse files Browse the repository at this point in the history
  • Loading branch information
azz authored and thymikee committed Aug 8, 2018
1 parent 690450d commit af036fa
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
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",
);
});

0 comments on commit af036fa

Please sign in to comment.