snelsi / next-recaptcha-v3

⭐ Straightforward solution for using ReCaptcha in your Next.js application
MIT License
108 stars 8 forks source link

Next-recaptcha-v3 makes jest test cases fail #154

Closed hompiler closed 1 year ago

hompiler commented 1 year ago

Describe the bug when I try to run the test cases with this package installed the test cases fails

To Reproduce just install and use the package

Error Message ` 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:

C:\atomica\case-manager-client\node_modules\next-recaptcha-v3\lib\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { ReCaptcha } from './ReCaptcha.js';
                                                                                  ^^^^^^

SyntaxError: Unexpected token 'export'

  11 |   value?: string;
  12 |   onChange?: (v: string) => void;
> 13 | }) {
     |     ^
  14 |   const key = process.env.NEXT_PUBLIC_RECAPTCHA_KEY ?? '';
  15 |   return (
  16 |     <ReCaptchaProvider reCaptchaKey={key}>

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
  at Object.<anonymous> (src/components/ReCaptcha/ReCaptcha.tsx:13:26)
  at Object.<anonymous> (src/components/ReCaptcha/index.tsx:11:59)
  at Object.<anonymous> (src/features/Auth/SignUp/components/SignUpForm.tsx:22:20)
  at Object.<anonymous> (src/features/Auth/SignUp/components/signUpForm.test.tsx:23:60)

`

snelsi commented 1 year ago

This library is Pure ESM.

I suspect your jest setup doesn't support ESM by default. You would need to configure jest to fix this error.

Check these links for more info: https://github.com/facebook/jest/issues/13739 https://jestjs.io/docs/ecmascript-modules

hompiler commented 1 year ago

If I understand correctly ESM support is experimental and I should not use it in a production application

snelsi commented 1 year ago

Support for ESM has been out for quite some time now. I think it's stable enough to safely turn it on your production application without any major problems in the future

snelsi commented 1 year ago

Feel free to reopen this ticket, if you have any more questions