Mocha tests no longer run after upgrading an Angular 12 app to Angular 13
NickName:Jessica Kagan Ask DateTime:2022-04-30T02:10:05

Mocha tests no longer run after upgrading an Angular 12 app to Angular 13

This question is the result of a previous attempt to upgrade an older Angular/Electron app to the latest version of each; I was able to migrate the frontend of the app from Angular 9 to Angular 12 before running into the issue described here.

The app has a test suite that runs successfully in Angular 12. While the app itself appears to run well after performing an upgrade to Angular 13 through Angular-CLI, running Mocha after all these upgrades will throw the following error:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\<REDACTED_BY_AUTHOR>\src\renderer\src\app\app.component.spec.ts
at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
at Loader.getFormat (internal/modules/esm/loader.js:116:42)
at Loader.getModuleJob (internal/modules/esm/loader.js:247:31)
at async Loader.import (internal/modules/esm/loader.js:181:17)

To my understanding, Angular 13's packaging now produces ES2020 modules by default, however, Mocha in a Typescript environment still uses CommonJS. I've tried the following so far:

  • Adding a "loader": ["ts-node/esm"] statement to mocharc.json. This allows Mocha/Typescript to start transpiling test files, but also results in errors in the interrim transpiled code. For example:

    TSError: ⨯ Unable to compile TypeScript: src/test/utils/root.hooks.ts:2:58 - error TS2774: 
    This condition will always return true since this function is always defined. Did you mean to call it instead? 
    2 var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    
  • Upgrading to the latest version of ts-node. This has no apparent effect.

  • Updating the module property in tsconfig.test.json from CommonJS (derived from the main Typescript file) to ES2020. This results in SyntaxError: Cannot use import statement outside a module errors. My understanding is that fixing this would put me down the tricky path of refactoring the test suite to wrap Mocha tests in modules, which is something I don't yet understand how to do.

With that in mind, how can I bring my test suite in compliance? The way I see it, there's three options. First, I could either look for some combination of Angular, Typescript, and Mocha options that allows me to use the existing test suite with limited modifications if any. The second option is to attempt the Mocha/ESM refactor, which would presumably require significant (if boilerplate-heavy) changes to our test suite. A final and more out of the box option is to try migrating to Jest; while this sounds orthogonal even to me, I'm aware that there are automated tools that make switching a test suite from using Mocha to Jest easier; Jest also has more comprehensive documentation (even including the ESM changes in Angular 13) and appears to work better out of the box with Angular. What's the best way to proceed?

For reference, I've included some configuration files. First, our default tsconfig.json file (though only the options changed from the defaults):

{
  "compilerOptions": {
    "target": "es2018",
    "module": "commonjs",
    "lib": ["es2018", "dom"],
    "outDir": "out",
    "strict": true
    "noImplicitAny": true
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "useUnknownInCatchVariables": false
  }
}

tsconfig.test.json overrides:

  "compilerOptions": {
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true
  },

And finally, mocharc.json:

{
  "extension": ["ts"],
  "timeout": 20000,
  "spec": ["./src/test/utils/root.hooks.ts", "src/**/*.spec.ts"],
  "exclude": ["**/node_modules/**/*"],
  "require": ["ts-node/register", "./src/test/setup.js"],
  "loader": ["ts-node/esm"]
}

Copyright Notice:Content Author:「Jessica Kagan」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/72062115/mocha-tests-no-longer-run-after-upgrading-an-angular-12-app-to-angular-13

More about “Mocha tests no longer run after upgrading an Angular 12 app to Angular 13” related questions

Mocha tests no longer run after upgrading an Angular 12 app to Angular 13

This question is the result of a previous attempt to upgrade an older Angular/Electron app to the latest version of each; I was able to migrate the frontend of the app from Angular 9 to Angular 12 ...

Show Detail

Unit Tests are failing after updating to Angular 13

After updating to Angular 13, lots of my unit tests, that were running without problems on Angular 12, are now failing. What I found more frustrating is that the tests are failing only when running

Show Detail

Tilde ~ in SCSS @use statement no longer resolving to node_modules as of Angular 13

After upgrading an Angular 12 project to Angular 13 I encountered an issue where SCSS was no longer to locate a shared style sheet in a library. It appears that that Tilde (~) no longer resolves to

Show Detail

no "MethodCall" after upgrading to Angular 13

We're importing &quot;MethodCall&quot; in our project via import { MethodCall } from &quot;@angular/compiler&quot;; This worked until Angular 12. After upgrading to Angular 13, ng build throws error

Show Detail

Webdriver e2e spec tests fail after upgrading to Angular 13. Unknown file extension ".ts"

I run my Angular e2e tests using WDIO now that I know Protractor is going away. They worked fine in Angular 12. After upgrading to Angular 13, I get this error when running my tests: Execution of 2

Show Detail

How to setup angular.io testing using mocha

i've been trying to find solutions for a long time, i've tried many ways found on the internet but they just don't work because API is either outdated or packages are no longer supported. What i'm

Show Detail

After Upgrading to Angular 13 the tests with --code-coverage is failing

After Upgrading to Angular 13 the tests which are run with --code-coverage are failing with error that some plugin is missing I am using karma-coverage-istanbul-reporter on the karma.conf.js and th...

Show Detail

angular universal ssr return 502 after upgrading from angular 9 to angular 13

After upgrading angular from 9 to 13 (using angular universal for SSR ), our web application return 502 error, I think this happened due to the increasing requests. when I revert my changes and dow...

Show Detail

Cannot run mocha tests in angular project

I am unable to run mocha test in my angular project and I'm unable to debug what is the reason of that. In package .json I changed "test": "mocha -r ts-node/register src/**/test.ts" npm test &gt;

Show Detail

Uncaught TypeError after upgrading Angular v12 to v13

I am struggling with the following error: Uncaught TypeError: (0 , tslib__WEBPACK_IMPORTED_MODULE_2__.__decorate) is not a function at Module.2200 (app.service.ts:14) at __webpack_require__ (

Show Detail