just-scripts 2.2.2 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,440 +0,0 @@
1
- import * as mockfs from 'mock-fs';
2
- import { encodeArgs, exec, spawn } from 'just-scripts-utils';
3
- import { TaskFunction } from 'just-task';
4
- import { tscTask, tscWatchTask, TscTaskOptions } from '../tscTask';
5
- import { callTaskForTest } from './callTaskForTest';
6
- import { normalizeCmdArgsForTest } from './normalizeCmdArgsForTest';
7
-
8
- // Jest will hoist these before the imports above, so these modules will be mocked first
9
- jest.mock('just-scripts-utils/lib/exec', () => {
10
- const { mockExecFactory } = require('./mockExecFactory');
11
- return mockExecFactory();
12
- });
13
- jest.mock('just-task/lib/logger');
14
-
15
- /**
16
- * Returns the composition of the `tsc.js` Node module in terms `mock-fs` understands, which is necessary for Node's
17
- * module loader to succeed.
18
- */
19
- function mockFsTsc(relativePath?: string) {
20
- // Relative to cwd when the test runs
21
- const relativeRepoRoot = '../..';
22
- const ourRelativePath = relativePath || relativeRepoRoot;
23
-
24
- const mockFsConfig: { [key: string]: any } = {};
25
- mockFsConfig[`${ourRelativePath}/node_modules/typescript/lib/tsc.js`] = 'a file';
26
- mockFsConfig[`${ourRelativePath}/node_modules/typescript/package.json`] = 'a file';
27
- return mockFsConfig;
28
- }
29
-
30
- interface Given {
31
- tscTaskFn: (options?: TscTaskOptions) => TaskFunction;
32
- }
33
-
34
- interface Expected {
35
- execOrSpawnSpy: jest.Mock<any>;
36
- }
37
-
38
- type TaskTestCase = [/* name */ string, Given, Expected];
39
-
40
- describe(`tscTask`, () => {
41
- afterEach(() => {
42
- jest.clearAllMocks();
43
- mockfs.restore();
44
- });
45
-
46
- /**
47
- * Both `tscTask` and `tscWatchTask` should handle test cases similarly.
48
- */
49
- describe.each<TaskTestCase>([
50
- ['tscTask', { tscTaskFn: tscTask }, { execOrSpawnSpy: exec as jest.Mock<any> }],
51
- ['tscWatchTask', { tscTaskFn: tscWatchTask }, { execOrSpawnSpy: spawn as jest.Mock<any> }],
52
- ])(`using '%s' function`, (_name, given, expected) => {
53
- /**
54
- * Testing arguments treatment
55
- */
56
- describe(`testing arguments treatment`, () => {
57
- describe(`with no arguments`, () => {
58
- it(`execs command`, () => {
59
- mockfs({
60
- ...mockFsTsc(),
61
- 'tsconfig.json': 'a file',
62
- });
63
- const task = given.tscTaskFn();
64
- expect.assertions(1);
65
- return callTaskForTest(task).then(() => {
66
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
67
- });
68
- });
69
- });
70
-
71
- describe(`with empty options`, () => {
72
- it(`execs command`, () => {
73
- mockfs({
74
- ...mockFsTsc(),
75
- 'tsconfig.json': 'a file',
76
- });
77
- const givenOptions = {};
78
- const task = given.tscTaskFn(givenOptions);
79
- expect.assertions(1);
80
- return callTaskForTest(task).then(() => {
81
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
82
- });
83
- });
84
- });
85
-
86
- describe(`with some options`, () => {
87
- const givenOptions = { allowJs: true, outDir: 'some/out/path' };
88
- const expectedOptions = { ...givenOptions };
89
-
90
- beforeAll(() => {
91
- mockfs({
92
- ...mockFsTsc(),
93
- 'tsconfig.json': 'a file',
94
- });
95
- const task = given.tscTaskFn(givenOptions);
96
- return callTaskForTest(task);
97
- });
98
-
99
- it(`execs command`, () => {
100
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
101
- });
102
-
103
- it(`treats options arg as immutable`, () => {
104
- expect(givenOptions).toEqual(expectedOptions);
105
- });
106
- });
107
- });
108
-
109
- /**
110
- * Testing various options and command output
111
- */
112
- describe(`with no arguments`, () => {
113
- it(`execs expected command`, () => {
114
- mockfs({
115
- ...mockFsTsc(),
116
- 'tsconfig.json': 'a file',
117
- });
118
- const task = given.tscTaskFn();
119
- expect.assertions(3);
120
- return callTaskForTest(task).then(() => {
121
- // Restore mockfs so snapshots work
122
- mockfs.restore();
123
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
124
- // Inspect the call to `encodeArgs` since it is easier to strip out repo-specific path values.
125
- expect(encodeArgs).toHaveBeenCalled();
126
- const actualCmdArgs = normalizeCmdArgsForTest((encodeArgs as jest.Mock<any>).mock.calls[0][0]);
127
- expect(actualCmdArgs).toMatchSnapshot();
128
- });
129
- });
130
- });
131
-
132
- describe(`with empty options`, () => {
133
- it(`execs expected command`, () => {
134
- mockfs({
135
- ...mockFsTsc(),
136
- 'tsconfig.json': 'a file',
137
- });
138
- const givenOptions = {};
139
- const task = given.tscTaskFn(givenOptions);
140
- expect.assertions(3);
141
- return callTaskForTest(task).then(() => {
142
- // Restore mockfs so snapshots work
143
- mockfs.restore();
144
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
145
- // Inspect the call to `encodeArgs` since it is easier to strip out repo-specific path values.
146
- expect(encodeArgs).toHaveBeenCalled();
147
- const actualCmdArgs = normalizeCmdArgsForTest((encodeArgs as jest.Mock<any>).mock.calls[0][0]);
148
- expect(actualCmdArgs).toMatchSnapshot();
149
- });
150
- });
151
- });
152
-
153
- describe(`with default options and tsconfig.json does not exist at package root`, () => {
154
- it(`does not exec command`, () => {
155
- mockfs({
156
- ...mockFsTsc(),
157
- });
158
- const task = given.tscTaskFn();
159
- expect.assertions(1);
160
- return callTaskForTest(task).then(() => {
161
- expect(expected.execOrSpawnSpy).not.toHaveBeenCalled();
162
- });
163
- });
164
- });
165
-
166
- describe(`with 'project' option where 'project' is custom path and tsconfig.json exists`, () => {
167
- it(`execs expected command`, () => {
168
- mockfs({
169
- ...mockFsTsc(),
170
- 'a/custom/path': {
171
- 'tsconfig.json': 'a file',
172
- },
173
- });
174
- const givenOptions = { project: 'a/custom/path/tsconfig.json' };
175
- const task = given.tscTaskFn(givenOptions);
176
- expect.assertions(3);
177
- return callTaskForTest(task).then(() => {
178
- // Restore mockfs so snapshots work
179
- mockfs.restore();
180
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
181
- // Inspect the call to `encodeArgs` since it is easier to strip out repo-specific path values.
182
- expect(encodeArgs).toHaveBeenCalled();
183
- const actualCmdArgs = normalizeCmdArgsForTest((encodeArgs as jest.Mock<any>).mock.calls[0][0]);
184
- expect(actualCmdArgs).toMatchSnapshot();
185
- });
186
- });
187
- });
188
-
189
- describe(`with 'project' option where 'project' is custom path and tsconfig.json does not exist`, () => {
190
- it(`does not exec command`, () => {
191
- mockfs({
192
- ...mockFsTsc(),
193
- });
194
- const givenOptions = { project: 'a/custom/path/tsconfig.json' };
195
- const task = given.tscTaskFn(givenOptions);
196
- expect.assertions(1);
197
- return callTaskForTest(task).then(() => {
198
- expect(expected.execOrSpawnSpy).not.toHaveBeenCalled();
199
- });
200
- });
201
- });
202
-
203
- describe(`with 'build' option where 'build' is custom path and tsconfig.json exists`, () => {
204
- it(`execs expected command`, () => {
205
- mockfs({
206
- ...mockFsTsc(),
207
- 'a/custom/path': {
208
- 'tsconfig.json': 'a file',
209
- },
210
- });
211
- const givenOptions = { build: 'a/custom/path/tsconfig.json' };
212
- const task = given.tscTaskFn(givenOptions);
213
- expect.assertions(3);
214
- return callTaskForTest(task).then(() => {
215
- // Restore mockfs so snapshots work
216
- mockfs.restore();
217
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
218
- // Inspect the call to `encodeArgs` since it is easier to strip out repo-specific path values.
219
- expect(encodeArgs).toHaveBeenCalled();
220
- const actualCmdArgs = normalizeCmdArgsForTest((encodeArgs as jest.Mock<any>).mock.calls[0][0]);
221
- expect(actualCmdArgs).toMatchSnapshot();
222
- });
223
- });
224
- });
225
-
226
- describe(`with 'build' option where 'build' is custom path and tsconfig.json does not exist`, () => {
227
- it(`does not exec command`, () => {
228
- mockfs({
229
- ...mockFsTsc(),
230
- });
231
- const givenOptions = { build: 'a/custom/path/tsconfig.json' };
232
- const task = given.tscTaskFn(givenOptions);
233
- expect.assertions(1);
234
- return callTaskForTest(task).then(() => {
235
- expect(expected.execOrSpawnSpy).not.toHaveBeenCalled();
236
- });
237
- });
238
- });
239
-
240
- describe(`with 'build' option where 'build' is multiple paths and they all exist`, () => {
241
- it(`execs expected command`, () => {
242
- mockfs({
243
- ...mockFsTsc(),
244
- 'project/a': {
245
- 'tsconfig.json': 'a file',
246
- },
247
- 'project/b': {
248
- 'tsconfig.json': 'a file',
249
- },
250
- 'project/c': {
251
- 'tsconfig.json': 'a file',
252
- },
253
- });
254
- const givenOptions = {
255
- build: ['project/a/tsconfig.json', 'project/b/tsconfig.json', 'project/c/tsconfig.json'],
256
- };
257
- const task = given.tscTaskFn(givenOptions);
258
- expect.assertions(3);
259
- return callTaskForTest(task).then(() => {
260
- // Restore mockfs so snapshots work
261
- mockfs.restore();
262
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
263
- // Inspect the call to `encodeArgs` since it is easier to strip out repo-specific path values.
264
- expect(encodeArgs).toHaveBeenCalled();
265
- const actualCmdArgs = normalizeCmdArgsForTest((encodeArgs as jest.Mock<any>).mock.calls[0][0]);
266
- expect(actualCmdArgs).toMatchSnapshot();
267
- });
268
- });
269
- });
270
-
271
- describe(`with 'build' option where 'build' is multiple paths and some do not exist`, () => {
272
- it(`does not exec command`, () => {
273
- mockfs({
274
- ...mockFsTsc(),
275
- 'project/a': {
276
- 'tsconfig.json': 'a file',
277
- },
278
- });
279
- const givenOptions = {
280
- build: ['project/a/tsconfig.json', 'project/b/tsconfig.json', 'project/c/tsconfig.json'],
281
- };
282
- const task = given.tscTaskFn(givenOptions);
283
- expect.assertions(1);
284
- return callTaskForTest(task).then(() => {
285
- expect(expected.execOrSpawnSpy).not.toHaveBeenCalled();
286
- });
287
- });
288
- });
289
-
290
- describe(`with string value option`, () => {
291
- it(`execs expected command`, () => {
292
- mockfs({
293
- ...mockFsTsc(),
294
- 'tsconfig.json': 'a file',
295
- });
296
- const givenOptions = { module: 'ESNext' };
297
- const task = given.tscTaskFn(givenOptions);
298
- expect.assertions(3);
299
- return callTaskForTest(task).then(() => {
300
- // Restore mockfs so snapshots work
301
- mockfs.restore();
302
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
303
- // Inspect the call to `encodeArgs` since it is easier to strip out repo-specific path values.
304
- expect(encodeArgs).toHaveBeenCalled();
305
- const actualCmdArgs = normalizeCmdArgsForTest((encodeArgs as jest.Mock<any>).mock.calls[0][0]);
306
- expect(actualCmdArgs).toMatchSnapshot();
307
- });
308
- });
309
- });
310
-
311
- describe(`with string array option`, () => {
312
- it(`execs expected command`, () => {
313
- mockfs({
314
- ...mockFsTsc(),
315
- 'tsconfig.json': 'a file',
316
- });
317
- const givenOptions = { lib: ['es6', 'dom', 'esnext.intl'] };
318
- const task = given.tscTaskFn(givenOptions);
319
- expect.assertions(3);
320
- return callTaskForTest(task).then(() => {
321
- // Restore mockfs so snapshots work
322
- mockfs.restore();
323
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
324
- // Inspect the call to `encodeArgs` since it is easier to strip out repo-specific path values.
325
- expect(encodeArgs).toHaveBeenCalled();
326
- const actualCmdArgs = normalizeCmdArgsForTest((encodeArgs as jest.Mock<any>).mock.calls[0][0]);
327
- expect(actualCmdArgs).toMatchSnapshot();
328
- });
329
- });
330
- });
331
-
332
- describe(`with a boolean 'true' switch`, () => {
333
- it(`execs expected command`, () => {
334
- mockfs({
335
- ...mockFsTsc(),
336
- 'tsconfig.json': 'a file',
337
- });
338
- const givenOptions = { allowJs: true };
339
- const task = given.tscTaskFn(givenOptions);
340
- expect.assertions(3);
341
- return callTaskForTest(task).then(() => {
342
- // Restore mockfs so snapshots work
343
- mockfs.restore();
344
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
345
- // Inspect the call to `encodeArgs` since it is easier to strip out repo-specific path values.
346
- expect(encodeArgs).toHaveBeenCalled();
347
- const actualCmdArgs = normalizeCmdArgsForTest((encodeArgs as jest.Mock<any>).mock.calls[0][0]);
348
- expect(actualCmdArgs).toMatchSnapshot();
349
- });
350
- });
351
- });
352
-
353
- describe(`with a boolean 'false' switch`, () => {
354
- it(`execs expected command`, () => {
355
- mockfs({
356
- ...mockFsTsc(),
357
- 'tsconfig.json': 'a file',
358
- });
359
- const givenOptions = { allowJs: false };
360
- const task = given.tscTaskFn(givenOptions);
361
- expect.assertions(3);
362
- return callTaskForTest(task).then(() => {
363
- // Restore mockfs so snapshots work
364
- mockfs.restore();
365
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
366
- // Inspect the call to `encodeArgs` since it is easier to strip out repo-specific path values.
367
- expect(encodeArgs).toHaveBeenCalled();
368
- const actualCmdArgs = normalizeCmdArgsForTest((encodeArgs as jest.Mock<any>).mock.calls[0][0]);
369
- expect(actualCmdArgs).toMatchSnapshot();
370
- });
371
- });
372
- });
373
-
374
- describe(`with a combination of switches`, () => {
375
- it(`execs expected command`, () => {
376
- mockfs({
377
- ...mockFsTsc(),
378
- 'tsconfig.json': 'a file',
379
- });
380
- const givenOptions = { allowJs: true, build: 'tsconfig.json', outDir: 'some/out/path' };
381
- const task = given.tscTaskFn(givenOptions);
382
- expect.assertions(3);
383
- return callTaskForTest(task).then(() => {
384
- // Restore mockfs so snapshots work
385
- mockfs.restore();
386
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
387
- // Inspect the call to `encodeArgs` since it is easier to strip out repo-specific path values.
388
- expect(encodeArgs).toHaveBeenCalled();
389
- const actualCmdArgs = normalizeCmdArgsForTest((encodeArgs as jest.Mock<any>).mock.calls[0][0]);
390
- expect(actualCmdArgs).toMatchSnapshot();
391
- });
392
- });
393
- });
394
-
395
- /**
396
- * Testing repo layout
397
- */
398
- describe(`testing repo layout`, () => {
399
- describe(`where repo has TypeScript installed`, () => {
400
- it(`execs command`, () => {
401
- mockfs({
402
- ...mockFsTsc(),
403
- 'tsconfig.json': 'a file',
404
- });
405
- const task = given.tscTaskFn();
406
- expect.assertions(1);
407
- return callTaskForTest(task).then(() => {
408
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
409
- });
410
- });
411
- });
412
-
413
- describe(`where package has TypeScript installed`, () => {
414
- it(`execs command`, () => {
415
- mockfs({
416
- ...mockFsTsc('.'),
417
- 'tsconfig.json': 'a file',
418
- });
419
- const task = given.tscTaskFn();
420
- expect.assertions(1);
421
- return callTaskForTest(task).then(() => {
422
- expect(expected.execOrSpawnSpy).toHaveBeenCalled();
423
- });
424
- });
425
- });
426
-
427
- describe(`where repo and package do not have TypeScript installed`, () => {
428
- it(`returns error`, () => {
429
- mockfs({
430
- 'tsconfig.json': 'a file',
431
- });
432
- expect.assertions(1);
433
- expect(() => {
434
- given.tscTaskFn();
435
- }).toThrow('cannot find tsc');
436
- });
437
- });
438
- });
439
- });
440
- });