angularcachingkarma-runner

karma serving cached content that is set for nocache


in the karma.conf.js I have the following configuration:

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/'
}, ... });

I have a typescript library that is a web worker that compiles with ES2022 and WebWorker. I have another library that contains the consuming service that interacts with the worker.

I have an injection token in this library that is used in the spec test for the service.

{
    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 serves this file which initially contained the following export.

export * from './subsonic-core-identity.worker';

the file identity.worker.js was served, but the script loading process did not match to mjs that the transpile created when the identity.worker library was packaged and deployed.

I have since updated the file to the following contents:

export * from './subsonic-core-identity.worker.mjs';

the karma dev server is serving the old file with out the .mjs. I have cleared the browser cache, I have made sure the pattern was set to nocache: true, I have restarted my computer and I deleted all cached data that is linked with chrome. I am unable to clear the karma cache holding onto the first instance of this file. I have loaded the mjs directly and the worker background thread is created just fine.

just so you know how the folder location is set in the test run I will share the angular.json that makes up the project definition for the identity library.

"@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": []
      }
    }
  }
}

Solution

  • I spent several days researching and trying to determine why it was serving what appeared to be a cached instance of this file. Since I was at the end of my rope, I decided why not increment the version of the library and see what happens.

    I incremented the package version from 0.0.1-alpha.0 to 0.0.1-alpha.1.

    I then re-packaged the library and updated the dependency link in the workspace to reference the new version of the identity.worker tar ball.

    Spooled up karma jasmine and executed some tests. to my surprise the karma dev server served the updated file.

    The take away from this is in order for karma dev and maybe the angular dev environment to serve an updated file from a node modules location, the package version must be incremented.