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

support testing with jest #10

Closed
Julian-B90 opened this issue Jun 3, 2021 · 6 comments
Closed

support testing with jest #10

Julian-B90 opened this issue Jun 3, 2021 · 6 comments
Assignees
Labels
needs investigation This issue needs additional investigation

Comments

@Julian-B90
Copy link
Contributor

it is possible you can import the local from date-fns/locale?

Because we i run my JEST test i get this error:

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.
     • 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:

    /frontend/node_modules/date-fns/esm/locale/index.js:2
    export { default as af } from "./af/index.js";
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1479:14)
      at node_modules/ngx-mat-datefns-date-adapter/bundles/ngx-mat-datefns-date-adapter.umd.js:2:191
      at Object.<anonymous> (node_modules/ngx-mat-datefns-date-adapter/bundles/ngx-mat-datefns-date-adapter.umd.js:5:2)
@sapozhnikovay
Copy link
Owner

Hello, @Julian-B90 ,

Could you please create a small reproduction repo or a Stackblitz ?

Also, I've looked through the Jest suggestions in the error message. have you tried disabling date-fns package from transformation via "transformIgnorePatterns" setting in Jest config?

@Julian-B90
Copy link
Contributor Author

Hey @sapozhnikovay i have create a git repo. I hope it helps.

Wenn you run npm run test you get the error.

https://github.com/Julian-B90/angular-jest-reproduce-ngx-mat-datefns-date-adapter

@sapozhnikovay
Copy link
Owner

Thanks, Julian. I'll look into the issue and let you know.

@sapozhnikovay
Copy link
Owner

Julian,

Right now you can include date-fns ES Module into jest transforming process via configuration.
Set this into jest.config.ts :

   transformIgnorePatterns: ['node_modules/(?!@ngrx|date-fns)'],

And this into compilerOptions object in tsconfig.ts :

    "allowJs": true

In the repo provided above this settings lead to successful compilation process(at first run it takes time =) and subsequent error in the test itself (lack of various imports in the configureTestingModule for the ngx-mat-datefns-date-adapter).

I'll examine more thoroughly the ESM exports in the date-fns and update my package to use plain exports in a couple of days.

@Julian-B90
Copy link
Contributor Author

@sapozhnikovay the options works thanks you. But you were right this test are now very slow.

@sapozhnikovay
Copy link
Owner

Fixed in version 11.2.2

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

No branches or pull requests

2 participants