akita
akita copied to clipboard
Akita 7 fails in jest
Is this a regression?
No
Description
jest.config.js file:
require('jest-preset-angular/ngcc-jest-processor'); const { pathsToModuleNameMapper } = require('ts-jest/utils'); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const { compilerOptions } = require('./tsconfig'); process.env.TZ = 'UTC'; module.exports = { preset: 'jest-preset-angular', setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], reporters: [ 'default', 'jest-junit' ], testResultsProcessor: 'jest-junit', transformIgnorePatterns: [ '<rootDir>/node_modules/(?!lodash-es)', ], globals: { 'ts-jest': { tsconfig: '<rootDir>/src/tsconfig.spec.json', diagnostics: { ignoreCodes: [151001] } } }, // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }), modulePathIgnorePatterns: ['<rootDir>/cypress'] };
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
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:
C:\Users\ybena\Desktop\angularApp\SL.Web.App\node_modules\@datorama\akita\src\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export * from './lib/index';
^^^^^^
SyntaxError: Unexpected token 'export'
Please provide the environment you discovered this bug in
jest 27.3.1
akita 7.0.1
angular 12.2.13
Anything else?
No response
Do you want to create a pull request?
No
I've been getting the same error with angular and nx 13. Managed to fix it by changing
transformIgnorePatterns: ['node_modules/(?!.*.mjs$)']
to
transformIgnorePatterns: ['node_modules/(?!.*.mjs$|@datorama/akita)']
in jest.config.js of failing projects.
Adding it to the same regex is very important, it won't work if I keep it in separate strings.
@theorlovsky Your fix is working perfectly! Thank you very much!!!!
It works but running my tests (in nx) takes much longer
The repo is updated now. I'll release new versions soon.
@NetanelBasal I've upgraded a project to Akita 8.0.0 and I'm still getting this same issue with Jest (where it expects akita isn't going to do an export since it's a .js file). Is there something else that needs to be fixed on my end for this to work?
Not resolved in version 8.
@NetanelBasal Upgraded the @datorama/akita: 8.0.1, and updated
transformIgnorePatterns: [ 'node_modules/(?!.*.mjs$|@datorama/akita)'] in jest.config.ts
Unit tests take more time to run. Is there any fix?