Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest encountered an unexpected token #4267

Open
MaryamAdnan3 opened this issue Feb 13, 2024 · 7 comments
Open

Jest encountered an unexpected token #4267

MaryamAdnan3 opened this issue Feb 13, 2024 · 7 comments

Comments

@MaryamAdnan3
Copy link

MaryamAdnan3 commented Feb 13, 2024

Detail:

I have updated a third party library axios version to 1.6.7 . When I ran tests I started to get the following error
` 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:

D:\gitRepos\apimatic-js-runtime\node_modules\axios\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import axios from './lib/axios.js';
                                                                                         ^^^^^^

SyntaxError: Cannot use import statement outside a module

> 1 | import { AxiosHeaders, AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosResponseHeaders, RawAxiosResponseHeaders } from 'axios';
    | ^
  2 | import axios from 'axios';
  3 | import isNode from 'detect-node';
  4 | import FormData from 'form-data';

  at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1350:14)
  at Object.<anonymous> (src/httpClient.ts:1:1)`

package.json

  "devDependencies": {
    "@babel/cli": "^7.21.5",
    "@babel/core": "^7.22.1",
    "@babel/preset-env": "^7.18.2",
    "babel-jest": "^26.6.3",
    "@size-limit/preset-small-lib": "^7.0.8",
    "@types/detect-node": "^2.0.0",
    "@types/lodash.flatmap": "^4.5.6",
    "abort-controller": "^3.0.0",
    "babel-plugin-annotate-pure-calls": "^0.4.0",
    "jest": "^26.6.3",
    "jsdom": "^19.0.0",
    "jsdom-global": "^3.0.2",
    "lerna-alias": "3.0.3-0",
    "rollup": "^2.79.0",
    "rollup-plugin-typescript2": "^0.31.0",
    "size-limit": "^7.0.8",
    "ts-jest": "^29.1.2",
    "typescript": "^4.1.2"
  }

jest.config.js
`const { jest: lernaAliases } = require('lerna-alias');

module.exports = {
preset: 'ts-jest',
moduleNameMapper: lernaAliases(),
transform: {
'^.+\.tsx?$': 'ts-jest', // Add TypeScript transformation
'^.+\.jsx?$': 'babel-jest', // Add Babel transformation for ES6 modules
},
coverageReporters: [['lcov', { projectRoot: '../../' }]]
};
`

@ledenakralica
Copy link

I have a similar issue. It seems to me that for some reason ts-jest instructs the typescript compiler to emit ES3/ES5 code. I cannot pinpoint where this is happening though

@ahnpnl
Copy link
Collaborator

ahnpnl commented May 19, 2024

This error indicates that Jest tried to process axios but axios is not passed (by Jest) to ts-jest to transform.

Usually, this can be solved by configuring transformIgnoredPattern option in Jest config file or moduleNameMapper to tell Jest to resolve CJS version of axios.

You can also use Jest ESM mode to process ESM axios file.

@karpikpl
Copy link

karpikpl commented Jun 18, 2024

I think I'm having the same issue https://github.com/karpikpl/typescript-esm-sample
I tried using transformIgnoredPattern but no luck :(

SyntaxError: Cannot use import statement outside a module

    > 1 | import { graphql } from '@octokit/graphql'

this seems to have no effect:

    "transformIgnorePatterns": [
      "node_modules/(?!(@octokit/graphql|@octokit/request))"
    ],

@thenoseman
Copy link

I am having a similar issue.
I assume I just don't know how to configure the setup correctly.
I created a stripped reproduction repo here where I described what I did for debugging so far (so that I don't seem lazy 😉).

In my case I see that for the node_module in question the TS compiler outputs:

 {
  "outputFiles": [],
  "emitSkipped": true,
  "diagnostics": []
}

This then causes ts-jest to output the message.
This might just be a setup issue, so excuse me if I ask at the wrong place.

transformIgnorePatterns is configured correctly AFAIK.

Please see the README in the repository for more information.

@ahnpnl
Copy link
Collaborator

ahnpnl commented Aug 28, 2024

@thenoseman you can use this workaround

transform: {
    '^.+\\.(ts|js)$': ['ts-jest', {
      isolatedModules: true,
    }]
},

@thenoseman
Copy link

I cannot thank you enough, that drove me nuts for two days!
Thanks a lot!

@brunolm
Copy link

brunolm commented Nov 8, 2024

That didn't seem to work for me

 FAIL  src/utils/html-to-markdown.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:

[...]/node_modules/rehype-parse/index.js:2
    export {default} from './lib/index.js'
    ^^^^^^

    SyntaxError: Unexpected token 'export'

       9 | interface TextNode extends Node {
      10 |   type: "text";
    > 11 |   value: string;
         |                                     ^
      12 | }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants