Karma缓存未更新:设置nocache仍提供旧Web Worker文件
我的karma.conf.js配置如下:
config.set({ basePath: '', frameworks: ['jasmine', '@angular-devkit/build-angular'], plugins: [ require('karma-jasmine'), require('karma-chrome-launcher'), require('karma-jasmine-html-reporter'), require('karma-coverage-istanbul-reporter'), require('@angular-devkit/build-angular/plugins/karma') ], files: [ { pattern: 'src/workers/**/*.worker', type: 'js', watched: true, included: false, nocache: true, served: true } ], proxies: { '/workers/': '/base/src/workers/' }, ... });
我有一个以ES2022编译的TypeScript Web Worker库,还有一个消费该Worker的服务库,测试时通过注入令牌配置Worker实例:
{ provide: ProxyWorkerToken, useFactory: () => { const url = new URL('http://localhost:9876/workers/identity.worker.js'); if (url) { return new Worker(url, { name: 'identity.worker', type: 'module' }); } throw new Error(`url is undefined, node_module path to worker could not be resolved.`); } }
最初Karma提供的identity.worker.js内容为:
export * from './subsonic-core-identity.worker';
但这与库打包生成的mjs文件不匹配,我将文件内容修改为:
export * from './subsonic-core-identity.worker.mjs';
然而Karma开发服务器仍返回旧版文件,我已尝试清理浏览器缓存、确认nocache设为true、重启电脑、清除Chrome缓存,但均无效。直接加载mjs文件时Worker能正常创建。
附身份库的angular.json配置:
"@subsonic-core/identity": { "projectType": "library", "root": "projects/subsonic-core/identity", "sourceRoot": "projects/subsonic-core/identity/src", "prefix": "lib", "architect": { "build": { "builder": "@angular-devkit/build-angular:ng-packagr", "options": { "project": "projects/subsonic-core/identity/ng-package.json" }, "configurations": { "production": { "tsConfig": "projects/subsonic-core/identity/tsconfig.lib.prod.json" }, "development": { "tsConfig": "projects/subsonic-core/identity/tsconfig.lib.json" } }, "defaultConfiguration": "production" }, "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "tsConfig": "projects/subsonic-core/identity/tsconfig.spec.json", "assets": [ { "glob": "**/*", "input": "./node_modules/@subsonic-core/identity.worker/fesm2022", "output": "./workers" } ], "polyfills": [ "zone.js", "zone.js/testing" ], "scripts": [] } } } }
按优先级尝试以下方法:
清除Karma本地缓存目录
Karma会在项目根目录的./.karma或./node_modules/.cache/karma存储缓存,直接删除这些目录后重启Karma。更新测试资产配置与Worker库
先重新构建Worker库确保node_modules下的文件是最新的:ng build @subsonic-core/identity.worker再修改
angular.json的测试assets配置,指定加载你修改后的本地identity.worker.js,而非从node_modules复制的旧文件:"assets": [ { "glob": "**/*.mjs", "input": "./node_modules/@subsonic-core/identity.worker/fesm2022", "output": "./workers" }, { "glob": "identity.worker.js", "input": "./src/workers", "output": "./workers" } ]调整Karma的
files配置优先级
在karma.conf.js的files数组中,将修改后的identity.worker.js放在通配符规则前面,确保优先加载:files: [ { pattern: 'src/workers/identity.worker.js', type: 'js', watched: true, included: false, nocache: true, served: true }, { pattern: 'src/workers/**/*.worker', type: 'js', watched: true, included: false, nocache: true, served: true } ]彻底关闭Karma缓存
在karma.conf.js中添加全局缓存禁用配置:config.set({ // ...其他配置 cache: false, // ... });临时添加URL版本戳
在Worker的请求URL后添加时间戳参数,强制绕过缓存:const url = new URL('http://localhost:9876/workers/identity.worker.js?v=' + Date.now());
内容的提问来源于stack exchange,提问作者Kenneth Carter

