Jonathan002
Jonathan002

Reputation: 10368

How to get Nrwl Nest.js Jest workspace working with firebase es6 packages?

I have a nrwl Angular-Jest application setup via npx create-nx-workspace.

While working on this project, I had upgraded from:

"firebase": "9.1.3",
"firebase-admin": "9.12.0",
"firebase-functions": "3.15.7",

to

"firebase": "9.16.0",
"firebase-admin": "11.5.0",
"firebase-functions": "4.2.0",

to get the extension emulator working locally. Although it works, I now have issues running jest. It says that firebase now has es6 imports and wont work. Error Message Below:

  ● 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:

/Users/[justOnlyMe]/Desktop/[nrwlProject]/node_modules/firebase-admin/lib/esm/auth/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import mod from "../../auth/index.js";
                                                                                  ^^^^^^

SyntaxError: Cannot use import statement outside a module

  at Runtime.createScriptFromCode (../../../node_modules/jest-runtime/build/index.js:1728:14)
  at Object.<anonymous> (../../../node_modules/firebase-functions/lib/common/providers/identity.js:25:14)

I've also tried using transformIgnorePatterns to get a transformation just for firebase node_modules but now jest continues to run and seems unresponsive. (I believe this may be an issue with ts-jest in jest).

transform: {
    '^.+\\.[tj]s$': 'ts-jest',
  },
  transformIgnorePatterns: [
    '../../../node_modules/(?!@firebase.*/)',
    '../../../node_modules/(?!firebase/)',
    '../../../node_modules/(?!firebase-admin/)',
    '../../../node_modules/(?!firebase-functions/)',
  ],

Question: Does anyone know how to get es6 node_modules to work with jest in a nrwl nest.js typescript project?

My jest config:

module.exports = {
  displayName: '[projectName]',
  preset: '../../../jest.preset.js',
  globals: {
    'ts-jest': {
      tsconfig: '<rootDir>/tsconfig.spec.json',
    },
  },
  transform: {
    '^.+\\.[tj]s$': 'ts-jest',
  },
  setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
  moduleFileExtensions: ['ts', 'js', 'html'],
  coverageDirectory: '../../../coverage/apps/back/[projectName]',
  testEnvironment: 'node',
}

Upvotes: 4

Views: 337

Answers (1)

underflow
underflow

Reputation: 2201

In your jest config add in the transform object the babel-jest config:

"transform": {
  "^.+\\.js?$": "babel-jest",
  "^.+\\.ts?$": "ts-jest"
}

Upvotes: 1

Related Questions