forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode-coverage-include_spec.ts
More file actions
82 lines (70 loc) · 2.9 KB
/
code-coverage-include_spec.ts
File metadata and controls
82 lines (70 loc) · 2.9 KB
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
/**
* @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.dev/license
*/
import { execute } from '../../index';
import {
BASE_OPTIONS,
describeBuilder,
UNIT_TEST_BUILDER_INFO,
setupApplicationTarget,
} from '../setup';
describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
describe('Option: "coverageInclude"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
await harness.writeFiles({
'src/app/included.ts': `export const a = 1;`,
'src/app/included.spec.ts': `
import { a } from './included';
describe('included', () => {
it('should work', () => {
expect(a).toBe(1);
});
});
`,
'src/app/excluded.ts': `export const b = 2;`,
});
});
it('should only include and report coverage for files that match the glob pattern', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
coverage: true,
coverageInclude: ['**/included.ts'],
});
const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
const summary = JSON.parse(harness.readFile('coverage/test/coverage-final.json'));
const summaryKeys = Object.keys(summary);
const includedKey = summaryKeys.find((key) => key.endsWith('src/app/included.ts'));
const excludedKey = summaryKeys.find((key) => key.endsWith('src/app/excluded.ts'));
// Check that the included file is in the report and the excluded one is not.
expect(includedKey).toBeDefined();
expect(excludedKey).toBeUndefined();
// Check that the coverage data for the included file is valid.
const includedCoverage = summary[includedKey!];
// The file has one statement, and it should have been executed once.
expect(includedCoverage.s['0']).toBe(1);
});
it('should only include referenced files when no include pattern is provided', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
coverage: true,
// coverageInclude is not provided, so only referenced files should be included.
});
const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
const summary = JSON.parse(harness.readFile('coverage/test/coverage-final.json'));
const summaryKeys = Object.keys(summary);
const includedKey = summaryKeys.find((key) => key.endsWith('src/app/included.ts'));
const excludedKey = summaryKeys.find((key) => key.endsWith('src/app/excluded.ts'));
// The included file is referenced by its spec and should be in the report.
expect(includedKey).toBeDefined();
// The excluded file is not referenced and should NOT be in the report.
expect(excludedKey).toBeUndefined();
});
});
});