Skip to content

Commit 14d8025

Browse files
committed
Add tests for saveInlineSnapshots()
1 parent 0d83224 commit 14d8025

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const prettier = require.requireActual('prettier');
2+
3+
module.exports = {
4+
format: (text, opts) =>
5+
prettier.format(
6+
text,
7+
Object.assign(
8+
{
9+
pluginSearchDirs: [
10+
require('path').dirname(require.resolve('prettier')),
11+
],
12+
},
13+
opts,
14+
),
15+
),
16+
getFileInfo: {sync: () => ({inferredParser: 'babylon'})},
17+
resolveConfig: {sync: () => null},
18+
};

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
jest.mock('fs');
9+
jest.mock('prettier');
910

1011
const fs = require('fs');
1112
const path = require('path');
@@ -14,6 +15,7 @@ const chalk = require('chalk');
1415
const {
1516
getSnapshotData,
1617
getSnapshotPath,
18+
saveInlineSnapshots,
1719
keyToTestName,
1820
saveSnapshotFile,
1921
serialize,
@@ -26,15 +28,23 @@ const {
2628
const writeFileSync = fs.writeFileSync;
2729
const readFileSync = fs.readFileSync;
2830
const existsSync = fs.existsSync;
31+
const statSync = fs.statSync;
32+
const readdirSync = fs.readdirSync;
2933
beforeEach(() => {
3034
fs.writeFileSync = jest.fn();
3135
fs.readFileSync = jest.fn();
3236
fs.existsSync = jest.fn(() => true);
37+
fs.statSync = jest.fn(filePath => ({
38+
isDirectory: () => !filePath.endsWith('.js'),
39+
}));
40+
fs.readdirSync = jest.fn(() => []);
3341
});
3442
afterEach(() => {
3543
fs.writeFileSync = writeFileSync;
3644
fs.readFileSync = readFileSync;
3745
fs.existsSync = existsSync;
46+
fs.statSync = statSync;
47+
fs.readdirSync = readdirSync;
3848
});
3949

4050
test('keyToTestName()', () => {
@@ -84,6 +94,88 @@ test('saveSnapshotFile() works with \r', () => {
8494
);
8595
});
8696

97+
test('saveInlineSnapshots() replaces empty function call with a template literal', () => {
98+
const filename = path.join(__dirname, 'my.test.js');
99+
fs.readFileSync = jest.fn(() => `expect(1).toMatchInlineSnapshot();\n`);
100+
101+
saveInlineSnapshots([
102+
{
103+
frame: {column: 11, file: filename, line: 1},
104+
snapshot: `1`,
105+
},
106+
]);
107+
108+
expect(fs.writeFileSync).toHaveBeenCalledWith(
109+
filename,
110+
'expect(1).toMatchInlineSnapshot(`1`);\n',
111+
);
112+
});
113+
114+
test('saveInlineSnapshots() replaces existing template literal', () => {
115+
const filename = path.join(__dirname, 'my.test.js');
116+
fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot(`2`);\n');
117+
118+
saveInlineSnapshots([
119+
{
120+
frame: {column: 11, file: filename, line: 1},
121+
snapshot: `1`,
122+
},
123+
]);
124+
125+
expect(fs.writeFileSync).toHaveBeenCalledWith(
126+
filename,
127+
'expect(1).toMatchInlineSnapshot(`1`);\n',
128+
);
129+
});
130+
131+
test('saveInlineSnapshots() replaces existing template literal with property matchers', () => {
132+
const filename = path.join(__dirname, 'my.test.js');
133+
fs.readFileSync = jest.fn(
134+
() => 'expect(1).toMatchInlineSnapshot({}, `2`);\n',
135+
);
136+
137+
saveInlineSnapshots([
138+
{
139+
frame: {column: 11, file: filename, line: 1},
140+
snapshot: `1`,
141+
},
142+
]);
143+
144+
expect(fs.writeFileSync).toHaveBeenCalledWith(
145+
filename,
146+
'expect(1).toMatchInlineSnapshot({}, `1`);\n',
147+
);
148+
});
149+
150+
test('saveInlineSnapshots() throws if frame does not match', () => {
151+
const filename = path.join(__dirname, 'my.test.js');
152+
fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot();\n');
153+
154+
const save = () =>
155+
saveInlineSnapshots([
156+
{
157+
frame: {column: 2 /* incorrect */, file: filename, line: 1},
158+
snapshot: `1`,
159+
},
160+
]);
161+
162+
expect(save).toThrowError(/Couldn't locate all inline snapshots./);
163+
});
164+
165+
test('saveInlineSnapshots() throws if multiple calls to to the same location', () => {
166+
const filename = path.join(__dirname, 'my.test.js');
167+
fs.readFileSync = jest.fn(() => 'expect(1).toMatchInlineSnapshot();\n');
168+
169+
const frame = {column: 11, file: filename, line: 1};
170+
171+
const save = () =>
172+
saveInlineSnapshots([{frame, snapshot: `1`}, {frame, snapshot: `2`}]);
173+
174+
expect(save).toThrowError(
175+
/Multiple inline snapshots for the same call are not supported./,
176+
);
177+
});
178+
87179
test('getSnapshotData() throws when no snapshot version', () => {
88180
const filename = path.join(__dirname, 'old-snapshot.snap');
89181
fs.readFileSync = jest.fn(() => 'exports[`myKey`] = `<div>\n</div>`;\n');

0 commit comments

Comments
 (0)