-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathselected_spec.ts
165 lines (143 loc) · 5.07 KB
/
selected_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @license
* Copyright Google Inc. 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
*/
import { Architect } from '@angular-devkit/architect';
import { logging } from '@angular-devkit/core';
import { createArchitect, host, karmaTargetSpec } from '../test-utils';
describe('Karma Builder', () => {
let architect: Architect;
beforeEach(async () => {
await host.initialize().toPromise();
architect = (await createArchitect(host.root())).architect;
});
afterEach(() => host.restore().toPromise());
describe('with include option', () => {
it('should fail when include does not match any files', async () => {
const overrides = {
include: ['abc.spec.ts', 'def.spec.ts'],
};
const run = await architect.scheduleTarget(karmaTargetSpec, overrides);
await expectAsync(run.result).toBeRejectedWith(
`Specified patterns: "abc.spec.ts, def.spec.ts" did not match any spec files`,
);
await run.stop();
});
it('should fail when main test file does not include require.context usage', async () => {
let lastErrorLogEntry: logging.LogEntry | undefined;
const logger = new logging.Logger('test');
logger.subscribe(m => {
if (m.level === 'error') {
lastErrorLogEntry = m;
}
});
const mockedRequireContext = 'Object.assign(() => { }, { keys: () => [] as string[] })';
const regex = /require\.context\(.*/;
host.replaceInFile('src/test.ts', regex, mockedRequireContext);
const overrides = {
include: ['**/*.spec.ts'],
};
const run = await architect.scheduleTarget(karmaTargetSpec, overrides, {
logger,
});
await expectAsync(run.result).toBeResolved();
expect(lastErrorLogEntry && lastErrorLogEntry.message).toContain(
'const context = require.context',
);
expect(lastErrorLogEntry && lastErrorLogEntry.message)
// tslint:disable-next-line:max-line-length
.toContain(
"The 'include' option requires that the 'main' file for tests includes the below line:",
);
await run.stop();
});
it('should work with test.ts that filters found keys', async () => {
// the replacement below is only to prove a point that resulting test.ts file will compile!
host.replaceInFile('src/test.ts', 'context.keys().map(context);', 'context.keys().filter(k => !!k).map(context);');
const overrides = {
include: ['src/app/app.component.spec.ts'],
};
const logger = new logging.Logger('test');
logger.subscribe(m => {
if (m.level === 'error') {
fail(m);
}
});
const run = await architect.scheduleTarget(karmaTargetSpec, overrides, {
logger,
});
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: true }));
await run.stop();
});
[
{
test: 'relative path from workspace to spec',
input: ['src/app/app.component.spec.ts'],
},
{
test: 'relative path from workspace to file',
input: ['src/app/app.component.ts'],
},
{
test: 'relative path from project root to spec',
input: ['app/services/test.service.spec.ts'],
},
{
test: 'relative path from project root to file',
input: ['app/services/test.service.ts'],
},
{
test: 'relative path from workspace to directory',
input: ['src/app/services'],
},
{
test: 'relative path from project root to directory',
input: ['app/services'],
},
{
test: 'glob with spec suffix',
input: ['**/*.pipe.spec.ts', '**/*.pipe.spec.ts', '**/*test.service.spec.ts'],
},
].forEach((options, index) => {
it(`should work with ${options.test} (${index})`, async () => {
host.writeMultipleFiles({
'src/app/services/test.service.spec.ts': `
describe('TestService', () => {
it('should succeed', () => {
expect(true).toBe(true);
});
});`,
'src/app/failing.service.spec.ts': `
describe('FailingService', () => {
it('should be ignored', () => {
expect(true).toBe(false);
});
});`,
'src/app/property.pipe.spec.ts': `
describe('PropertyPipe', () => {
it('should succeed', () => {
expect(true).toBe(true);
});
});`,
});
const overrides = {
include: options.input,
};
const logger = new logging.Logger('test');
logger.subscribe(m => {
if (m.level === 'error') {
fail(m);
}
});
const run = await architect.scheduleTarget(karmaTargetSpec, overrides, {
logger,
});
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: true }));
await run.stop();
});
});
});
});