forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils-test.js
167 lines (146 loc) Β· 5.16 KB
/
utils-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
const {
getSnapshotData,
getSnapshotPath,
keyToTestName,
saveSnapshotFile,
testNameToKey,
SNAPSHOT_GUIDE_LINK,
SNAPSHOT_VERSION,
SNAPSHOT_VERSION_WARNING,
} = require('../utils');
const fs = require('fs');
const path = require('path');
const writeFileSync = fs.writeFileSync;
const readFileSync = fs.readFileSync;
beforeEach(() => {
fs.writeFileSync = jest.fn();
fs.readFileSync = jest.fn();
});
afterEach(() => {
fs.writeFileSync = writeFileSync;
fs.readFileSync = readFileSync;
});
jest.mock('jest-file-exists', () => () => true);
test('keyToTestName()', () => {
expect(keyToTestName('abc cde 12')).toBe('abc cde');
expect(keyToTestName('abc cde 12')).toBe('abc cde ');
expect(() => keyToTestName('abc cde'))
.toThrowError('Snapshot keys must end with a number.');
});
test('testNameToKey', () => {
expect(testNameToKey('abc cde', 1)).toBe('abc cde 1');
expect(testNameToKey('abc cde ', 12)).toBe('abc cde 12');
});
test('getSnapshotPath()', () => {
expect(getSnapshotPath(
'/abc/cde/a-test.js',
)).toBe(
path.join('/abc', 'cde', '__snapshots__', 'a-test.js.snap'),
);
});
test('saveSnapshotFile() works with \r\n', () => {
const filename = path.join(__dirname, 'remove-newlines.snap');
const data = {
myKey: '<div>\r\n</div>',
};
saveSnapshotFile(data, filename);
expect(fs.writeFileSync)
.toBeCalledWith(
filename,
`// Jest Snapshot v1, ${SNAPSHOT_GUIDE_LINK}\n\n` +
'exports[`myKey`] = `<div>\n</div>`;\n'
);
});
test('saveSnapshotFile() works with \r', () => {
const filename = path.join(__dirname, 'remove-newlines.snap');
const data = {
myKey: '<div>\r</div>',
};
saveSnapshotFile(data, filename);
expect(fs.writeFileSync)
.toBeCalledWith(
filename,
`// Jest Snapshot v1, ${SNAPSHOT_GUIDE_LINK}\n\n` +
'exports[`myKey`] = `<div>\n</div>`;\n'
);
});
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');
const update = false;
expect(() => getSnapshotData(filename, update)).toThrowError(
`Outdated snapshot: No snapshot header found. ` +
`Jest 19 introduced versioned snapshots to ensure all people on ` +
`a project are using the same version of Jest. ` +
`Please update all snapshots during this upgrade of Jest.\n\n` +
SNAPSHOT_VERSION_WARNING
);
});
test('getSnapshotData() throws for older snapshot version', () => {
const filename = path.join(__dirname, 'old-snapshot.snap');
fs.readFileSync = jest.fn(() =>
`// Jest Snapshot v0.99, ${SNAPSHOT_GUIDE_LINK}\n\n` +
'exports[`myKey`] = `<div>\n</div>`;\n'
);
const update = false;
expect(() => getSnapshotData(filename, update)).toThrowError(
`Outdated snapshot: The version of the snapshot file associated ` +
`with this test is outdated. The snapshot file version ensures that ` +
`all people on a project are using the same version of Jest. ` +
`Please update all snapshots during this upgrade of Jest.\n\n` +
`Expected: v${SNAPSHOT_VERSION}\n` +
`Received: v0.99\n\n` +
SNAPSHOT_VERSION_WARNING
);
});
test('getSnapshotData() throws for newer snapshot version', () => {
const filename = path.join(__dirname, 'old-snapshot.snap');
fs.readFileSync = jest.fn(() =>
`// Jest Snapshot v2, ${SNAPSHOT_GUIDE_LINK}\n\n` +
'exports[`myKey`] = `<div>\n</div>`;\n'
);
const update = false;
expect(() => getSnapshotData(filename, update)).toThrowError(
`Outdated Jest version: the version of this snapshot file indicates ` +
`that this project is meant to be used with a newer version of Jest. ` +
`The snapshot file version ensures that all people on a project ` +
`are using the same version of Jest. ` +
`Please update your version of Jest and re-run the tests.\n\n` +
`Expected: v${SNAPSHOT_VERSION}\n` +
`Received: v2`
);
});
test('getSnapshotData() does not throw for when updating', () => {
const filename = path.join(__dirname, 'old-snapshot.snap');
fs.readFileSync = jest.fn(() =>
'exports[`myKey`] = `<div>\n</div>`;\n'
);
const update = true;
expect(() => getSnapshotData(filename, update)).not.toThrow();
});
test('escaping', () => {
const filename = path.join(__dirname, 'escaping.snap');
const data = '"\'\\';
saveSnapshotFile({key: data}, filename);
const writtenData = fs.writeFileSync.mock.calls[0][1];
expect(writtenData)
.toBe(
`// Jest Snapshot v1, ${SNAPSHOT_GUIDE_LINK}\n\n` +
"exports[`key`] = `\"'\\\\`;\n"
);
// eslint-disable-next-line no-unused-vars
const exports = {};
// eslint-disable-next-line no-eval
const readData = eval('var exports = {}; ' + writtenData + ' exports');
expect(readData).toEqual({key: data});
const snapshotData = readData.key;
expect(data).toEqual(snapshotData);
});