forked from jest-community/vscode-jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.test.ts
501 lines (456 loc) · 26.1 KB
/
helpers.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
jest.unmock('../src/helpers');
jest.unmock('../src/Settings');
jest.unmock('./test-helper');
const mockExistsSync = jest.fn();
const mockReadFileSync = jest.fn();
const mockWriteFileSync = jest.fn();
const mockMkdirSync = jest.fn();
jest.mock('fs', () => ({
existsSync: mockExistsSync,
readFileSync: mockReadFileSync,
writeFileSync: mockWriteFileSync,
mkdirSync: mockMkdirSync,
}));
const mockPlatform = jest.fn();
jest.mock('os', () => ({ platform: mockPlatform }));
import { resolve } from 'path';
import * as vscode from 'vscode';
import * as os from 'os';
import * as path from 'path';
import {
isCreateReactAppTestCommand,
nodeBinExtension,
cleanAnsi,
prepareIconFile,
testIdString,
escapeRegExp,
removeSurroundingQuote,
toFilePath,
toLowerCaseDriveLetter,
toUpperCaseDriveLetter,
shellQuote,
toErrorString,
getPackageJson,
getTestCommand,
getDefaultJestCommand,
parseCmdLine,
toAbsoluteRootPath,
} from '../src/helpers';
import * as helper from '../src/helpers';
import { makeUri, makeWorkspaceFolder } from './test-helper';
// Manually (forcefully) set the executable's file extension to test its addition independently of the operating system.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(nodeBinExtension as string) = '.TEST';
describe('ModuleHelpers', () => {
describe('nodeBinExtension', () => {
// Since `nodeBinExtension` is a variable, we have to reload the module in order to re-evaluate it.
it('should return an empty string on Linux', () => {
jest.resetModules();
mockPlatform.mockReturnValueOnce('linux');
// eslint-disable-next-line @typescript-eslint/no-var-requires
expect(require('../src/helpers').nodeBinExtension).toBe('');
});
it('should equal ".cmd" on Windows', () => {
jest.resetModules();
mockPlatform.mockReturnValueOnce('win32');
// eslint-disable-next-line @typescript-eslint/no-var-requires
expect(require('../src/helpers').nodeBinExtension).toBe('.cmd');
});
});
describe('isCreateReactAppTestCommand', () => {
it('should return true for CRA', () => {
expect(isCreateReactAppTestCommand('react-scripts test --env=jsdom')).toBe(true);
});
it('should return true for CRA with cross-env', () => {
expect(isCreateReactAppTestCommand('cross-env CI=true react-scripts test --env=jsdom')).toBe(
true
);
});
it('should return false for other scripts', () => {
expect(isCreateReactAppTestCommand(undefined)).toBe(false);
expect(isCreateReactAppTestCommand('custom-script')).toBe(false);
});
it('should return false for other scripts with cross-env', () => {
expect(isCreateReactAppTestCommand('cross-env CI=true custom-script')).toBe(false);
});
});
describe('cleanAnsi', () => {
it('removes ANSI characters from string', () => {
const ansiString =
'\u001b[36m<body>\u001b[39m \u001b[36m<div>\u001b[39m \u001b[36m<div\u001b[39m \u001b[33mclass\u001b[39m=\u001b[32m"root"\u001b[39m \u001b[36m>\u001b[39m \u001b[0mLearn React\u001b[0m \u001b[36m</div>\u001b[39m \u001b[36m</div>\u001b[39m\u001b[36m</body>\u001b[39m';
expect(cleanAnsi(ansiString)).toBe(
'<body> <div> <div class="root" > Learn React </div> </div></body>'
);
});
});
describe('prepareIconFile', () => {
const context = {
asAbsolutePath: (name: string) => name,
} as vscode.ExtensionContext;
beforeEach(() => {
jest.resetAllMocks();
(mockReadFileSync as jest.Mock).mockReturnValue(Buffer.from(''));
(mockExistsSync as jest.Mock).mockReturnValue(true);
});
it('is creating icon file from source file if it does not exist', () => {
(mockExistsSync as jest.Mock).mockReturnValue(false);
prepareIconFile(context, 'state', '<svg />');
expect(mockWriteFileSync).toHaveBeenCalledWith(
path.join('generated-icons', 'state.svg'),
'<svg />'
);
expect(mockMkdirSync).toHaveBeenCalled();
});
it('does not write file if it exists and is the same', () => {
(mockReadFileSync as jest.Mock).mockReturnValue(Buffer.from('<svg />'));
prepareIconFile(context, 'state', '<svg />');
expect(mockWriteFileSync).toHaveBeenCalledTimes(0);
});
it('can replace fill color', () => {
(mockReadFileSync as jest.Mock).mockReturnValue(
Buffer.from('<svg fill="currentColor"></svg>')
);
(mockExistsSync as jest.Mock).mockReturnValue(false);
prepareIconFile(context, 'default', '<svg fill="currentColor"></svg>');
expect((mockWriteFileSync as jest.Mock).mock.calls[0][1]).toBe(
'<svg fill="currentColor"></svg>'
);
prepareIconFile(context, 'gray', '<svg fill="currentColor"></svg>', '#8C8C8C');
expect((mockWriteFileSync as jest.Mock).mock.calls[1][1]).toBe('<svg fill="#8C8C8C"></svg>');
prepareIconFile(context, 'red', '<svg fill="currentColor"></svg>', 'red');
expect((mockWriteFileSync as jest.Mock).mock.calls[2][1]).toBe('<svg fill="red"></svg>');
});
});
});
describe('escapeRegExp', () => {
it.each`
str | expected
${'no special char'} | ${'no special char'}
${'with (a)'} | ${'with \\(a\\)'}
${'with {} and $sign'} | ${'with \\{\\} and \\$sign'}
${'with []'} | ${'with \\[\\]'}
${{ value: 'with []', exactMatch: true }} | ${'with \\[\\]$'}
${{ value: 'with []', exactMatch: false }} | ${'with \\[\\]'}
${{ value: 'with []', isRegExp: true }} | ${'with []'}
${{ value: 'with []', isRegExp: false }} | ${'with \\[\\]'}
`('escapeRegExp: $str', ({ str, expected }) => {
expect(escapeRegExp(str)).toEqual(expected);
});
});
describe('testIdString', () => {
it.each`
type | id | expected
${'display'} | ${{ title: 'test', ancestorTitles: [] }} | ${'test'}
${'display-reverse'} | ${{ title: 'test', ancestorTitles: [] }} | ${'test'}
${'full-name'} | ${{ title: 'test', ancestorTitles: [] }} | ${'test'}
${'display'} | ${{ title: 'regexp (a) $x/y', ancestorTitles: [] }} | ${'regexp (a) $x/y'}
${'display-reverse'} | ${{ title: 'regexp (a) $x/y', ancestorTitles: [] }} | ${'regexp (a) $x/y'}
${'full-name'} | ${{ title: 'regexp (a) $x/y', ancestorTitles: [] }} | ${'regexp (a) $x/y'}
${'display'} | ${{ title: 'test', ancestorTitles: ['d-1', 'd-1-1'] }} | ${'d-1 > d-1-1 > test'}
${'display-reverse'} | ${{ title: 'test', ancestorTitles: ['d-1', 'd-1-1'] }} | ${'test < d-1-1 < d-1'}
${'full-name'} | ${{ title: 'test', ancestorTitles: ['d-1', 'd-1-1'] }} | ${'d-1 d-1-1 test'}
${'full-name'} | ${{ title: 'regexp ($a)', ancestorTitles: ['d-1', 'd-1-1'] }} | ${'d-1 d-1-1 regexp ($a)'}
`('$type: $expected', ({ type, id, expected }) => {
expect(testIdString(type, id)).toEqual(expected);
});
});
describe('removeSurroundingQuote', () => {
it.each`
str | expected
${'no quote'} | ${'no quote'}
${'"double quote"'} | ${'double quote'}
${"'single quote'"} | ${'single quote'}
${"''single single quote''"} | ${"'single single quote'"}
`('can remove surrounding quotes from $str', ({ str, expected }) => {
expect(removeSurroundingQuote(str)).toEqual(expected);
});
});
describe('toFilePath', () => {
it.each`
path | expected
${'/a/b/c'} | ${'/a/b/c'}
${'C:/a/b/c.js'} | ${'C:/a/b/c.js'}
${'c:/a/b/c.js'} | ${'c:/a/b/c.js'}
${'z:\\a\\b\\c.js'} | ${'Z:\\a\\b\\c.js'}
${'\\a\\b\\c.js'} | ${'\\a\\b\\c.js'}
${''} | ${''}
`('escape $path => $expected', ({ path, expected }) => {
expect(toFilePath(path)).toEqual(expected);
});
});
describe('toLowerCaseDriveLetter', () => {
it.each`
filePath | expected
${'C:\\path\\file.ext'} | ${'c:\\path\\file.ext'}
${'c:\\path\\file.ext'} | ${undefined}
${'c:/path/file.ext'} | ${undefined}
${'/path/file.ext'} | ${undefined}
`('$filePath => $expected', ({ filePath, expected }) => {
expect(toLowerCaseDriveLetter(filePath)).toBe(expected);
});
});
describe('toUpperCaseDriveLetter', () => {
it.each`
filePath | expected
${'C:\\path\\file.ext'} | ${undefined}
${'c:\\path\\file.ext'} | ${'C:\\path\\file.ext'}
${'c:/path/file.ext'} | ${undefined}
${'/path/file.ext'} | ${undefined}
`('$filePath => $expected', ({ filePath, expected }) => {
expect(toUpperCaseDriveLetter(filePath)).toBe(expected);
});
});
describe('shellQuote', () => {
it.each`
case | platform | shell | str | expected
${1} | ${'win32'} | ${undefined} | ${'plain text'} | ${'"plain text"'}
${2} | ${'linux'} | ${undefined} | ${'plain text'} | ${'plain\\ text'}
${3} | ${'win32'} | ${'powershell'} | ${"with 'single quote'"} | ${"'with ''single quote'''"}
${4} | ${'win32'} | ${'cmd.exe'} | ${"with 'single quote'"} | ${'"with \'single quote\'"'}
${5} | ${'linux'} | ${'/bin/bash'} | ${"with 'single quote'"} | ${"with\\ \\'single\\ quote\\'"}
${6} | ${'darwin'} | ${'/bin/zsh'} | ${"with 'single quote'"} | ${"with\\ \\'single\\ quote\\'"}
${7} | ${'darwin'} | ${{ path: '/bin/zsh', args: ['-l'] }} | ${"with 'single quote'"} | ${"with\\ \\'single\\ quote\\'"}
${8} | ${'win32'} | ${undefined} | ${"with 'single quote'"} | ${'"with \'single quote\'"'}
${9} | ${'linux'} | ${undefined} | ${"with 'single quote'"} | ${"with\\ \\'single\\ quote\\'"}
${10} | ${'win32'} | ${'powershell'} | ${'with "double quote"'} | ${'\'with ""double quote""\''}
${11} | ${'win32'} | ${'cmd.exe'} | ${'with "double quote"'} | ${'"with ""double quote"""'}
${12} | ${'linux'} | ${'bash'} | ${'with "double quote"'} | ${'with\\ \\"double\\ quote\\"'}
${13} | ${'win32'} | ${'powershell'} | ${'with $name.txt'} | ${"'with $name.txt'"}
${14} | ${'win32'} | ${'cmd.exe'} | ${'with $name.txt'} | ${'"with $name.txt"'}
${15} | ${'linux'} | ${'bash'} | ${'with $name.txt'} | ${'with\\ \\$name.txt'}
${16} | ${'win32'} | ${'powershell'} | ${'with \\$name\\.txt'} | ${"'with \\$name\\.txt'"}
${17} | ${'win32'} | ${'cmd.exe'} | ${'with \\$name\\.txt'} | ${'"with \\$name\\.txt"'}
${18} | ${'linux'} | ${'bash'} | ${'with \\$name\\.txt'} | ${'with\\ \\\\\\$name\\\\.txt'}
${19} | ${'linux'} | ${{ path: '/bin/sh', args: ['--login'] }} | ${'with \\$name\\.txt'} | ${'with\\ \\\\\\$name\\\\.txt'}
${20} | ${'win32'} | ${'powershell'} | ${''} | ${"''"}
${21} | ${'win32'} | ${undefined} | ${''} | ${'""'}
${22} | ${'darwin'} | ${undefined} | ${''} | ${'""'}
${23} | ${'win32'} | ${'powershell'} | ${'with \\ and \\\\'} | ${"'with \\ and \\\\'\\"}
${24} | ${'win32'} | ${undefined} | ${'with \\ and \\\\'} | ${'"with \\ and \\\\"'}
${25} | ${'linux'} | ${undefined} | ${'with \\ and \\\\'} | ${'with\\ \\\\\\ and\\ \\\\\\\\'}
${26} | ${'win32'} | ${'powershell'} | ${'something\\'} | ${"'something\\'\\"}
${27} | ${'win32'} | ${undefined} | ${'something\\'} | ${'"something\\"'}
${28} | ${'darwin'} | ${undefined} | ${'something\\'} | ${'something\\\\'}
${29} | ${'win32'} | ${'powershell'} | ${'with `backtick'} | ${"'with `backtick'"}
${30} | ${'win32'} | ${undefined} | ${'with `backtick'} | ${'"with `backtick"'}
${31} | ${'darwin'} | ${undefined} | ${'with `backtick'} | ${'with\\ \\`backtick'}
`(
'case $case: can quote "$str" for $shell on $platform',
({ platform, shell, str, expected }) => {
jest.resetAllMocks();
mockPlatform.mockReturnValueOnce(platform);
expect(shellQuote(str, shell)).toEqual(expected);
}
);
});
it.each`
name | e | matchString
${'undefined'} | ${undefined} | ${undefined}
${'string'} | ${'regular error'} | ${undefined}
${'an Error object'} | ${new Error('test error')} | ${'Error: test error'}
${'arbitrary object'} | ${{ text: 'anything', value: 1 }} | ${undefined}
`('toErrorString: $name', ({ e, matchString }) => {
if (matchString) {
expect(toErrorString(e)).toEqual(expect.stringContaining(matchString));
} else {
expect(toErrorString(e)).toMatchSnapshot();
}
});
describe('get info from Package.json', () => {
const packageWithTest = {
scripts: {
test: 'react-scripts test',
},
};
describe('getPackageJson', () => {
it('can read package.json from file system', () => {
mockReadFileSync.mockReturnValueOnce(JSON.stringify(packageWithTest));
expect(getPackageJson('root')).toEqual(packageWithTest);
expect(mockReadFileSync).toHaveBeenCalledWith(
resolve('root', 'package.json'),
expect.anything()
);
});
it('if package.json does not exist, return undefined', () => {
mockReadFileSync.mockImplementation(() => {
throw new Error('does not exist');
});
expect(getPackageJson('root')).toBeUndefined();
});
});
describe('getTestCommand', () => {
it('can get test script from package.json', () => {
mockReadFileSync.mockReturnValueOnce(JSON.stringify(packageWithTest));
expect(getTestCommand('root')).toEqual(packageWithTest.scripts.test);
});
it.each`
case | impl
${'no package.json'} | ${() => {
throw new Error('does not exist');
}}
${'no scripts'} | ${() => ({})}
${'invalid package.json'} | ${() => 'invalid package.json'}
`('if error, returns undefined: $case', ({ impl }) => {
mockReadFileSync.mockImplementation(impl);
expect(getTestCommand('root')).toBeUndefined();
});
});
});
describe('getDefaultJestCommand', () => {
it.each`
case | packageTestScript | pmLockFile | binary | expected
${1} | ${'react-scripts test'} | ${'yarn.lock'} | ${'react-scripts'} | ${'yarn test'}
${2} | ${'react-scripts test'} | ${'package-lock.json'} | ${'react-scripts'} | ${'npm test --'}
${3} | ${'some other test'} | ${'yarn.lock'} | ${'jest'} | ${'binary'}
${4} | ${'some other test'} | ${'package-lock.json'} | ${'react-native-scripts'} | ${'npm test --'}
${5} | ${'some other test'} | ${'package-lock.json'} | ${undefined} | ${undefined}
${6} | ${'jest'} | ${'package-lock.json'} | ${undefined} | ${'npm test --'}
${7} | ${undefined} | ${'package-lock.json'} | ${'jest'} | ${'binary'}
${8} | ${'something with jest --someFlag'} | ${undefined} | ${'jest'} | ${'npm test --'}
`('case $case => $expected', ({ packageTestScript, pmLockFile, binary, expected }) => {
const packageFile = {
scripts: {
test: packageTestScript,
},
};
mockReadFileSync.mockReturnValueOnce(JSON.stringify(packageFile));
const binaryFile = `${binary}.TEST`;
mockExistsSync.mockImplementation((p) => p.endsWith(pmLockFile) || p.endsWith(binaryFile));
const defaultCmd = getDefaultJestCommand();
if (expected === 'binary') {
const pattern = new RegExp(`${binaryFile}"$`);
expect(defaultCmd).toMatch(pattern);
} else {
expect(defaultCmd).toEqual(expected);
}
});
});
describe('platform-specific tests', () => {
const canRunTest = (isWin32: boolean) =>
(isWin32 && os.platform() === 'win32') || (!isWin32 && os.platform() !== 'win32');
beforeAll(() => {
mockPlatform.mockImplementation(jest.requireActual('os').platform);
});
describe('parseCmdLine', () => {
it.each`
isWin32 | cmdLine | expected
${false} | ${'jest'} | ${{ cmd: 'jest', args: [], program: '${workspaceFolder}/jest' }}
${false} | ${'./node_modules/.bin/jest'} | ${{ cmd: 'node_modules/.bin/jest', args: [], program: '${workspaceFolder}/node_modules/.bin/jest' }}
${false} | ${'./node_modules/.bin/..//jest'} | ${{ cmd: 'node_modules/jest', args: [], program: '${workspaceFolder}/node_modules/jest' }}
${false} | ${'../jest --config ../jest-config.json'} | ${{ cmd: '../jest', args: ['--config', '../jest-config.json'], program: '${workspaceFolder}/../jest' }}
${false} | ${'../jest --config "../jest-config.json"'} | ${{ cmd: '../jest', args: ['--config', '"../jest-config.json"'], program: '${workspaceFolder}/../jest' }}
${false} | ${'../jest --config=../jest-config.json'} | ${{ cmd: '../jest', args: ['--config=../jest-config.json'], program: '${workspaceFolder}/../jest' }}
${false} | ${'../jest --config="../jest-config.json"'} | ${{ cmd: '../jest', args: ['--config=', '"../jest-config.json"'], program: '${workspaceFolder}/../jest' }}
${false} | ${'../jest --config "a dir/jest-config.json" --coverage'} | ${{ cmd: '../jest', args: ['--config', '"a dir/jest-config.json"', '--coverage'], program: '${workspaceFolder}/../jest' }}
${false} | ${'jest --config "../dir with space/jest-config.json"'} | ${{ cmd: 'jest', args: ['--config', '"../dir with space/jest-config.json"'], program: '${workspaceFolder}/jest' }}
${false} | ${'/absolute/jest --runInBand'} | ${{ cmd: '/absolute/jest', args: ['--runInBand'], program: '/absolute/jest' }}
${false} | ${'"dir with space/jest" --arg1=1 --arg2 2 "some string"'} | ${{ cmd: 'dir with space/jest', args: ['--arg1=1', '--arg2', '2', '"some string"'], program: '${workspaceFolder}/dir with space/jest' }}
${false} | ${'"/dir with space/jest" --arg1=1 --arg2 2 "some string"'} | ${{ cmd: '/dir with space/jest', args: ['--arg1=1', '--arg2', '2', '"some string"'], program: '/dir with space/jest' }}
${false} | ${"'/dir with space/jest' --arg1=1 --arg2 2 'some string'"} | ${{ cmd: '/dir with space/jest', args: ['--arg1=1', '--arg2', '2', "'some string'"], program: '/dir with space/jest' }}
${false} | ${'jest --arg1 "escaped \\"this\\" string" --arg2 2'} | ${{ cmd: 'jest', args: ['--arg1', '"escaped \\"this\\" string"', '--arg2', '2'], program: '${workspaceFolder}/jest' }}
${true} | ${'.\\node_modules\\.bin\\jest'} | ${{ cmd: 'node_modules\\.bin\\jest', args: [], program: '${workspaceFolder}\\node_modules\\.bin\\jest' }}
${true} | ${'..\\jest --config="..\\jest-config.json"'} | ${{ cmd: '..\\jest', args: ['--config=', '"..\\jest-config.json"'], program: '${workspaceFolder}\\..\\jest' }}
${true} | ${'jest --config "..\\dir with space\\jest-config.json"'} | ${{ cmd: 'jest', args: ['--config', '"..\\dir with space\\jest-config.json"'], program: '${workspaceFolder}\\jest' }}
${true} | ${'\\absolute\\jest --runInBand'} | ${{ cmd: '\\absolute\\jest', args: ['--runInBand'], program: '\\absolute\\jest' }}
${true} | ${'"\\dir with space\\jest" --arg1=1 --arg2 2 "some string"'} | ${{ cmd: '\\dir with space\\jest', args: ['--arg1=1', '--arg2', '2', '"some string"'], program: '\\dir with space\\jest' }}
${true} | ${'c:\\jest --arg1 "escaped \\"this\\" string" --arg2 2'} | ${{ cmd: 'c:\\jest', args: ['--arg1', '"escaped \\"this\\" string"', '--arg2', '2'], program: 'c:\\jest' }}
`('$cmdLine', ({ cmdLine, expected, isWin32 }) => {
if (!canRunTest(isWin32)) {
return;
}
const [actualCmd, ...actualArgs] = parseCmdLine(cmdLine);
expect(actualCmd).toEqual(expected.cmd);
expect(actualArgs).toEqual(expected.args);
});
describe('will remove surrounding quotes', () => {
it.each`
command | expected
${'cleanCmd'} | ${'cleanCmd'}
${'"with double quote"'} | ${'with double quote'}
${"'with single quote'"} | ${'with single quote'}
${'"with quotes "in the middle" is fine"'} | ${'with quotes "in the middle" is fine'}
${"'with quotes 'in the middle' is fine'"} | ${"with quotes 'in the middle' is fine"}
${'with quotes "in the middle" is fine'} | ${'with quotes "in the middle" is fine'}
${'with escape "in the \'middle\'" is fine'} | ${'with escape "in the \'middle\'" is fine'}
${'with escape "in the "middle"" is fine'} | ${'with escape "in the "middle"" is fine'}
${"'c:\\quoted root\\window\\command'"} | ${'c:\\quoted root\\window\\command'}
${"'\\quoted root\\window\\command'"} | ${'\\quoted root\\window\\command'}
`(
'uses cleanupCommand to remove surrounding quotes for command: $command',
({ command, expected }) => {
expect(removeSurroundingQuote(command)).toEqual(expected);
}
);
});
});
});
describe('toAbsoluteRootPath', () => {
const ws = makeWorkspaceFolder('ws1');
ws.uri.fsPath = path.join(path.sep, 'ws1');
it.each`
case | rootPath | expected
${1} | ${''} | ${ws.uri.fsPath}
${2} | ${'folder'} | ${path.resolve(ws.uri.fsPath, 'folder')}
${3} | ${path.join(path.sep, 'folder')} | ${path.join(path.sep, 'folder')}
`('case $case', ({ rootPath, expected }) => {
expect(toAbsoluteRootPath(ws, rootPath)).toEqual(expected);
});
});
describe('getValidJestCommand', () => {
const workspace = makeWorkspaceFolder('test-folder');
const ws1 = makeUri('', 'test-folder', 'w1', 'child');
const ws2 = makeUri('', 'test-folder', 'w2');
const mockWorkspaceManager: any = {
getFoldersFromFilesystem: jest.fn(),
};
beforeEach(() => {
jest.clearAllMocks();
});
it.each`
case | defaultJestCommands | uris | validSettings
${'valid default'} | ${['jest']} | ${undefined} | ${[{ jestCommandLine: 'jest', rootPath: workspace.uri.fsPath }]}
${'valid workspace'} | ${[undefined, 'jest2']} | ${[ws1]} | ${[{ rootPath: ws1.fsPath, jestCommandLine: 'jest2' }]}
${'same rootPath'} | ${[undefined, undefined]} | ${[ws2]} | ${undefined}
${'no workspace'} | ${[undefined]} | ${[]} | ${undefined}
${'multiple workspaces'} | ${[undefined, 'jest1', 'jest2']} | ${[ws1, ws2]} | ${[{ rootPath: ws1.fsPath, jestCommandLine: 'jest1' }, { rootPath: ws2.fsPath, jestCommandLine: 'jest2' }]}
`('$case', async ({ defaultJestCommands, uris, validSettings }) => {
const defaultJestCommandSpy = jest.spyOn(helper, 'getDefaultJestCommand');
defaultJestCommands.forEach((cmd) => {
defaultJestCommandSpy.mockReturnValueOnce(cmd);
});
mockWorkspaceManager.getFoldersFromFilesystem.mockReturnValue(Promise.resolve(uris));
const result = await helper.getValidJestCommand(workspace, mockWorkspaceManager);
expect(result.uris).toEqual(uris);
if (!validSettings) {
expect(result.validSettings).toHaveLength(0);
} else {
expect(result.validSettings).toHaveLength(validSettings.length);
validSettings.forEach((setting) => expect(result.validSettings).toContainEqual(setting));
}
});
it('when root has no jest but the sub folder did', async () => {
(vscode.workspace as any).workspaceFolders = [makeWorkspaceFolder('whatever')];
mockWorkspaceManager.getFoldersFromFilesystem.mockReturnValue(Promise.resolve([ws1, ws2]));
const defaultJestCommandSpy = jest.spyOn(helper, 'getDefaultJestCommand');
defaultJestCommandSpy.mockReturnValueOnce(undefined).mockReturnValueOnce('should be ws2');
const result = await helper.getValidJestCommand(workspace, mockWorkspaceManager, ws1.fsPath);
expect(defaultJestCommandSpy).toHaveBeenCalledTimes(2);
expect(result.validSettings).toEqual([
{ rootPath: ws2.fsPath, jestCommandLine: 'should be ws2' },
]);
});
});
describe('escapeQuotes', () => {
it.each`
case | inputString | expected
${'no quotes'} | ${'no quotes'} | ${'no quotes'}
${'single quotes'} | ${"with 'single quotes'"} | ${"with \\'single quotes\\'"}
${'double quotes'} | ${'with "double quotes"'} | ${'with \\"double quotes\\"'}
${'mixed quotes'} | ${'with "double" and \'single\''} | ${'with \\"double\\" and \\\'single\\\''}
${'escaped quotes'} | ${'with \\"escaped\\" quotes'} | ${'with \\\\"escaped\\\\" quotes'}
${'escaped quotes 2'} | ${"with \\'escaped\\' quotes"} | ${"with \\\\'escaped\\\\' quotes"}
${'escaped quotes 3'} | ${'with \\\'escaped\\\' and "quotes"'} | ${'with \\\\\'escaped\\\\\' and \\"quotes\\"'}
`('$case', ({ inputString, expected }) => {
expect(helper.escapeQuotes(inputString)).toEqual(expected);
});
});