m-radzikowski / aws-sdk-client-mock

AWS JavaScript SDK v3 mocks for easy unit testing. πŸ–‹οΈ Typed πŸ”¬ Tested πŸ“„ Documented πŸ› οΈ Maintained
https://m-radzikowski.github.io/aws-sdk-client-mock/
MIT License
810 stars 40 forks source link

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 jeffbski-rga opened 1 month ago

jeffbski-rga commented 1 month ago

Checklist

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

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.

thetumper commented 1 month ago

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 https://github.com/m-radzikowski/aws-sdk-client-mock/issues/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.