Skip to content

Commit af036fa

Browse files
azzthymikee
authored andcommitted
Add test cases for #6744 (#6772)
1 parent 690450d commit af036fa

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

e2e/__tests__/__snapshots__/to_match_inline_snapshot.test.js.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,14 @@ exports[`supports async tests 1`] = `
135135
});
136136
"
137137
`;
138+
139+
exports[`writes snapshots with non-literals in expect(...) 1`] = `
140+
"it('works with inline snapshots', () => {
141+
expect({a: 1}).toMatchInlineSnapshot(\`
142+
Object {
143+
\\"a\\": 1,
144+
}
145+
\`);
146+
});
147+
"
148+
`;

e2e/__tests__/to_match_inline_snapshot.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,23 @@ test('supports async tests', () => {
212212
expect(fileAfter).toMatchSnapshot();
213213
});
214214

215+
test('writes snapshots with non-literals in expect(...)', () => {
216+
const filename = 'async.test.js';
217+
const test = `
218+
it('works with inline snapshots', () => {
219+
expect({a: 1}).toMatchInlineSnapshot();
220+
});
221+
`;
222+
223+
writeFiles(TESTS_DIR, {[filename]: test});
224+
const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false', filename]);
225+
226+
const fileAfter = readFile(filename);
227+
expect(stderr).toMatch('1 snapshot written from 1 test suite.');
228+
expect(status).toBe(0);
229+
expect(fileAfter).toMatchSnapshot();
230+
});
231+
215232
// issue: https://github.com/facebook/jest/issues/6702
216233
test('handles mocking native modules prettier relies on', () => {
217234
const filename = 'mockFail.test.js';

packages/jest-snapshot/src/__tests__/inline_snapshots.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,30 @@ test('saveInlineSnapshots() uses escaped backticks', () => {
173173
'expect("`").toMatchInlineSnapshot(`\\``);\n',
174174
);
175175
});
176+
177+
test('saveInlineSnapshots() works with non-literals in expect call', () => {
178+
const filename = path.join(__dirname, 'my.test.js');
179+
fs.readFileSync = (jest.fn(
180+
() => `expect({a: 'a'}).toMatchInlineSnapshot();\n`,
181+
): any);
182+
prettier.resolveConfig.sync.mockReturnValue({
183+
bracketSpacing: false,
184+
singleQuote: true,
185+
});
186+
187+
saveInlineSnapshots(
188+
[
189+
{
190+
frame: {column: 18, file: filename, line: 1},
191+
snapshot: `{a: 'a'}`,
192+
},
193+
],
194+
prettier,
195+
babelTraverse,
196+
);
197+
198+
expect(fs.writeFileSync).toHaveBeenCalledWith(
199+
filename,
200+
"expect({a: 'a'}).toMatchInlineSnapshot(`{a: 'a'}`);\n",
201+
);
202+
});

0 commit comments

Comments
 (0)