nx: where need to specify transformIgnorePatterns to fix "Jest encountered an unexpected token

Please make sure you have read the submission guidelines before posting an issue

Prerequisites

Please answer the following questions for yourself before submitting an issue. YOU MAY DELETE THE PREREQUISITES SECTION.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed
  • I’m reporting the issue to the correct repository (not related to Angular, AngularCLI or any dependency)

Expected Behavior

Please describe the behavior you are expecting

Current Behavior

What is the current behavior?

Failure Information (for bugs)

Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. step 1
  2. step 2

Context

Please provide any relevant information about your setup:

  • version of Nx used
  • version of Angular CLI used
  • angular.json configuration
  • version of Angular DevKit used
  • 3rd-party libraries and their versions
  • and most importantly - a use-case that fails

A minimal reproduce scenario using allows us to quickly confirm a bug (or point out coding problem) as well as confirm that we are fixing the right problem.

Failure Logs

Please include any relevant log snippets or files here.

Other

Any other relevant information that will help us help you.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 24 (4 by maintainers)

Most upvoted comments

I’m a bit confused. I would have expected that when creating a new angular app with nx the setup will work out of the box. And jest not really supporting modules seams somehow awkward for a so much hyped library. It’s really disappointing to see how much trouble it is using modules.

Adding this to root jest.config.js worked for me:

"moduleNameMapper": {
    "^lodash-es$": "lodash"
}

See https://stackoverflow.com/questions/42260218/jest-setup-syntaxerror-unexpected-token-export/54117206#answer-54117206

I’m having the same issue with the latest nx version

Hi there,

I’m hoping someone can assist, as I’m sitting with a similar issue.

In my case, this is the package that is in question

import { path } from "@appnest/web-router";

What I’ve tried;

  • Added this to the global jest.config.js in various forms (slashed, braces, curly braces) testPathIgnorePatterns: [ '/node_modules/?!(@appnest)' ]
  • Added this to the local jest.config.js in various forms (slashed, braces, curly braces) testPathIgnorePatterns: [ '../../node_modules/?!(@appnest)' ]
  • Added it to both local and global
  • Added the allowJs: true to both global and local tsconfig.spec.json files
  • Added a babel-config.js with content
module.exports = {
    presets: [
      "@babel/preset-env",
      "@babel/preset-typescript"
    ],
    plugins: [
      "dynamic-import-node",
    ],
    ignore: [
      "./node_modules"
    ]
  }

What I have no idea about is this part for my instance (in the jest.config.js, as mentioned in the comment above)

"moduleNameMapper": { "^lodash-es$": "lodash" }

My environment is a newly created workspace; npm init nx-workspace@latest myworkspace npm i @appnest/web-router

Updating app.component.ts to look as follows

import { Component, OnInit } from '@angular/core';
import { path } from "@appnest/web-router";

@Component({
  selector: 'myworkspace-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'my-app';

  ngOnInit() {
    console.log(path())
  }
}

I have no idea what to try differently

This is not nx related. Jest dropped the support for babel 6 that means that it does not read the babel.rc file anymore. Create a babel.config.js file with your configs should solve the problem https://jestjs.io/docs/en/getting-started#using-babel

In my case using react apps, after migrating to 14.6 I had to update my apps jest.config.ts to choose @nrwl/react/babel preset.

transform: {
        '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest',
        '^.+\\.[tj]sx?$': ['babel-jest', { presets: ['@nrwl/react/babel'] }], <----------
    },

You can set this in the jest.config.js at root level.

module.exports = {
  testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'],
  transform: {
    '^.+\\.(ts|js|html)$': 'jest-preset-angular/preprocessor.js'
  },
  transformIgnorePatterns: ['node_modules/(?!lodash-es/*|@angular/common/locales/*)'],
  resolver: '@nrwl/builders/plugins/jest/resolver',
  moduleFileExtensions: ['ts', 'js', 'html'],
  coverageReporters: ['json', 'html', 'cobertura', 'text-summary']
};