使用Angular CLI创建一个新项目,并生成一个网络工作人员并进行测试。
ng new demo
ng generate web-worker app
ng test --browsers=ChromeHeadless
获取错误:
TypeError: Failed to construct 'Worker': Module scripts are not supported on DedicatedWorker yet. You can try the feature with '--enable-experimental-web-platform-features' flag (see https://crbug.com/680046)
在命令行中添加“ --enable-experimental-web-platform-features”失败,并带有“未知选项”。是否可以将“ enable-experimental-web-platform-features”作为标志传递或对其进行有条件的配置?
答案 0 :(得分:1)
我找到了解决方案。在脚本之后的“测试”部分的angular.json中:[]需要添加webWorkerTsConfig字段,如构建部分所示:
"webWorkerTsConfig": "tsconfig.worker.json"
我的“测试”部分现在看起来像(这是一个空项目):
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": [],
"webWorkerTsConfig": "tsconfig.worker.json"
}
},
希望,对您有帮助:)
答案 1 :(得分:0)
另一种可能有用的方法是嘲笑Worker。在单元测试中,我添加了以下内容:
class Windows {
private url: string;
private onmessage: (msg: any) => void;
constructor(stringUrl: string) {
this.url = stringUrl;
this.onmessage = () => {};
}
postMessage(msg: any) {
this.onmessage(msg);
}
}
// @ts-ignore
window.Worker = Worker;