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

Cannot find module @tanstack/angular-query-experimental in jest test #8364

Closed
ThiloAschebrock opened this issue Nov 28, 2024 · 7 comments
Closed
Labels
bug Something isn't working package: angular-query

Comments

@ThiloAschebrock
Copy link

Describe the bug

I am using Angulary Query in conjunction with test setup with jest. Ever since Angular Query version 5.60.0, all tests that import from @tanstack/angular-query-experimental fail with Cannot find module '@tanstack/angular-query-experimental':

Test suite failed to run

    Cannot find module '@tanstack/angular-query-experimental' from 'src/app/app.component.spec.ts'

      1 | import { TestBed } from '@angular/core/testing';
    > 2 | import {
        | ^
      3 |   provideTanStackQuery,
      4 |   QueryClient,
      5 | } from '@tanstack/angular-query-experimental';

      at new ModuleNotFoundError (node_modules/jest-resolve/build/ModuleNotFoundError.js:79:5)
      at Object.<anonymous> (src/app/app.component.spec.ts:2:1)

See minimal example at https://stackblitz.com/edit/qiyn4c. All versions up to 5.59.20 are able to import without issues though.

Your minimal, reproducible example

https://stackblitz.com/edit/qiyn4c

Steps to reproduce

  1. run npm test in the console

Expected behavior

Jest test able to import from @tanstack/angular-query-experimental.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Node 20, 22

Tanstack Query adapter

angular-query

TanStack Query version

v5.61.5

TypeScript version

v5.6.3

Additional context

I understand that this might require a configuration change in my test setup, but am still wondering what changes in 2459701 triggered this issues and if this was an intentional change.

@arnoud-dv
Copy link
Collaborator

This is probably due to Jest not directly supporting ES modules so these need to be transformed to commonjs.

I don't think 5.60.0 is actually the cut off point where it doesn't work anymore as I got that version working with Jest locally with your repro code.

If you have a choice I would suggest to move to Vitest, its API is almost identical to Jest, is much faster and has full support for ESM.

But many people still depend on Jest so I will look into this, if anyone has any ideas how to fix this please add a comment.

@arnoud-dv arnoud-dv added package: angular-query bug Something isn't working labels Nov 28, 2024
@arnoud-dv
Copy link
Collaborator

arnoud-dv commented Nov 28, 2024

Hi, @ThiloAschebrock this should be fixed in v5.61.6. I cannot reproduce it anymore with that version.

Please let me know if this solves it for you too.

@ThiloAschebrock
Copy link
Author

ThiloAschebrock commented Nov 28, 2024

Hi @arnoud-dv,
the issue is indeed resolved with v5.61.6. The cut off is definitely 5.60.0, I have tested it with several version to pinpoint the breaking point.

I would love to switch to vitest, but without switching to the analogjs platform this yet seems to be largely unsupported for Angular.

@ThiloAschebrock
Copy link
Author

ThiloAschebrock commented Nov 29, 2024

Actually, while this is fixing the import, it now throws the following issue when actually running a test that uses Tanstack Query with jest:

node:internal/modules/esm/utils:266
    throw new ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG();
          ^
 
TypeError: A dynamic import callback was invoked without --experimental-vm-modules
    at importModuleDynamicallyCallback (node:internal/modules/esm/utils:266:11)
    at EffectHandle.effectFn (node_modules/@tanstack/angular-query-experimental\src\providers.ts:288:11)

@arnoud-dv
Copy link
Collaborator

I changed the test in your repro to this:

import {fakeAsync, TestBed, tick} from '@angular/core/testing';
import {
    injectQuery,
    provideTanStackQuery,
    QueryClient,
} from '@tanstack/angular-query-experimental';

describe('TanstackQuery', () => {
    it('testing', fakeAsync(() => {
        TestBed.configureTestingModule({
            providers: [provideTanStackQuery(new QueryClient())],
        });

        const query = TestBed.runInInjectionContext(() =>
            injectQuery(() => ({
                queryKey: ['todos'],
                queryFn: () =>
                    Promise.resolve('some data')
            })),
        )

        tick()

        expect(query.data()).toBe('some data');
    }))
});

And it runs green. What version of Jest and Node are you using?

@arnoud-dv
Copy link
Collaborator

If I add withDevtools I do get the error but I don't see why anyone would need that in a unit test.

        TestBed.configureTestingModule({
            providers: [provideTanStackQuery(new QueryClient(), withDevtools())],
        });

@ThiloAschebrock
Copy link
Author

Thank you very much for investigating. You are correct that this error is triggered by including the dev tools. This came up for my project, as I had abstracted the the provider setup into a custom provider function so that I can easily run the tests with the same query client configuration as used in the application. Obviously, the dev tools part wasn't needed in the unit tests and, consequently, I was able to resolve this issue by splitting the application and test setup for TanStack Query.

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

No branches or pull requests

2 participants