levino/mock-jwks

SyntaxError: Cannot use import statement outside a module

coler-j opened this issue · 8 comments

I am trying to import this into a NestJS project, within my jest tests. I get the following 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.
     • 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:

    /path/node_modules/mock-jwks/build/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import nock from 'nock';

I am using yarn 2 PnP

That's probably because it's a module now. Going esmodule is a little challenging, but doable. See the linked example in the readme. If you cannot get it to work, try version 1 which has the same API and features.

Version 1 currently is being flagged by DependaBot as one of the downstream dependencies has some security issues.

I'm currently going through the same issue described by OP, and your example did not help much.

Could you please elaborate?

You can only use version 2 if your project is a esmodule. To make your project an esmodule, I linked a guide. I cannot support you further on this since there is more than enough material out there. I linked a demo project that uses mock-jwks as an esmodule, which is only possible because the example project is an esmodule too. Esmodules are the future, so in general I would recommend upgrading.

I will address the security vulnerability in version 1 even though mock-jwks is supposed to be a development dependency of your project. If it (or its subdepedencies) have security issues, this should not affect your production code, so for me it is a kind of non-issue. But I will fix it anyhow.

Is #193 your issue @pliniodng ?

@pliniodng please try https://github.com/levino/mock-jwks/releases/tag/v1.0.10 which has been published to npmjs.org.

Yes, that is the issue. Thanks for releasing a patch version.

FYI, I am still seeing this issue in our ESM project:

 Details:

    /Users/kgrundl/src/fhir-auth/node_modules/mock-jwks/build/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { createJWKS, createKeyPair, signJwt } from './tools.js';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { FastifyInstance } from 'fastify';
      2 | import { URLSearchParams } from 'url';
    > 3 | import createJWKSMock from "mock-jwks";

We ended up writing our own test server but thought I would mention that the issue is still there.

levino commented

Thank you for reporting. Unfortunately it is impossible for me to debug as long as I do not have an example repo to reproduce the bug. There might be so many things that cause this error in a code base that I cannot see.

For your information: I test the usage of the library. I have no issues importing the code from npm. You might want to make a PR to the test repository which adds the new functionality which triggers your error. However my fist assumption is, that you have something misconfigured, please double check while comparing to that test repository.