Jest ionic 6 angular 14 Jest encountered an unexpected token - how to set up Jest

215 views Asked by At

I'm facing ionic 6 angular 14 Jest set up error on @awrsome-cordova library.

Jest.config.js

    const { pathsToModuleNameMapper } = require('ts-jest');
globalThis.ngJest = {
  skipNgcc: false,
  tsconfig: 'tsconfig.spec.json',
};

const esModules = ['@awesome-cordova-plugins',
'@ionic','@ionic/core','@ionic/angular','@ionic-native','@angular/core','@angular','aws-amplify' ];
module.exports = {
  preset: 'jest-preset-angular',
  testEnvironment: 'jsdom',
  moduleNameMapper:{
     "^aws-amplify": "<rootDir>/node_modules/aws-amplify/dist/aws-amplify.js",
          
  },
  setupFilesAfterEnv: ['<rootDir>/src/setup-jest.ts'],
  collectCoverage: true,
  testMatch: ['**/+(*.)+(spec).+(ts)'],
    testPathIgnorePatterns: [
    "<rootDir>/node_modules/",
    "<rootDir>/dist/"
  ],
  transform: {
    '^.+\\.(ts|js|mjs|svg)$': 'jest-preset-angular',
  },
  moduleFileExtensions: [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node",
    ".html"
  ],
  transformIgnorePatterns: [
    `<rootDir>/node_modules/(?!${esModules.join('|')})`,
  ],
};

Setup-jest.ts

import 'jest-preset-angular';

Babel.config.js

module.exports = function (api) {
process.env.NODE_ENV === "development" ? api.cache(false) : api.cache(true);
const presets = [
  [
    "@babel/preset-env",
    {
      targets: {
        node: "current",
      },
    },
  ],
  "@babel/preset-typescript",
];
const plugins = ["@babel/plugin-transform-modules-commonjs"];
return {
  presets,
  plugins,
};

};

This my error: npm run test

[email protected] test jest

ts-jest[config] (WARN) message TS151001: If you have issues related to imports, you should consider setting esModuleInterop to true in your TypeScript configuration file (usually tsconfig.json). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typescript-2-7/#easier-ecmascript-module-interoperability for more information. ts-jest[ts-jest-transformer] (WARN) Got a unknown file type to compile (file: app\src\app\pages\side-menu-exp\side-menu-exp.page.html). To fix this, in your Jest config change the transform key which value is ts-jest so that it does not match this kind of files anymore. FAIL src/app/pages/side-menu-exp/side-menu-exp.page.spec.ts ● Test suite failed to run

Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
 • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
 • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation

Details:

SyntaxError: \app\src\app\pages\side-menu-exp\side-menu-exp.page.html: Support for the experimental syntax 'jsx' isn't currently enabled (1:1):

> 1 | <ion-content>
    | ^
  2 |   <!-- side hand menu starts -->
  3 |   <div class="side-nav-sect menuOpen" #sideMenu_content (click)="backdropDismiss($event)">
  4 |     <div class="sde-nav-wrp">

Add @babel/preset-react (https://github.com/babel/babel/tree/main/packages/babel-preset-react) to the 'presets' section of your Babel config to enable transformation.
If you want to leave it as-is, add @babel/plugin-syntax-jsx (https://github.com/babel/babel/tree/main/packages/babel-plugin-syntax-jsx) to the 'plugins' section to enable parsing.

  19 | @Component({
  20 |   selector: 'app-side-menu-exp',
> 21 |   templateUrl: './side-menu-exp.page.html',
1

There are 1 answers

1
Corey Sutton On

This answer will point you in the right direction.

Ensure you have installed all of the required packages. You need jest, ts-jest, @types/jest and @angular-builders/jest. You can use NPM install like this:

npm install --save-dev jest ts-jest @types/jest @angular-builders/jest

Follow the answer in the above link and you will be good to go.