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

[Bug]: Cannot execute the defineConfig function in a Jest test #532

Open
patricklafrance opened this issue Dec 5, 2024 · 4 comments
Open
Labels
🐞 bug Something isn't working

Comments

@patricklafrance
Copy link

patricklafrance commented Dec 5, 2024

Version

System:
    OS: Windows 11 10.0.22631
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i7-13700H
    Memory: 9.70 GB / 31.62 GB
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @rsbuild/core: 1.1.8 => 1.1.8

Details

When executing the defineConfig function in a Jest test, I get the following error:

TypeError: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received 'http://localhost/main.js'

The code under test:

import { defineConfig } from "@rsbuild/core";

export function myDefineConfig() {
    return defineConfig({});
}

The test:

import { myDefineConfig } from "../src/myDefineConfig.ts";

test("my test", () => {
    const config = myDefineConfig();

    expect(config).toMatchSnapshot();
});

I found this example which inspired most of my reproduction configs. It seems like something changed since this example has been created.

Reproduce link

https://github.com/patricklafrance/rsbuild-jest

Reproduce Steps

  • pnpm install
  • pnpm test
@patricklafrance patricklafrance added the 🐞 bug Something isn't working label Dec 5, 2024
@chenjiahan chenjiahan transferred this issue from web-infra-dev/rsbuild Dec 6, 2024
@chenjiahan
Copy link
Member

Rslib generates the following code and it breaks Jest + JSDom:

let __rslib_import_meta_url__ = 'undefined' == typeof document ? new (require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;

@chenjiahan
Copy link
Member

@patricklafrance I think you should remove testEnvironment: "jsdom" from your Jest config file because you are testing a Node.js library, not a client library.

@patricklafrance
Copy link
Author

@chenjiahan oh that's embarrassing, indeed removing testEnvironment: "jsdom" fix the issue. Too much copy paste 😓

Should I leave this issue open since it doesn't work with testEnvironment: "jsdom" and the other issue you mentionned?

@chenjiahan
Copy link
Member

Never mind 😄

We should check whether the output of Rslib is reasonable and then decide whether to close this issue.

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

2 participants