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

MedusaTestRunner, medusa-test-utils -> Loaders for module Workflows failed: Method Map.prototype.set called on incompatible receiver #<Map> #9350

Closed
jbrigbyjs opened this issue Sep 27, 2024 · 8 comments

Comments

@jbrigbyjs
Copy link

Bug report

Loaders failing when you have multiple test runners in different test files.
error appear always on second test runner

Zrzut ekranu z 2024-09-27 12-08-30

reproduce
npm run test:integration:http

System information

Medusa version (including plugins): preview, 2.0.10
Node.js version: 20
Database: postgres 16
Operating system: ubuntu 22

  ●  › TEST: Create accounts › should authorize vendor by emailpass provider

    Loaders for module Workflows failed: Method Map.prototype.set called on incompatible receiver #<Map>

      at runLoaders (node_modules/@medusajs/modules-sdk/src/loaders/utils/load-internal.ts:376:14)
      at loadInternalModule (node_modules/@medusajs/modules-sdk/src/loaders/utils/load-internal.ts:154:17)
      at loadModule (node_modules/@medusajs/modules-sdk/src/loaders/module-loader.ts:85:10)
      at moduleLoader (node_modules/@medusajs/modules-sdk/src/loaders/module-loader.ts:21:32)
      at Function.bootstrap (node_modules/@medusajs/modules-sdk/src/medusa-module.ts:362:7)
      at node_modules/@medusajs/modules-sdk/src/medusa-app.ts:131:23
          at async Promise.all (index 2)
      at loadModules (node_modules/@medusajs/modules-sdk/src/medusa-app.ts:97:3)
      at MedusaApp_ (node_modules/@medusajs/modules-sdk/src/medusa-app.ts:351:22)
      at MedusaApp (node_modules/@medusajs/modules-sdk/src/medusa-app.ts:545:10)
      at MedusaAppLoader.load (node_modules/@medusajs/framework/src/medusa-app-loader.ts:245:23)
      at Object.beforeAll_ (node_modules/medusa-test-utils/src/medusa-test-runner.ts:134:25)

  ●  › TEST: Create accounts › should authorize vendor by emailpass provider

    Loaders for module Workflows failed: Method Map.prototype.set called on incompatible receiver #<Map>

      at runLoaders (node_modules/@medusajs/modules-sdk/src/loaders/utils/load-internal.ts:376:14)
      at loadInternalModule (node_modules/@medusajs/modules-sdk/src/loaders/utils/load-internal.ts:154:17)
      at loadModule (node_modules/@medusajs/modules-sdk/src/loaders/module-loader.ts:85:10)
      at moduleLoader (node_modules/@medusajs/modules-sdk/src/loaders/module-loader.ts:21:32)
      at Function.bootstrap (node_modules/@medusajs/modules-sdk/src/medusa-module.ts:362:7)
      at node_modules/@medusajs/modules-sdk/src/medusa-app.ts:131:23
          at async Promise.all (index 2)
      at loadModules (node_modules/@medusajs/modules-sdk/src/medusa-app.ts:97:3)
      at MedusaApp_ (node_modules/@medusajs/modules-sdk/src/medusa-app.ts:351:22)
      at MedusaApp (node_modules/@medusajs/modules-sdk/src/medusa-app.ts:545:10)
      at MedusaAppLoader.load (node_modules/@medusajs/framework/src/medusa-app-loader.ts:245:23)
      at Object.beforeAll_ (node_modules/medusa-test-utils/src/medusa-test-runner.ts:134:25)

  ●  › TEST: Create accounts › should authorize vendor by emailpass provider

    Loaders for module StockLocation failed: Method Map.prototype.set called on incompatible receiver #<Map>

      at runLoaders (node_modules/@medusajs/modules-sdk/src/loaders/utils/load-internal.ts:376:14)
      at loadInternalModule (node_modules/@medusajs/modules-sdk/src/loaders/utils/load-internal.ts:154:17)
      at loadModule (node_modules/@medusajs/modules-sdk/src/loaders/module-loader.ts:85:10)
      at moduleLoader (node_modules/@medusajs/modules-sdk/src/loaders/module-loader.ts:21:32)
      at Function.bootstrap (node_modules/@medusajs/modules-sdk/src/medusa-module.ts:362:7)
      at node_modules/@medusajs/modules-sdk/src/medusa-app.ts:131:23
          at async Promise.all (index 3)

@bwmirek
Copy link

bwmirek commented Oct 1, 2024

Same issue for 1st Oct RC version. Noticed that removing --runInBand from command solves the issue for me.

@damianr13
Copy link

I was wondering why was this issue closed. I still experience issues in v2.0.1.

As @bwmirek, removing --runInBand fixes it, but this is more of a workaround than a solution. I also noticed that if I ran it on less powerful machines (in Github Actions) jest uses only one worker by default, and it acts the same as if I passed --runInBand.

Is this something you are going to look into?

@riqwan
Copy link
Contributor

riqwan commented Nov 25, 2024

Hey, yes we're looking into this. We tracked the bug down to jest, but looking to see if we can fix it on our end. cc @damianr13 @bwmirek @jbrigbyjs

We have created a ticket to track this, will update here when we have a fix.

@dwene
Copy link
Contributor

dwene commented Dec 6, 2024

@riqwan any luck on this? :)

@riqwan
Copy link
Contributor

riqwan commented Dec 10, 2024

Apologies for the long delay. This was a pain to debug, but we have a workaround.

Real quick, the reason we are here is because:

  1. Jest mocks node globals - including Map
  2. MikroORM works a lot with Maps and at some point, it does a property instanceof Map, which fails, because its no longer an actual Map as jest has mocked it
  3. On the first run mikroorm generates the metadata from scratch, everything works peachy.
  4. On the second run, upon running the second init script, the metadata of the entities is picked up from storage
  5. MikroORM does a cloning process that relies on instanceof Map, which fails and converts the Map to an object.
  6. Any code path that relies on it being a Map, now fails.

The workaround here is to basically clear the storage on every run and force mikroorm to generate the metadata instead of relying on the one from storage, which is incorrect due to the incorrect cloning that took place.

Workaround:

  • In your jest.config.js , add this: `setupFiles: ["./integration-tests/setup.js"]
  • Create a new file - integration-tests/setup.js
  • Add the following:
const { MetadataStorage } = require("@mikro-orm/core");

MetadataStorage.clear();

Let me know if that works.

There are some questions that are unanswered here, but we will revisit that once we've migrated to the latest mikroorm.

cc @dwene @jbrigbyjs @damianr13 @bwmirek

@dwene
Copy link
Contributor

dwene commented Dec 11, 2024

Thank you so much! I will give it a shot tomorrow.

@riqwan
Copy link
Contributor

riqwan commented Dec 11, 2024

Nice, let me know how it goes! @dwene

@dwene
Copy link
Contributor

dwene commented Dec 11, 2024

@riqwan yep that worked, thanks! I was able to use a typescript setup file.

setup.ts

import { MetadataStorage } from '@mikro-orm/core';

MetadataStorage.clear();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants