Skip to content

Commit

Permalink
Add tests for saveInlineSnapshots()
Browse files Browse the repository at this point in the history
  • Loading branch information
azz committed Jun 3, 2018
1 parent 0d83224 commit 14d8025
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/jest-snapshot/src/__mocks__/prettier.js
Original file line number Diff line number Diff line change
@@ -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},
};
92 changes: 92 additions & 0 deletions packages/jest-snapshot/src/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

jest.mock('fs');
jest.mock('prettier');

const fs = require('fs');
const path = require('path');
Expand All @@ -14,6 +15,7 @@ const chalk = require('chalk');
const {
getSnapshotData,
getSnapshotPath,
saveInlineSnapshots,
keyToTestName,
saveSnapshotFile,
serialize,
Expand All @@ -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()', () => {
Expand Down Expand Up @@ -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`] = `<div>\n</div>`;\n');
Expand Down

0 comments on commit 14d8025

Please sign in to comment.