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

react: TypeError during tests with Vitest + Recoil #203

Open
theohagos opened this issue Dec 1, 2024 · 16 comments
Open

react: TypeError during tests with Vitest + Recoil #203

theohagos opened this issue Dec 1, 2024 · 16 comments

Comments

@theohagos
Copy link

@module-federation/[email protected]
[email protected],
[email protected]

When running test the following error is thrown
TypeError: Method Promise.prototype.then called on incompatible receiver [object Module]

@theohagos
Copy link
Author

Upgraded to [email protected] but the end result is the same :(

@gioboa
Copy link
Collaborator

gioboa commented Dec 2, 2024

Hi @theohagos can you create a basic example with that shows this issue please?

@theohagos
Copy link
Author

theohagos commented Dec 7, 2024

Hello @gioboa, thanks for your response.

The project is internal to JPMorgan, so afraid won't be able to post an exact replica but I have narrowed it down to recoiljs that is causing this error. I am also using nx workspace with rspack module federation to create remote MFEs. These apps also use recoil with vitest and the tests run successfully without this error.

@gioboa
Copy link
Collaborator

gioboa commented Dec 7, 2024

So the problem is the combination mf/vite + recoiljs + vitest, right?

@theohagos
Copy link
Author

Hello @gioboa,

Yes exactly that. Remove recoiljs test passes, add recoiljs back and then test throws above error. But there is no error when running the app, it works, it consumes remotes and no issues there.

Funnily enough I have the opposite problem with originjs/vite-plugin-federation, that is what led me to this plugin. With origin, tests pass but the app fails with this error Cannot read properties of null (reading 'useState'), looks like it is loading multiple version of react.

@gioboa
Copy link
Collaborator

gioboa commented Dec 8, 2024

looks like it is loading multiple version of react.

You can share react dep to avoid that problem, but idk if you can do it with that plugin

@gioboa gioboa changed the title Tests failure when using Vitest and module-federation/vite together react: TypeError with Vitest + Recoil Dec 8, 2024
@gioboa gioboa changed the title react: TypeError with Vitest + Recoil react: TypeError during tests with Vitest + Recoil Dec 8, 2024
@gioboa
Copy link
Collaborator

gioboa commented Dec 8, 2024

Have you tried with different Node versions?

@deizianens
Copy link

I have the same error when running Vitest + mf/vite

My versions:
[email protected],
[email protected]

I'm not using Recoil. Tested with node versions 16, 18 and 20. All of them outputs the same error message:
Captura de Tela 2024-12-09 às 09 47 03

I tested using vi.doMock and Error: [vitest] There was an error when mocking a module. If you are using "vi.mock" factory, make sure there are no top level variables inside, since this call is hoisted to top of the file. Read more: https://vitest.dev/api/vi.html#vi-mock message disappears but TypeError: Method Promise.prototype.then called on incompatible receiver [object Module] continues.

@theohagos
Copy link
Author

Hi @gioboa,

so it looks like it may not be specific to recoiljs since @deizianens is ALSO facing the same error without using recoiljs?

@gioboa
Copy link
Collaborator

gioboa commented Dec 9, 2024

Hi @deizianens , can you create a basic example that reproduce the issue please?

@deizianens
Copy link

deizianens commented Dec 9, 2024

Sure @gioboa
You can reproduce the error TypeError: Method Promise.prototype.then called on incompatible receiver [object Module] with:
https://github.com/deizianens/mfe-vite

When running yarn test the error above is raised:
Captura de Tela 2024-12-09 às 13 58 53

@mgreca
Copy link

mgreca commented Dec 12, 2024

It is happening to me too, my tests were working with originjs/vite-plugin-federation and when I replaced it with this library, they started to fail, but I don't use Recoil, only vitest

@saraiba33
Copy link

It is happening to me too, my tests were working with originjs/vite-plugin-federation and when I replaced it with this library, they started to fail, but I don't use Recoil, only vitest

When you were using originjs/vite-plugin-federation was there any additional setup or mocking you had to do to get the tests to pass? I have tried both and both seem to have issues with the tests. I'm getting the same error. Unfortunately, it is for my employer, and can not share specifics

@mgreca
Copy link

mgreca commented Dec 13, 2024

@saraiba33 I haven't changed anything, but what I did to skip this issue was to avoid federation when running tests, just doing the following in vite.config:

 plugins: [
      ...
      mode !== 'test' &&
        federation({
     ...

@gioboa
Copy link
Collaborator

gioboa commented Dec 19, 2024

I tested the @deizianens example https://github.com/deizianens/mfe-vite
and without federation.shared config the error is gone. I'll investigate more in the next days.

@JamesMHenderson
Copy link
Contributor

I get the same error when I include a Suspense around my component

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

No branches or pull requests

6 participants