-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
format-files.test.js
320 lines (288 loc) · 11.4 KB
/
format-files.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* eslint no-console:0 */
import fsMock from 'fs';
import findUpMock from 'find-up';
import globMock from 'glob';
import mockGetStdin from 'get-stdin';
import getLogger from 'loglevel-colored-level-prefix';
import formatMock from './prettier-eslint';
import formatFiles from './format-files';
jest.mock('fs');
beforeEach(() => {
process.stdout.write = jest.fn();
console.error = jest.fn();
console.log = jest.fn();
formatMock.mockClear();
fsMock.writeFile.mockClear();
fsMock.readFile.mockClear();
});
afterEach(() => {
process.exitCode = 0;
});
test('sanity test', async () => {
const globs = ['src/**/1*.js', 'src/**/2*.js'];
await formatFiles({ _: globs });
expect(globMock).toHaveBeenCalledTimes(globs.length);
expect(fsMock.readFile).toHaveBeenCalledTimes(6);
expect(formatMock).toHaveBeenCalledTimes(6);
expect(fsMock.writeFile).toHaveBeenCalledTimes(0);
expect(process.stdout.write).toHaveBeenCalledTimes(6);
expect(console.error).toHaveBeenCalledTimes(1);
const mockOutput = expect.stringMatching(/MOCK_OUTPUT.*index.js/);
const successOutput = expect.stringMatching(/success.*6.*files/);
expect(process.stdout.write).toHaveBeenCalledWith(mockOutput);
expect(console.error).toHaveBeenCalledWith(successOutput);
});
test('glob call inclues an ignore of node_modules', async () => {
const fileGlob = 'src/**/1*.js';
await formatFiles({ _: [fileGlob] });
const globOptions = expect.objectContaining({
ignore: expect.arrayContaining(['**/node_modules/**'])
});
const callback = expect.any(Function);
expect(globMock).toHaveBeenCalledWith(fileGlob, globOptions, callback);
});
test('glob call excludes an ignore of node_modules', async () => {
const fileGlob = 'foo/node_modules/stuff*.js';
await formatFiles({ _: [fileGlob] });
expect(globMock).not.toHaveBeenCalledWith(
expect.any,
expect.objectContaining({
// should not have an ignore with **/node_modules/**
ignore: expect.arrayContaining(['**/node_modules/**'])
}),
expect.any
);
});
test('should accept stdin', async () => {
mockGetStdin.stdin = ' var [ foo, { bar } ] = window.APP ;';
await formatFiles({ stdin: true });
expect(formatMock).toHaveBeenCalledTimes(1);
// the trim is part of the test
const text = mockGetStdin.stdin.trim();
expect(formatMock).toHaveBeenCalledWith(expect.objectContaining({ text }));
expect(process.stdout.write).toHaveBeenCalledTimes(1);
expect(process.stdout.write).toHaveBeenCalledWith('MOCK_OUTPUT for stdin');
});
test('will write to files if that is specified', async () => {
const fileGlob = 'src/**/1*.js';
await formatFiles({ _: [fileGlob], write: true });
expect(fsMock.writeFile).toHaveBeenCalledTimes(4);
});
test('handles stdin errors gracefully', async () => {
mockGetStdin.stdin = 'MOCK_SYNTAX_ERROR';
await formatFiles({ stdin: true });
expect(console.error).toHaveBeenCalledTimes(1);
});
test('handles file errors gracefully', async () => {
const globs = ['files-with-syntax-errors/*.js', 'src/**/1*.js'];
await formatFiles({ _: globs, write: true });
expect(fsMock.writeFile).toHaveBeenCalledTimes(4);
expect(console.error).toHaveBeenCalledTimes(4);
const successOutput = expect.stringMatching(/success.*4.*files/);
const failureOutput = expect.stringMatching(/failure.*2.*files/);
expect(console.error).toHaveBeenCalledWith(successOutput);
expect(console.error).toHaveBeenCalledWith(failureOutput);
});
test('does not print success if there were no successful files', async () => {
await formatFiles({ _: ['no-match/*.js'] });
const successOutput = expect.stringMatching(/unhandled error/);
expect(process.stdout.write).not.toHaveBeenCalledWith(successOutput);
});
test('fails gracefully if something odd happens', async () => {
await formatFiles({ _: ['throw-error/*.js'] });
expect(console.error).toHaveBeenCalledTimes(1);
const label = expect.stringMatching(/prettier-eslint-cli/);
const notice = expect.stringMatching(/unhandled error/);
const errorStack = expect.stringMatching(/something weird happened/);
expect(console.error).toHaveBeenCalledWith(label, notice, errorStack);
});
test('logs errors to the console if something goes wrong', async () => {
const globs = ['eslint-config-error/*.js', 'src/**/2*.js'];
await formatFiles({ _: globs, write: true });
expect(fsMock.writeFile).toHaveBeenCalledTimes(4);
expect(console.error).toHaveBeenCalledTimes(4);
const successOutput = expect.stringMatching(/success.*4.*files/);
const failureOutput = expect.stringMatching(/failure.*2.*files/);
expect(console.error).toHaveBeenCalledWith(successOutput);
expect(console.error).toHaveBeenCalledWith(failureOutput);
const errorPrefix = expect.stringMatching(/prettier-eslint-cli.*ERROR/);
const cliError = expect.stringContaining('eslint-config-error');
const errorOutput = expect.stringContaining('Some weird eslint config error');
expect(console.error).toHaveBeenCalledWith(
errorPrefix,
cliError,
errorOutput
);
});
test('does not log anything to the console if logLevel is silent', async () => {
const log = getLogger();
const globs = ['eslint-config-error/*.js', 'src/**/2*.js'];
await formatFiles({
_: globs,
write: true,
logLevel: log.levels.SILENT
});
expect(fsMock.writeFile).toHaveBeenCalledTimes(4);
expect(console.error).not.toHaveBeenCalled();
});
test('forwards logLevel onto prettier-eslint', async () => {
await formatFiles({ _: ['src/**/1*.js'], logLevel: 'debug' });
const options = expect.objectContaining({ logLevel: 'debug' });
expect(formatMock).toHaveBeenCalledWith(options);
});
test('forwards prettierLast onto prettier-eslint', async () => {
await formatFiles({ _: ['src/**/1*.js'], prettierLast: true });
expect(formatMock).toHaveBeenCalledWith(
expect.objectContaining({ prettierLast: true })
);
});
test('forwards prettierOptions onto prettier-eslint', async () => {
await formatFiles({
_: ['src/**/1*.js'],
trailingComma: 'es5'
});
expect(formatMock).toHaveBeenCalledWith(
expect.objectContaining({ prettierOptions: { trailingComma: 'es5' } })
);
});
test('wont save file if contents did not change', async () => {
const fileGlob = 'no-change/*.js';
await formatFiles({ _: [fileGlob], write: true });
expect(fsMock.readFile).toHaveBeenCalledTimes(3);
expect(fsMock.writeFile).toHaveBeenCalledTimes(0);
const unchangedOutput = expect.stringMatching(/3.*?files.*?unchanged/);
expect(console.error).toHaveBeenCalledWith(unchangedOutput);
});
test('will report unchanged files even if not written', async () => {
const fileGlob = 'no-change/*.js';
await formatFiles({ _: [fileGlob], write: false });
expect(fsMock.readFile).toHaveBeenCalledTimes(3);
expect(fsMock.writeFile).toHaveBeenCalledTimes(0);
const unchangedOutput = expect.stringMatching(/3.*?files.*?unchanged/);
expect(console.error).toHaveBeenCalledWith(unchangedOutput);
});
test('allows you to specify an ignore glob', async () => {
const ignore = ['src/ignore/thing', 'src/ignore/otherthing'];
const fileGlob = 'src/**/1*.js';
await formatFiles({ _: [fileGlob], ignore });
const globOptions = expect.objectContaining({
ignore: [...ignore, '**/node_modules/**']
});
const callback = expect.any(Function);
expect(globMock).toHaveBeenCalledWith(fileGlob, globOptions, callback);
});
test('wont modify a file if it is eslint ignored', async () => {
await formatFiles({ _: ['src/**/eslintignored*.js'], write: true });
expect(fsMock.readFile).toHaveBeenCalledTimes(1);
expect(fsMock.writeFile).toHaveBeenCalledTimes(1);
expect(fsMock.readFile).toHaveBeenCalledWith(
expect.stringMatching(/applied/),
'utf8',
expect.any(Function)
);
expect(fsMock.writeFile).toHaveBeenCalledWith(
expect.stringMatching(/applied/),
expect.stringMatching(/MOCK_OUTPUT.*?applied/),
expect.any(Function)
);
const ignoredOutput = expect.stringMatching(/success.*1.*file/);
expect(console.error).toHaveBeenCalledWith(ignoredOutput);
});
test('will modify a file if it is eslint ignored with noIgnore', async () => {
await formatFiles({
_: ['src/**/eslintignored*.js'],
write: true,
eslintIgnore: false
});
expect(fsMock.readFile).toHaveBeenCalledTimes(4);
expect(fsMock.writeFile).toHaveBeenCalledTimes(4);
const ignoredOutput = expect.stringMatching(/success.*4.*files/);
expect(console.error).toHaveBeenCalledWith(ignoredOutput);
});
test('wont modify a file if it is prettier ignored', async () => {
await formatFiles({ _: ['src/**/prettierignored*.js'], write: true });
expect(fsMock.readFile).toHaveBeenCalledTimes(1);
expect(fsMock.writeFile).toHaveBeenCalledTimes(1);
expect(fsMock.readFile).toHaveBeenCalledWith(
expect.stringMatching(/applied/),
'utf8',
expect.any(Function)
);
expect(fsMock.writeFile).toHaveBeenCalledWith(
expect.stringMatching(/applied/),
expect.stringMatching(/MOCK_OUTPUT.*?applied/),
expect.any(Function)
);
const ignoredOutput = expect.stringMatching(/success.*1.*file/);
expect(console.error).toHaveBeenCalledWith(ignoredOutput);
});
test('will modify a file if it is prettier ignored with noIgnore', async () => {
await formatFiles({
_: ['src/**/prettierignored*.js'],
write: true,
prettierIgnore: false
});
expect(fsMock.readFile).toHaveBeenCalledTimes(4);
expect(fsMock.writeFile).toHaveBeenCalledTimes(4);
const ignoredOutput = expect.stringMatching(/success.*4.*files/);
expect(console.error).toHaveBeenCalledWith(ignoredOutput);
});
test('will not blow up if an .eslintignore or .prettierignore cannot be found', async () => {
const originalSync = findUpMock.sync;
findUpMock.sync = () => null;
try {
await formatFiles({
_: ['src/**/no-ignore/*.js'],
write: true
});
} finally {
findUpMock.sync = originalSync;
}
});
describe('listDifferent', () => {
test('will list different files', async () => {
await formatFiles({
_: ['src/**/1*.js', 'src/**/no-change*.js'],
listDifferent: true
});
expect(fsMock.readFile).toHaveBeenCalledTimes(7);
expect(fsMock.writeFile).toHaveBeenCalledTimes(0);
const unchangedOutput = expect.stringMatching(/3.*files were.*unchanged/);
const successOutput = expect.stringMatching(/success.*4.*files/);
expect(console.error).toHaveBeenCalledTimes(2);
expect(console.error).toHaveBeenCalledWith(unchangedOutput);
expect(console.error).toHaveBeenCalledWith(successOutput);
const path =
'/Users/fredFlintstone/Developer/top-secret/footless-carriage/';
expect(console.log).toHaveBeenCalledTimes(4);
expect(console.log).toHaveBeenCalledWith(`${path}stop/log.js`);
expect(console.log).toHaveBeenCalledWith(`${path}stop/index.js`);
expect(console.log).toHaveBeenCalledWith(`${path}index.js`);
expect(console.log).toHaveBeenCalledWith(`${path}start.js`);
});
test('will error out when contents did change', async () => {
const fileGlob = 'src/**/1*.js';
await formatFiles({
_: [fileGlob],
listDifferent: true
});
expect(process.exitCode).toBe(1);
});
test('wont error out when contents did not change', async () => {
const fileGlob = 'no-change/*.js';
await formatFiles({
_: [fileGlob],
listDifferent: true
});
expect(process.exitCode).toBe(0);
});
});
describe('eslintConfigPath', () => {
test('will use eslintrc', async () => {
await formatFiles({
_: ['src/**/1*.js'],
eslintConfigPath: '.eslintrc'
});
expect(process.exitCode).toBe(0);
});
});