Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular-devkit/build-angular): parse web-workers in tests when webWorkerTsConfig is defined #21160

Merged
merged 1 commit into from Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/angular_devkit/build_angular/BUILD.bazel
Expand Up @@ -295,6 +295,7 @@ LARGE_SPECS = {
},
"extract-i18n": {},
"karma": {
"size": "large",
"extra_deps": [
"@npm//karma",
"@npm//karma-chrome-launcher",
Expand Down
@@ -0,0 +1,114 @@
/**
* @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
*/

import { execute } from '../../index';
import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeBuilder } from '../setup';

describeBuilder(execute, KARMA_BUILDER_INFO, (harness) => {
describe('Option: "webWorkerTsConfig"', () => {
beforeEach(async () => {
await harness.writeFiles({
'src/tsconfig.worker.json': `
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/worker",
"lib": [
"es2018",
"webworker"
],
"types": []
},
"include": [
"**/*.worker.ts",
]
}`,
'src/app/app.worker.ts': `
/// <reference lib="webworker" />

const prefix: string = 'Data: ';
addEventListener('message', ({ data }) => {
postMessage(prefix + data);
});
`,
'src/app/app.component.ts': `
import { Component } from '@angular/core';

@Component({
selector: 'app-root',
template: ''
})
export class AppComponent {
worker = new Worker(new URL('./app.worker', import.meta.url));
}
`,
'./src/app/app.component.spec.ts': `
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
AppComponent
]
}).compileComponents();
});

it('worker should be defined', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
expect(app.worker).toBeDefined();
});
});`,
});
});

it(`should not parse web workers when "webWorkerTsConfig" is not set or set to undefined.`, async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
webWorkerTsConfig: undefined,
});

await harness.writeFile(
'./src/app/app.component.spec.ts',
`
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [
AppComponent
]
}).compileComponents();
});

it('worker should throw', () => {
expect(() => TestBed.createComponent(AppComponent))
.toThrowError(/Failed to construct 'Worker'/);
});
});`,
);

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});

it(`should parse web workers when "webWorkerTsConfig" is set.`, async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
webWorkerTsConfig: 'src/tsconfig.worker.json',
});

const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});
});
});
Expand Up @@ -64,13 +64,13 @@ export function getTestConfig(
rules: extraRules,
parser:
webWorkerTsConfig === undefined
? undefined
: {
? {
javascript: {
worker: false,
url: false,
},
},
}
: undefined,
},
plugins: extraPlugins,
optimization: {
Expand Down