From 6aab35c6c79fd1367121c1db1b99e273e209589c Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sat, 28 Jul 2018 15:25:57 +1000 Subject: [PATCH] Add test cases for #6744 --- .../to_match_inline_snapshot.test.js.snap | 11 ++++++++ .../to_match_inline_snapshot.test.js | 16 +++++++++++ .../src/__tests__/inline_snapshots.test.js | 27 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/e2e/__tests__/__snapshots__/to_match_inline_snapshot.test.js.snap b/e2e/__tests__/__snapshots__/to_match_inline_snapshot.test.js.snap index 9045ee66b05f..a3372d2f31fd 100644 --- a/e2e/__tests__/__snapshots__/to_match_inline_snapshot.test.js.snap +++ b/e2e/__tests__/__snapshots__/to_match_inline_snapshot.test.js.snap @@ -113,3 +113,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, +} +\`); +}); +" +`; diff --git a/e2e/__tests__/to_match_inline_snapshot.test.js b/e2e/__tests__/to_match_inline_snapshot.test.js index f02ad696c316..a304b7d7a5e9 100644 --- a/e2e/__tests__/to_match_inline_snapshot.test.js +++ b/e2e/__tests__/to_match_inline_snapshot.test.js @@ -163,3 +163,19 @@ test('supports async tests', () => { expect(status).toBe(0); 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(); +}); diff --git a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.js b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.js index 53e9649e6db1..29a8c6b2eee1 100644 --- a/packages/jest-snapshot/src/__tests__/inline_snapshots.test.js +++ b/packages/jest-snapshot/src/__tests__/inline_snapshots.test.js @@ -173,3 +173,30 @@ test('saveInlineSnapshots() uses escaped backticks', () => { 'expect("`").toMatchInlineSnapshot(`\\``);\n', ); }); + +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); + 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", + ); +});