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

aws-sdk-client-mock-jest 4.1.0 package.json exports causes "SyntaxError: Cannot use import statement outside a module" when using es modules #242

Open
1 task done
jeffbski-rga opened this issue Oct 15, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@jeffbski-rga
Copy link

jeffbski-rga commented Oct 15, 2024

Checklist

  • I have read Caveats documentation and didn't find a solution for this problem there.

Bug description

My jest tests were fine with aws-sdk-client-mock-jest@4.0.2 but as soon as I upgrade to 4.1.0 I am getting

Details:

/Users/s0046755/projects/adam-assumptions-model-inputs/node_modules/aws-sdk-client-mock-jest/dist/es/jest.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { __assign, __spreadArray } from "tslib";
                                                                                  ^^^^^^

SyntaxError: Cannot use import statement outside a module

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)

I am running in Node.js 20.17.0 and for jest I run it with NODE_OPTIONS=--experimental-vm-modules jest so I can use es modules.

Problem is resolved if package.json exports is removed

I was able to diff the aws-sdk-client-mock-jest package.json of 4.0.2 and 4.1.0 and found that the newer package.json includes an exports property. It appears that the inclusion of this is not playing well with jest causing the above error. If I manually remove the exports property from your package.json in node_modules/aws-sdk-client-mock-json then things once again work fine. So there must be some incompatibility with jest and the exports property.

Apparently the exports property was added to support the vitest feature.

Reproduction

Gist with all necessary code to reproduce the problem
https://gist.github.com/jeffbski-rga/97f15fbffb23aa7f91484da49aa7d21d

package.json

{
  "name": "aws-sdk-client-mock-jest-4.1.0-bug",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest \"--testMatch=**/*.mjs\" index.test.mjs"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "aws-sdk-client-mock": "4.1.0"
  },
  "devDependencies": {
    "aws-sdk-client-mock-jest": "4.1.0",
    "jest": "^29.7.0"
  }
}

index.test.mjs

import 'aws-sdk-client-mock-jest';

Reproduce in Node.js 20.17.0 by running npm t

Simply attempting to import the module will cause the error.

Environment

  • Node version: 20.17.0
  • Testing lib and version: jest
  • Typescript version:
  • AWS SDK v3 Client mock version: 4.1.0

Solution

Removing the exports property from the aws-sdk-client-mock-jest package.json fixes the problem.

The exports in 4.1.0 package.json (remove this to fix, but it may affect vitest feature since it was added for that)

  "exports": {
    ".": {
      "require": {
        "types": "./dist/types/jest.d.ts",
        "default": "./dist/cjs/jest.js"
      },
      "import": {
        "types": "./dist/types/jest.d.ts",
        "default": "./dist/es/jest.js"
      }
    },
    "./vitest": {
      "require": {
        "types": "./dist/types/vitest.d.ts",
        "default": "./dist/cjs/vitest.js"
      },
      "import": {
        "types": "./dist/types/vitest.d.ts",
        "default": "./dist/es/vitest.js"
      }
    }
  },

4.0.2 did not have exports and it works properly.

@jeffbski-rga jeffbski-rga added the bug Something isn't working label Oct 15, 2024
@jeffbski-rga jeffbski-rga changed the title aws-sdk-client-mock-jest 4.1.0 package.json exports causes SyntaxError: Cannot use import statement outside a module aws-sdk-client-mock-jest 4.1.0 package.json exports causes "SyntaxError: Cannot use import statement outside a module" when using es modules Oct 15, 2024
@thetumper
Copy link

thetumper commented Oct 18, 2024

Seeing a "might-be-related" issue: seems core jest functions are being "stomped" on by these, when webpacked. Result is jest module mocks (non-SDK) aren't working, with "moduleName.mockImplementation..." resulting in a ts-loader error: "Property 'mockImplementation' does not exist on type 'xyz'.". Seems similar to Similar to #180?

The places where I do non-SDK jest mocking are referencing ./node_modules/aws-sdk-client-mock-jest/dist/types/jest.d.ts instead of ./node_modules/@types/jest/index.d.ts. Going back to 4.0.2 fixes it.

@TheHolyWaffle
Copy link

TheHolyWaffle commented Nov 22, 2024

Both attw and publint do spot esm vs cjs related issues with the package.json configuration:

vs 4.0.2 (last known working version):

@matt-blanchette
Copy link

Workaround for 4.1.0, forcing Jest to resolve import as CJS:

moduleNameMapper: {
  '^aws-sdk-client-mock-jest$': '<rootDir>/node_modules/aws-sdk-client-mock-jest/dist/cjs/jest.js',

Jest documentation says ESM is detected on .mjs file type or package.json type:module, but neither are present

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants