Skip to content

Commit e533d70

Browse files
FDIMmgechev
authored andcommitted
fix(@angular-devkit/build-angular): mocked context in single test transform matches webpack API
1 parent f4cda9e commit e533d70

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/plugins/single-test-transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default function loader(this: loader.LoaderContext, source: string) {
5151
options.logger.error(message.join(lineSeparator));
5252
}
5353

54-
const mockedRequireContext = '{ keys: () => ({ map: (_a) => { } }) };' + lineSeparator;
54+
const mockedRequireContext = 'Object.assign(() => { }, { keys: () => [], resolve: () => undefined });' + lineSeparator;
5555
source = source.replace(regex, mockedRequireContext + targettedImports);
5656

5757
return source;

packages/angular_devkit/build_angular/test/karma/selected_spec_large.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('Karma Builder', () => {
4242
}
4343
});
4444

45-
const mockedRequireContext = '{ keys: () => ({ map: (_a) => { } }) };';
45+
const mockedRequireContext = 'Object.assign(() => { }, { keys: () => [] as string[] })';
4646
const regex = /require\.context\(.*/;
4747
host.replaceInFile('src/test.ts', regex, mockedRequireContext);
4848

@@ -68,6 +68,29 @@ describe('Karma Builder', () => {
6868
await run.stop();
6969
});
7070

71+
it('should work with test.ts that filters found keys', async () => {
72+
// the replacement below is only to prove a point that resulting test.ts file will compile!
73+
host.replaceInFile('src/test.ts', 'context.keys().map(context);', 'context.keys().filter(k => !!k).map(context);');
74+
75+
const overrides = {
76+
include: ['src/app/app.component.spec.ts'],
77+
};
78+
const logger = new logging.Logger('test');
79+
logger.subscribe(m => {
80+
if (m.level === 'error') {
81+
fail(m);
82+
}
83+
});
84+
const run = await architect.scheduleTarget(karmaTargetSpec, overrides, {
85+
logger,
86+
});
87+
88+
await expectAsync(run.result).toBeResolvedTo(jasmine.objectContaining({ success: true }));
89+
90+
await run.stop();
91+
92+
});
93+
7194
[
7295
{
7396
test: 'relative path from workspace to spec',

0 commit comments

Comments
 (0)