You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)`
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
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.
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
package.json
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: '../../' }]]
};
`
The text was updated successfully, but these errors were encountered: