Search code examples
javascriptangularkarma-runner

Angular Doesn't Recognize karma.conf.js


In a commercial project, tests have been running fine for some time. We cut a release in Angular 8. On master, we upgrade to Angular 9. Now the release needs a hotfix, so I check the branch out and run tests. This is all in the same repo / file system directory.

It fails like Angular core has no exported member ɵɵFactoryDef and I quickly determine that the error is due to CLI version incompatibility.

So I uninstall ng CLI globally and reinstall npm i -g @angular/[email protected], in line with the release branch package.json. Now the above error is gone, but when I try to ng test the CLI insists that karma.conf doesn't exist. The only issue is that, like, it totally does exist.

Other things I have tried:

  1. rm -rf and npm i from scratch
  2. npm cache verify
  3. close and re-open terminals
  4. restart computer
  5. create a new karma.conf with a different name and point angular.json to that

ng cli does respect angular.json in that the below error will change depending on my karma file name and path, but it still continues to insist, on MacOS, that the below file doesn't exist: On Mac, I still get: ERROR [config]: File /Users/<various paths that totally exist>/src/karma.<tried different things here>.js does not exist!

Any ideas? Thanks!!

--- Edit: adding some technical info below per request.

test block from angular.json:


                "test": {
                    "builder": "@angular-devkit/build-angular:karma",
                    "options": {
                        "main": "src/test.ts",
                        "polyfills": "src/polyfills.ts",
                        "tsConfig": "src/tsconfig.spec.json",
                        "codeCoverage": true,
                        "karmaConfig": "src/karma.conf.js",
                        "scripts": ["node_modules/jquery/dist/jquery.js", "node_modules/jquery-mockjax/dist/jquery.mockjax.js"],
                        "assets": [
                            "src/favicon.ico",
                            {
                                "glob": "**/*.html",
                                "input": "./src/legacy",
                                "output": "./"
                            }
                        ]
                    }
                }

Partial client folder structure. Notice that storybook/ and test/ are siblings of src/, and angular.json is one level above `src/. I have already tried some karma path mutations including:

  1. "karmaConfig": "./src/karma.conf.js",
  2. "karmaConfig": "../../<correct folders>src/karma.conf.js",
  3. "karmaConfig": "src/karma.<change this>.js",

partial angular client folder structure


Solution

  • This ended up solving it for me, after steps mentioned in the question:

    1. Delete the entire repo
    2. Re-clone (actually, I did a shallow clone git clone <repo-url>.git --branch <ng8-branch> --depth 5
    3. Install again npm i

    Now ng test works as expected.