/
no-deprecated-functions.test.ts
265 lines (231 loc) · 7.47 KB
/
no-deprecated-functions.test.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule, {
JestVersion,
_clearCachedJestVersion,
} from '../no-deprecated-functions';
const ruleTester = new TSESLint.RuleTester();
/**
* Makes a new temp directory, prefixed with `eslint-plugin-jest-`
*
* @return {Promise<string>}
*/
const makeTempDir = async () =>
fs.mkdtempSync(path.join(os.tmpdir(), 'eslint-plugin-jest-'));
/**
* Sets up a fake project with a `package.json` file located in
* `node_modules/jest` whose version is set to the given `jestVersion`.
*
* @param {JestVersion} jestVersion
*
* @return {Promise<string>}
*/
const setupFakeProjectDirectory = async (
jestVersion: JestVersion,
): Promise<string> => {
const jestPackageJson: JSONSchemaForNPMPackageJsonFiles = {
name: 'jest',
version: `${jestVersion}.0.0`,
};
const tempDir = await makeTempDir();
const jestPackagePath = path.join(tempDir, 'node_modules', 'jest');
// todo: remove in node@10 & replace with { recursive: true }
fs.mkdirSync(path.join(tempDir, 'node_modules'));
fs.mkdirSync(jestPackagePath);
await fs.writeFileSync(
path.join(jestPackagePath, 'package.json'),
JSON.stringify(jestPackageJson),
);
return tempDir;
};
const generateValidCases = (
jestVersion: JestVersion | undefined,
functionCall: string,
): Array<TSESLint.ValidTestCase<never>> => {
const [name, func] = functionCall.split('.');
const settings = { jest: { version: jestVersion } } as const;
return [
{ settings, code: `${functionCall}()` },
{ settings, code: `${functionCall}` },
{ settings, code: `${name}['${func}']()` },
{ settings, code: `${name}['${func}']` },
];
};
const generateInvalidCases = (
jestVersion: JestVersion | undefined,
deprecation: string,
replacement: string,
): Array<TSESLint.InvalidTestCase<'deprecatedFunction', never>> => {
const [deprecatedName, deprecatedFunc] = deprecation.split('.');
const [replacementName, replacementFunc] = replacement.split('.');
const settings = { jest: { version: jestVersion } };
const errors: [TSESLint.TestCaseError<'deprecatedFunction'>] = [
{ messageId: 'deprecatedFunction', data: { deprecation, replacement } },
];
return [
{
code: `${deprecation}()`,
output: `${replacement}()`,
settings,
errors,
},
{
code: `${deprecatedName}['${deprecatedFunc}']()`,
output: `${replacementName}['${replacementFunc}']()`,
settings,
errors,
},
];
};
describe('the jest version cache', () => {
beforeEach(async () => process.chdir(await setupFakeProjectDirectory(17)));
// change the jest version *after* each test case
afterEach(async () => {
const jestPackageJson: JSONSchemaForNPMPackageJsonFiles = {
name: 'jest',
version: '24.0.0',
};
const tempDir = process.cwd();
await fs.writeFileSync(
path.join(tempDir, 'node_modules', 'jest', 'package.json'),
JSON.stringify(jestPackageJson),
);
});
ruleTester.run('no-deprecated-functions', rule, {
valid: [
'require("fs")', // this will cause jest version to be read & cached
'jest.requireActual()', // deprecated after jest 17
],
invalid: [],
});
});
// contains the cache-clearing beforeEach so we can test the cache too
describe('the rule', () => {
beforeEach(() => _clearCachedJestVersion());
// a few sanity checks before doing our massive loop
ruleTester.run('no-deprecated-functions', rule, {
valid: [
'jest',
'require("fs")',
...generateValidCases(14, 'jest.resetModuleRegistry'),
...generateValidCases(17, 'require.requireActual'),
...generateValidCases(25, 'jest.genMockFromModule'),
],
invalid: [
...generateInvalidCases(
21,
'jest.resetModuleRegistry',
'jest.resetModules',
),
...generateInvalidCases(24, 'jest.addMatchers', 'expect.extend'),
...generateInvalidCases(
26,
'jest.genMockFromModule',
'jest.createMockFromModule',
),
],
});
describe.each<JestVersion>([
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
])('when using jest version %i', jestVersion => {
beforeEach(async () =>
process.chdir(await setupFakeProjectDirectory(jestVersion)),
);
const allowedFunctions: string[] = [];
const deprecations = ([
[15, 'jest.resetModuleRegistry', 'jest.resetModules'],
[17, 'jest.addMatchers', 'expect.extend'],
[21, 'require.requireMock', 'jest.requireMock'],
[21, 'require.requireActual', 'jest.requireActual'],
[22, 'jest.runTimersToTime', 'jest.advanceTimersByTime'],
[26, 'jest.genMockFromModule', 'jest.createMockFromModule'],
] as const).filter(deprecation => {
if (deprecation[0] > jestVersion) {
allowedFunctions.push(deprecation[1]);
return false;
}
return true;
});
ruleTester.run('explict jest version', rule, {
valid: [
'jest',
'require("fs")',
...allowedFunctions
.map(func => generateValidCases(jestVersion, func))
.reduce((acc, arr) => acc.concat(arr), []),
],
invalid: deprecations
.map(([, deprecation, replacement]) =>
generateInvalidCases(jestVersion, deprecation, replacement),
)
.reduce((acc, arr) => acc.concat(arr), []),
});
ruleTester.run('detected jest version', rule, {
valid: [
'jest',
'require("fs")',
...allowedFunctions
.map(func => generateValidCases(undefined, func))
.reduce((acc, arr) => acc.concat(arr), []),
],
invalid: deprecations
.map(([, deprecation, replacement]) =>
generateInvalidCases(undefined, deprecation, replacement),
)
.reduce((acc, arr) => acc.concat(arr), []),
});
});
describe('when no jest version is provided', () => {
describe('when the jest package.json is missing the version property', () => {
beforeEach(async () => {
const tempDir = await setupFakeProjectDirectory(1);
await fs.writeFileSync(
path.join(tempDir, 'node_modules', 'jest', 'package.json'),
JSON.stringify({}),
);
process.chdir(tempDir);
});
it('requires the version to be set explicitly', () => {
expect(() => {
const linter = new TSESLint.Linter();
linter.defineRule('no-deprecated-functions', rule);
linter.verify('jest.resetModuleRegistry()', {
rules: { 'no-deprecated-functions': 'error' },
});
}).toThrow(
'Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly',
);
});
});
describe('when the jest package.json is not found', () => {
beforeEach(async () => process.chdir(await makeTempDir()));
it('requires the version to be set explicitly', () => {
expect(() => {
const linter = new TSESLint.Linter();
linter.defineRule('no-deprecated-functions', rule);
linter.verify('jest.resetModuleRegistry()', {
rules: { 'no-deprecated-functions': 'error' },
});
}).toThrow(
'Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly',
);
});
});
});
});