-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathtest-files_spec.ts
151 lines (123 loc) · 4.99 KB
/
test-files_spec.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
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// eslint-disable-next-line import/no-extraneous-dependencies
import realGlob from 'fast-glob';
import { promises as fs } from 'fs';
import * as path from 'path';
import { findTestFiles } from './test-files';
describe('test-files', () => {
describe('findTestFiles()', () => {
let tempDir!: string;
beforeEach(async () => {
tempDir = await fs.mkdtemp('angular-cli-jest-builder-test-files-');
});
afterEach(async () => {
await fs.rm(tempDir, { recursive: true });
});
it('returns all the test files in the project', async () => {
await fs.writeFile(path.join(tempDir, 'foo.spec.ts'), '');
await fs.mkdir(path.join(tempDir, 'nested'));
await fs.writeFile(path.join(tempDir, 'nested', 'bar.spec.ts'), '');
const testFiles = await findTestFiles(
['**/*.spec.ts'] /* include */,
[] /* exclude */,
tempDir,
);
expect(testFiles).toEqual(new Set(['foo.spec.ts', path.join('nested', 'bar.spec.ts')]));
});
it('excludes `node_modules/` and files from input options', async () => {
await fs.writeFile(path.join(tempDir, 'foo.spec.ts'), '');
await fs.writeFile(path.join(tempDir, 'bar.ignored.spec.ts'), '');
await fs.mkdir(path.join(tempDir, 'node_modules', 'dep'), { recursive: true });
await fs.writeFile(path.join(tempDir, 'node_modules', 'dep', 'baz.spec.ts'), '');
const testFiles = await findTestFiles(
['**/*.spec.ts'] /* include */,
['**/*.ignored.spec.ts'] /* exclude */,
tempDir,
);
expect(testFiles).toEqual(new Set(['foo.spec.ts']));
});
it('finds files in multiple globs', async () => {
await fs.writeFile(path.join(tempDir, 'foo.spec.ts'), '');
await fs.writeFile(path.join(tempDir, 'bar.test.ts'), '');
await fs.writeFile(path.join(tempDir, 'foo.ignored.spec.ts'), '');
await fs.writeFile(path.join(tempDir, 'bar.ignored.test.ts'), '');
await fs.mkdir(path.join(tempDir, 'node_modules', 'dep'), { recursive: true });
await fs.writeFile(path.join(tempDir, 'node_modules', 'dep', 'baz.spec.ts'), '');
await fs.writeFile(path.join(tempDir, 'node_modules', 'dep', 'baz.test.ts'), '');
const testFiles = await findTestFiles(
['**/*.spec.ts', '**/*.test.ts'] /* include */,
// Exclude should be applied to all `glob()` executions.
['**/*.ignored.*.ts'] /* exclude */,
tempDir,
);
expect(testFiles).toEqual(new Set(['foo.spec.ts', 'bar.test.ts']));
});
it('is constrained to the workspace root', async () => {
await fs.mkdir(path.join(tempDir, 'nested'));
await fs.writeFile(path.join(tempDir, 'foo.spec.ts'), '');
await fs.writeFile(path.join(tempDir, 'nested', 'bar.spec.ts'), '');
const testFiles = await findTestFiles(
['**/*.spec.ts'] /* include */,
[] /* exclude */,
path.join(tempDir, 'nested'),
);
expect(testFiles).toEqual(new Set(['bar.spec.ts']));
});
it('throws if any `glob` invocation fails', async () => {
const err = new Error('Eww, I stepped in a glob.');
const glob = jasmine
.createSpy('glob', realGlob)
.and.returnValues(
Promise.resolve(['foo.spec.ts']),
Promise.reject(err),
Promise.resolve(['bar.test.ts']),
);
await expectAsync(
findTestFiles(
['*.spec.ts', '*.stuff.ts', '*.test.ts'] /* include */,
[] /* exclude */,
tempDir,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
glob as any,
),
).toBeRejectedWith(err);
});
it('disables brace expansion', async () => {
await fs.writeFile(path.join(tempDir, 'foo.spec.ts'), '');
await fs.writeFile(path.join(tempDir, 'bar.spec.ts'), '');
const testFiles = await findTestFiles(
['{foo,bar}.spec.ts'] /* include */,
[] /* exclude */,
tempDir,
);
expect(testFiles).toEqual(new Set());
});
it('disables `extglob` features', async () => {
await fs.writeFile(path.join(tempDir, 'foo.spec.ts'), '');
await fs.writeFile(path.join(tempDir, 'bar.spec.ts'), '');
const testFiles = await findTestFiles(
['+(foo|bar).spec.ts'] /* include */,
[] /* exclude */,
tempDir,
);
expect(testFiles).toEqual(new Set());
});
it('ignores directories', async () => {
await fs.mkdir(path.join(tempDir, 'foo.spec.ts'));
await fs.mkdir(path.join(tempDir, 'bar.spec.ts'));
await fs.writeFile(path.join(tempDir, 'bar.spec.ts', 'baz.spec.ts'), '');
const testFiles = await findTestFiles(
['**/*.spec.ts'] /* include */,
[] /* exclude */,
tempDir,
);
expect(testFiles).toEqual(new Set([path.join('bar.spec.ts', 'baz.spec.ts')]));
});
});
});