ngx-daterangepicker-material
ngx-daterangepicker-material copied to clipboard
DateRangePicker in gives error on DayJS when running Jest
Versions
- Angular version: 14
- component version: "ngx-daterangepicker-material": "^6.0.4",
Describe the bug Everything was running until we updated the project to angular 14. When i run the tests i get this error returned
`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:
C:\Git\PIG-Angular-Shared\node_modules\dayjs\esm\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import * as C from './constant';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
at Object.<anonymous> (../../PIG-Angular-Shared/node_modules/ngx-daterangepicker-material/fesm2020/ngx-daterangepicker-material.mjs:7:19)`
Additional context We get this error in the main project. the PIG-Angular-Shared is a package we use in different projects. We also have tests in the PIG-Angular-Shared project, but there is no problem to run those tests. We have tried around 20 different things (change jest.config, tsconfig or different version of dateRangePicker) to get this fixed, but nothing seems to work.
Any update ? I am also facing the same issue in my app.
I found a way to fix it, add this to your jest.config.js
transformIgnorePatterns: ['node_modules/(?!dayjs|.*\\.mjs$)'],
More information about the issue: https://jestjs.io/docs/configuration#transformignorepatterns-arraystring
i have tried it. but i can't get it to work. i have included my jest.config.js. am i doing it correct? ;-)
`const { pathsToModuleNameMapper } = require('ts-jest'); const { paths } = require('../../tsconfig.json').compilerOptions;
module.exports = { preset: 'jest-preset-angular', moduleNameMapper: { ...pathsToModuleNameMapper(paths, { prefix: '<rootDir>' }) }, setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'], globalSetup: 'jest-preset-angular/global-setup', testRunner: 'jest-jasmine2', transformIgnorePatterns: ['node_modules/(?!dayjs|.*\.mjs$)'], reporters: [ 'default', [ 'jest-junit', { outputDirectory: 'test_reports', outputName: 'jest-junit.xml', }, ], ], globals: { 'ts-jest': { tsconfig: '<rootDir>/projects/pig-angular-shared/tsconfig.spec.json', isolatedModules: true, }, }, }; `
Any updates on this? I am running into the same issue
I added my solution here: https://github.com/fetrarij/ngx-daterangepicker-material/issues/479