kubetail-org / edge-csrf

CSRF protection library for JavaScript that runs on the edge runtime (with Next.js, SvelteKit, Express, Node-HTTP integrations)
MIT License
148 stars 9 forks source link

[Bug] Testing code that uses `edge-csrf` ends up in error `SyntaxError: Cannot use import statement outside a module` #2

Closed erickpatrick closed 1 year ago

erickpatrick commented 1 year ago

Version 0.2.1

Steps to reproduce The following public repository contains a minimum replication environment: https://github.com/erickpatrick/nextjs-jest-edge-csrf-minimum-reproduction

Description of the repository The repository is a Next.js app. It was setup using the Jest Testing documentation from Next.js docs website.

The project contains the <rootDir>/myFile.ts file which uses edge-csrf. A companion test file, <rootDir>/myFyle.test.ts, contains a minimum test file where we try to test <rootDir>/myFile.ts functionality.

Actual behavior When running npm run test, Jest throws the following error:

● 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:

    /Users/dealveri/Code/test/node_modules/edge-csrf/dist/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { Config } from './config';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      7 |

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1449:14)
      at Object.<anonymous> (myfile.ts:9:56)
      at Object.<anonymous> (myfile.test.ts:7:17)

Expected Behavior After running npm run test, Jest's logs from successful or failed test runs should display. In other words, the files from edge-csrf repository would provide files already transformed to a low level enough (CommonJS, perhaps) which would allow npm run test to behave as expected.

Additional Context Attention to the **SyntaxError: Cannot use import statement outside a module** message. I've tried to follow Jest's documentation on how to add files from node_modules to be transformed, but it did not work.

I have also tried to define a babel.config.js like in the documentation and configuring it in order to transform the files from edge-csrf. However, it also did not work.

Environment System:

Binaries:

amorey commented 1 year ago

Thanks for the bug report and the helpful test repo! I added commonjs support and published a release candidate here: https://www.npmjs.com/package/edge-csrf/v/0.2.2-rc1

Can you try out v0.2.2-rc1 and let me know if that fixes the problem? I can work on the Next.js 13 upgrade next.

erickpatrick commented 1 year ago

Hi @amorey, I'm checking it now. Thank you for you quick update :)

erickpatrick commented 1 year ago

Hi @amorey, I've just tested it and it works as expected now. Many thanks for your quick work!

amorey commented 1 year ago

Great! Happy to hear it fixed the problem. I published the changes as v0.2.2: https://www.npmjs.com/package/edge-csrf/v/0.2.2

In addition I just published a release candidate for a version that includes Next.js 13 support: https://www.npmjs.com/package/edge-csrf/v/1.0.0-rc1

If you get a chance, please try that one out as well and let me know if you notice any problems.

erickpatrick commented 1 year ago

Hi @amorey, I will test it this weekend with Next.js v13 and will feedback latest on Monday :)

Many thanks for your work :)