Search code examples
typescriptjestjsnestjsts-jest

Jest encountered an unexpected token nestjs


I am having issues with running my e2e tests in nest.js using jest. I keep getting the error

Jest encountered an unexpected token

anytime I try to run my e2e test. All other test suites ran successfully but not. I have combed through the Internet all to no avail.

Here is my jest config in package.json

"jest": {
    "preset": "ts-jest",
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": "(?<!.e2e).spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }

Below is my tsconfig.json file

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2019",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "esModuleInterop": true,
    "lib": ["es2019"]
  }
}

I am using ts-jest and not babel

I run my e2e test with this script in package.json

"test:e2e": "jest --config ./test/jest-e2e.json", and below is the config in my jest-e2e.json file

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": "../",
  "testEnvironment": "node",
  "testRegex": ".e2e.spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  }
}

Finally, below is the actual error in the console:

Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • 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/en/configuration.html

    Details:

    /Users/michaelowolabi/Desktop/YoungM/sz/sz-graphql-api/node_modules/graphql-moment/lib/factory.coffee:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){{GraphQLScalarType} = require 'graphql'
                                                                                                                 ^

    SyntaxError: Unexpected token '='

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (node_modules/graphql-moment/index.js:2:17)

Solution

  • Looks like graphql-moment code isn't being published with a commonJS, UMD, or some other compatible format and Jest can't parse it. What you're most likely going to need to do is either mock it in your tests like so:

    jest.mock('graphql-moment', () => ({
      GraphQLDate: jest.fn(),
      ...etcOtherFunctionsFromTheLib
    }));
    

    or alternatively tell Jest to transpile the dependency code at Jest runtime with this in your Jest config:

      transformIgnorePatterns: [
        '<rootDir>/node_modules/(?!graphql-moment)',
      ],
    

    the second solution is probably better if you're testing that the date/time is what you expect.

    You can see here that the code isn't actually being transpiled. Maybe open a issue with them:

    https://github.com/jiexi/graphql-moment/blob/master/index.js