From a161fe5403fbc76e7e79850dfb8ad9a2d426c301 Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sun, 3 Jun 2018 22:07:44 +1000 Subject: [PATCH] Add tests for saveInlineSnapshots() --- .../jest-snapshot/src/__mocks__/prettier.js | 18 ++++ .../jest-snapshot/src/__tests__/utils.test.js | 92 +++++++++++++++++++ packages/jest-snapshot/src/utils.js | 2 +- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 packages/jest-snapshot/src/__mocks__/prettier.js diff --git a/packages/jest-snapshot/src/__mocks__/prettier.js b/packages/jest-snapshot/src/__mocks__/prettier.js new file mode 100644 index 000000000000..abef9f92d8b9 --- /dev/null +++ b/packages/jest-snapshot/src/__mocks__/prettier.js @@ -0,0 +1,18 @@ +const prettier = require.requireActual('prettier'); + +module.exports = { + format: (text, opts) => + prettier.format( + text, + Object.assign( + { + pluginSearchDirs: [ + require('path').dirname(require.resolve('prettier')), + ], + }, + opts, + ), + ), + getFileInfo: {sync: () => ({inferredParser: 'babylon'})}, + resolveConfig: {sync: () => null}, +}; diff --git a/packages/jest-snapshot/src/__tests__/utils.test.js b/packages/jest-snapshot/src/__tests__/utils.test.js index 76cb798902f1..4414fb14a93a 100644 --- a/packages/jest-snapshot/src/__tests__/utils.test.js +++ b/packages/jest-snapshot/src/__tests__/utils.test.js @@ -6,6 +6,7 @@ */ jest.mock('fs'); +jest.mock('prettier'); const fs = require('fs'); const path = require('path'); @@ -14,6 +15,7 @@ const chalk = require('chalk'); const { getSnapshotData, getSnapshotPath, + saveInlineSnapshots, keyToTestName, saveSnapshotFile, serialize, @@ -26,15 +28,23 @@ const { const writeFileSync = fs.writeFileSync; const readFileSync = fs.readFileSync; const existsSync = fs.existsSync; +const statSync = fs.statSync; +const readdirSync = fs.readdirSync; beforeEach(() => { fs.writeFileSync = jest.fn(); fs.readFileSync = jest.fn(); fs.existsSync = jest.fn(() => true); + fs.statSync = jest.fn(filePath => ({ + isDirectory: () => !filePath.endsWith('.js'), + })); + fs.readdirSync = jest.fn(() => []); }); afterEach(() => { fs.writeFileSync = writeFileSync; fs.readFileSync = readFileSync; fs.existsSync = existsSync; + fs.statSync = statSync; + fs.readdirSync = readdirSync; }); test('keyToTestName()', () => { @@ -84,6 +94,88 @@ test('saveSnapshotFile() works with \r', () => { ); }); +test('saveInlineSnapshots() replaces empty function call with a template literal', () => { + const filename = path.join(__dirname, 'my.test.js'); + fs.readFileSync = jest.fn(() => `expect(1).toMatchInlineSnapshot();\n`); + + saveInlineSnapshots([ + { + frame: {column: 11, file: filename, line: 1}, + snapshot: `1`, + }, + ]); + + expect(fs.writeFileSync).toHaveBeenCalledWith( + filename, + 'expect(1).toMatchInlineSnapshot(`1`);\n', + ); +}); + +test('saveInlineSnapshots() replaces existing template literal', () => { + const filename = path.join(__dirname, 'my.test.js'); + fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot(`2`);\n'); + + saveInlineSnapshots([ + { + frame: {column: 11, file: filename, line: 1}, + snapshot: `1`, + }, + ]); + + expect(fs.writeFileSync).toHaveBeenCalledWith( + filename, + 'expect(1).toMatchInlineSnapshot(`1`);\n', + ); +}); + +test('saveInlineSnapshots() replaces existing template literal with property matchers', () => { + const filename = path.join(__dirname, 'my.test.js'); + fs.readFileSync = jest.fn( + () => 'expect(1).toMatchInlineSnapshot({}, `2`);\n', + ); + + saveInlineSnapshots([ + { + frame: {column: 11, file: filename, line: 1}, + snapshot: `1`, + }, + ]); + + expect(fs.writeFileSync).toHaveBeenCalledWith( + filename, + 'expect(1).toMatchInlineSnapshot({}, `1`);\n', + ); +}); + +test('saveInlineSnapshots() throws if frame does not match', () => { + const filename = path.join(__dirname, 'my.test.js'); + fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot();\n'); + + const save = () => + saveInlineSnapshots([ + { + frame: {column: 2 /* incorrect */, file: filename, line: 1}, + snapshot: `1`, + }, + ]); + + expect(save).toThrowError(/Couldn't locate all inline snapshots./); +}); + +test('saveInlineSnapshots() throws if multiple calls to to the same location', () => { + const filename = path.join(__dirname, 'my.test.js'); + fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot();\n'); + + const frame = {column: 11, file: filename, line: 1}; + + const save = () => + saveInlineSnapshots([{frame, snapshot: `1`}, {frame, snapshot: `2`}]); + + expect(save).toThrowError( + /Multiple inline snapshots for the same call are not supported./, + ); +}); + test('getSnapshotData() throws when no snapshot version', () => { const filename = path.join(__dirname, 'old-snapshot.snap'); fs.readFileSync = jest.fn(() => 'exports[`myKey`] = `
\n
`;\n'); diff --git a/packages/jest-snapshot/src/utils.js b/packages/jest-snapshot/src/utils.js index 659f56bcf4c5..87e4daf17530 100644 --- a/packages/jest-snapshot/src/utils.js +++ b/packages/jest-snapshot/src/utils.js @@ -265,7 +265,7 @@ const createParser = (snapshots: InlineSnapshot[], inferredParser: string) => ( } if (snapshotsForFrame.length > 1) { throw new Error( - 'Jest: Multiple inline snapshots for the same call are not supported.', + 'Jest. Multiple inline snapshots for the same call are not supported.', ); } const snapshotIndex = args.findIndex(