Skip to content

Commit d2ca92a

Browse files
committed
test(@angular/build): use expectLog and expectNoLog helpers in tests
Introduces `expectLog` and `expectNoLog` jasmine helpers to simplify the assertions of logs in tests. This removes the need to use `jasmine.objectContaining` and `jasmine.stringMatching` for this purpose, making the tests more readable and less verbose.
1 parent dae7320 commit d2ca92a

16 files changed

+124
-325
lines changed

modules/testing/builder/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ export {
1515
type HarnessFileMatchers,
1616
JasmineBuilderHarness,
1717
describeBuilder,
18+
expectLog,
19+
expectNoLog,
1820
} from './jasmine-helpers';
1921
export * from './test-utils';

modules/testing/builder/src/jasmine-helpers.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { BuilderHandlerFn } from '@angular-devkit/architect';
10-
import { json } from '@angular-devkit/core';
10+
import { json, logging } from '@angular-devkit/core';
1111
import { readFileSync } from 'node:fs';
1212
import { concatMap, count, debounceTime, firstValueFrom, take, timeout } from 'rxjs';
1313
import {
@@ -196,3 +196,25 @@ export function expectDirectory<T>(
196196
},
197197
};
198198
}
199+
200+
export function expectLog(logs: readonly logging.LogEntry[], message: string | RegExp) {
201+
expect(logs).toContain(
202+
jasmine.objectContaining({
203+
message: jasmine.stringMatching(message),
204+
}),
205+
);
206+
}
207+
208+
export function expectNoLog(
209+
logs: readonly logging.LogEntry[],
210+
message: string | RegExp,
211+
failureMessage?: string,
212+
) {
213+
expect(logs)
214+
.withContext(failureMessage ?? '')
215+
.not.toContain(
216+
jasmine.objectContaining({
217+
message: jasmine.stringMatching(message),
218+
}),
219+
);
220+
}

packages/angular/build/src/builders/application/tests/behavior/rebuild-errors_spec.ts

Lines changed: 26 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { logging } from '@angular-devkit/core';
109
import { buildApplication } from '../../index';
11-
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
10+
import {
11+
APPLICATION_BUILDER_INFO,
12+
BASE_OPTIONS,
13+
describeBuilder,
14+
expectLog,
15+
expectNoLog,
16+
} from '../setup';
1217

1318
/**
1419
* Maximum time in milliseconds for single build/rebuild
@@ -92,47 +97,31 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
9297
},
9398
async ({ result, logs }) => {
9499
expect(result?.success).toBeFalse();
95-
expect(logs).toContain(
96-
jasmine.objectContaining<logging.LogEntry>({
97-
message: jasmine.stringMatching(typeErrorText),
98-
}),
99-
);
100+
expectLog(logs, typeErrorText);
100101

101102
// Make an unrelated change to verify error cache was updated
102103
// Should persist error in the next rebuild
103104
await harness.modifyFile('src/main.ts', (content) => content + '\n');
104105
},
105106
async ({ result, logs }) => {
106107
expect(result?.success).toBeFalse();
107-
expect(logs).toContain(
108-
jasmine.objectContaining<logging.LogEntry>({
109-
message: jasmine.stringMatching(typeErrorText),
110-
}),
111-
);
108+
expectLog(logs, typeErrorText);
112109

113110
// Revert the directive change that caused the error
114111
// Should remove the error
115112
await harness.writeFile('src/app/dir.ts', goodDirectiveContents);
116113
},
117114
async ({ result, logs }) => {
118115
expect(result?.success).toBeTrue();
119-
expect(logs).not.toContain(
120-
jasmine.objectContaining<logging.LogEntry>({
121-
message: jasmine.stringMatching(typeErrorText),
122-
}),
123-
);
116+
expectNoLog(logs, typeErrorText);
124117

125118
// Make an unrelated change to verify error cache was updated
126119
// Should continue showing no error
127120
await harness.modifyFile('src/main.ts', (content) => content + '\n');
128121
},
129122
({ result, logs }) => {
130123
expect(result?.success).toBeTrue();
131-
expect(logs).not.toContain(
132-
jasmine.objectContaining<logging.LogEntry>({
133-
message: jasmine.stringMatching(typeErrorText),
134-
}),
135-
);
124+
expectNoLog(logs, typeErrorText);
136125
},
137126
],
138127
{ outputLogsOnFailure: false },
@@ -152,78 +141,38 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
152141
await harness.appendToFile('src/app/app.component.html', '@if-one');
153142
},
154143
async ({ logs }) => {
155-
expect(logs).toContain(
156-
jasmine.objectContaining<logging.LogEntry>({
157-
message: jasmine.stringContaining('@if-one'),
158-
}),
159-
);
144+
expectLog(logs, '@if-one');
160145

161146
// Make an unrelated change to verify error cache was updated
162147
// Should persist error in the next rebuild
163148
await harness.modifyFile('src/main.ts', (content) => content + '\n');
164149
},
165150
async ({ logs }) => {
166-
expect(logs).toContain(
167-
jasmine.objectContaining<logging.LogEntry>({
168-
message: jasmine.stringContaining('@if-one'),
169-
}),
170-
);
151+
expectLog(logs, '@if-one');
171152

172153
// Add more invalid block syntax
173154
await harness.appendToFile('src/app/app.component.html', '@if-two');
174155
},
175156
async ({ logs }) => {
176-
expect(logs).toContain(
177-
jasmine.objectContaining<logging.LogEntry>({
178-
message: jasmine.stringContaining('@if-one'),
179-
}),
180-
);
181-
expect(logs).toContain(
182-
jasmine.objectContaining<logging.LogEntry>({
183-
message: jasmine.stringContaining('@if-two'),
184-
}),
185-
);
157+
expectLog(logs, '@if-one');
158+
expectLog(logs, '@if-two');
186159

187160
// Add more invalid block syntax
188161
await harness.appendToFile('src/app/app.component.html', '@if-three');
189162
},
190163
async ({ logs }) => {
191-
expect(logs).toContain(
192-
jasmine.objectContaining<logging.LogEntry>({
193-
message: jasmine.stringContaining('@if-one'),
194-
}),
195-
);
196-
expect(logs).toContain(
197-
jasmine.objectContaining<logging.LogEntry>({
198-
message: jasmine.stringContaining('@if-two'),
199-
}),
200-
);
201-
expect(logs).toContain(
202-
jasmine.objectContaining<logging.LogEntry>({
203-
message: jasmine.stringContaining('@if-three'),
204-
}),
205-
);
164+
expectLog(logs, '@if-one');
165+
expectLog(logs, '@if-two');
166+
expectLog(logs, '@if-three');
206167

207168
// Revert the changes that caused the error
208169
// Should remove the error
209170
await harness.writeFile('src/app/app.component.html', '<p>GOOD</p>');
210171
},
211172
({ logs }) => {
212-
expect(logs).not.toContain(
213-
jasmine.objectContaining<logging.LogEntry>({
214-
message: jasmine.stringContaining('@if-one'),
215-
}),
216-
);
217-
expect(logs).not.toContain(
218-
jasmine.objectContaining<logging.LogEntry>({
219-
message: jasmine.stringContaining('@if-two'),
220-
}),
221-
);
222-
expect(logs).not.toContain(
223-
jasmine.objectContaining<logging.LogEntry>({
224-
message: jasmine.stringContaining('@if-three'),
225-
}),
226-
);
173+
expectNoLog(logs, '@if-one');
174+
expectNoLog(logs, '@if-two');
175+
expectNoLog(logs, '@if-three');
227176
},
228177
],
229178
{ outputLogsOnFailure: false },
@@ -243,20 +192,12 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
243192
await harness.writeFile('src/app/app.component.css', 'invalid-css-content');
244193
},
245194
async ({ logs }) => {
246-
expect(logs).toContain(
247-
jasmine.objectContaining<logging.LogEntry>({
248-
message: jasmine.stringMatching('invalid-css-content'),
249-
}),
250-
);
195+
expectLog(logs, 'invalid-css-content');
251196

252197
await harness.writeFile('src/app/app.component.css', 'p { color: green }');
253198
},
254199
({ logs }) => {
255-
expect(logs).not.toContain(
256-
jasmine.objectContaining<logging.LogEntry>({
257-
message: jasmine.stringMatching('invalid-css-content'),
258-
}),
259-
);
200+
expectNoLog(logs, 'invalid-css-content');
260201

261202
harness
262203
.expectFile('dist/browser/main.js')
@@ -280,32 +221,20 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
280221
await harness.appendToFile('src/app/app.component.html', '<div>Hello, world!</div');
281222
},
282223
async ({ logs }) => {
283-
expect(logs).toContain(
284-
jasmine.objectContaining<logging.LogEntry>({
285-
message: jasmine.stringMatching('Unexpected character "EOF"'),
286-
}),
287-
);
224+
expectLog(logs, 'Unexpected character "EOF"');
288225

289226
await harness.appendToFile('src/app/app.component.html', '>');
290227
},
291228
async ({ logs }) => {
292-
expect(logs).not.toContain(
293-
jasmine.objectContaining<logging.LogEntry>({
294-
message: jasmine.stringMatching('Unexpected character "EOF"'),
295-
}),
296-
);
229+
expectNoLog(logs, 'Unexpected character "EOF"');
297230

298231
harness.expectFile('dist/browser/main.js').content.toContain('Hello, world!');
299232

300233
// Make an additional valid change to ensure that rebuilds still trigger
301234
await harness.appendToFile('src/app/app.component.html', '<div>Guten Tag</div>');
302235
},
303236
({ logs }) => {
304-
expect(logs).not.toContain(
305-
jasmine.objectContaining<logging.LogEntry>({
306-
message: jasmine.stringMatching('invalid-css-content'),
307-
}),
308-
);
237+
expectNoLog(logs, 'invalid-css-content');
309238

310239
harness.expectFile('dist/browser/main.js').content.toContain('Hello, world!');
311240
harness.expectFile('dist/browser/main.js').content.toContain('Guten Tag');

packages/angular/build/src/builders/application/tests/behavior/rebuild-web-workers_spec.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { logging } from '@angular-devkit/core';
109
import { buildApplication } from '../../index';
11-
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
10+
import {
11+
APPLICATION_BUILDER_INFO,
12+
BASE_OPTIONS,
13+
describeBuilder,
14+
expectLog,
15+
expectNoLog,
16+
} from '../setup';
1217

1318
/**
1419
* A regular expression used to check if a built worker is correctly referenced in application code.
@@ -62,46 +67,30 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
6267
},
6368
async ({ result, logs }) => {
6469
expect(result?.success).toBeFalse();
65-
expect(logs).toContain(
66-
jasmine.objectContaining<logging.LogEntry>({
67-
message: jasmine.stringMatching(errorText),
68-
}),
69-
);
70+
expectLog(logs, errorText);
7071

7172
// Make an unrelated change to verify error cache was updated
7273
// Should persist error in the next rebuild
7374
await harness.modifyFile('src/main.ts', (content) => content + '\n');
7475
},
7576
async ({ logs }) => {
76-
expect(logs).toContain(
77-
jasmine.objectContaining<logging.LogEntry>({
78-
message: jasmine.stringMatching(errorText),
79-
}),
80-
);
77+
expectLog(logs, errorText);
8178

8279
// Revert the change that caused the error
8380
// Should remove the error
8481
await harness.writeFile('src/app/worker.ts', workerCodeFile);
8582
},
8683
async ({ result, logs }) => {
8784
expect(result?.success).toBeTrue();
88-
expect(logs).not.toContain(
89-
jasmine.objectContaining<logging.LogEntry>({
90-
message: jasmine.stringMatching(errorText),
91-
}),
92-
);
85+
expectNoLog(logs, errorText);
9386

9487
// Make an unrelated change to verify error cache was updated
9588
// Should continue showing no error
9689
await harness.modifyFile('src/main.ts', (content) => content + '\n');
9790
},
9891
({ result, logs }) => {
9992
expect(result?.success).toBeTrue();
100-
expect(logs).not.toContain(
101-
jasmine.objectContaining<logging.LogEntry>({
102-
message: jasmine.stringMatching(errorText),
103-
}),
104-
);
93+
expectNoLog(logs, errorText);
10594

10695
// Ensure built worker is referenced in the application code
10796
harness.expectFile('dist/browser/main.js').content.toMatch(REFERENCED_WORKER_REGEXP);

packages/angular/build/src/builders/application/tests/behavior/typescript-rebuild-lazy_spec.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import type { logging } from '@angular-devkit/core';
109
import { buildApplication } from '../../index';
1110
import { OutputHashing } from '../../schema';
12-
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup';
11+
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder, expectLog } from '../setup';
1312

1413
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
1514
beforeEach(async () => {
@@ -57,13 +56,7 @@ describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => {
5756
},
5857
async ({ result, logs }) => {
5958
expect(result?.success).toBeFalse();
60-
expect(logs).toContain(
61-
jasmine.objectContaining<logging.LogEntry>({
62-
message: jasmine.stringMatching(
63-
`Type 'number' is not assignable to type 'string'.`,
64-
),
65-
}),
66-
);
59+
expectLog(logs, `Type 'number' is not assignable to type 'string'.`);
6760

6861
// Fix TS error
6962
await harness.writeFile('src/lazy.ts', `export const foo: string = "1";`);

0 commit comments

Comments
 (0)