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: viem 1.15.5 and above (isows) breaks jest #1329

Closed
1 task done
mlenser opened this issue Oct 10, 2023 · 4 comments
Closed
1 task done

bug: viem 1.15.5 and above (isows) breaks jest #1329

mlenser opened this issue Oct 10, 2023 · 4 comments

Comments

@mlenser
Copy link

mlenser commented Oct 10, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Package Version

1.15.5

Current Behavior

Hi, since viem switched to isows as part of 1.15.5 jest is unable to handle anything inside viem.

This is likely an upstream issue that needs to be solved in isows.

Error:

 Test suite failed to run
                                                                                                                                                                               
    Jest encountered an unexpected token                                                                                                                                       
                                                                                                                                                                               
    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.                                                                                                                                                                              
                                                                                                                                                                               
    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.                                            
                                                                                                                                                                               
    By default "node_modules" folder is ignored by transformers.                                                                                                               
                                                                                                                                                                               
    Here's what you can do:                                                                                                                                                    
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.                                                        
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript                                                                        
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.                                               
     • If you need a custom transformation specify a "transform" option in your config.                                                                                        
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    REPO_PATH\node_modules\isows\_esm\native.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { getNativeWebSocket } from "./utils.js";

We're running jest the recommended way by NextJS which does not transform node_modules.

If I add the following to my jest.config.mjs it works:

const config = {
  // other config
  moduleNameMapper: {
    isows: '<rootDir>/node_modules/isows/_cjs/index.js',
  },
  transformIgnorePatterns: ['node_modules/(?!isows/)'],
};

Expected Behavior

Jest is able to use viem utils as part of the components it tests.

Steps To Reproduce

A reproduction would be a bit time consuming to set up. If desired, I can do so.

Link to Minimal Reproducible Example (StackBlitz, CodeSandbox, GitHub repo etc.)

No response

Anything else?

No response

@jxom
Copy link
Member

jxom commented Oct 10, 2023

Jest has pretty poor support for CJS + ESM interop at the moment. I think they have plans to make this better. I'd consider your workaround fine.

I'd highly recommend a migration to Vitest though. It's relatively straight forward, and worth it.

@jxom jxom closed this as not planned Won't fix, can't repro, duplicate, stale Oct 10, 2023
@katspaugh
Copy link

katspaugh commented Oct 13, 2023

For anyone in the same boat, you can bypass this by mocking the isows module.
Disregard that, see the workaround in the issue description.

@mlenser
Copy link
Author

mlenser commented Oct 13, 2023

You don't need to mock it. The provided jest.config.mjs in the issue fixes the issue while using isows from node_modules.

Copy link
Contributor

This issue has been locked since it has been closed for more than 14 days.

If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Viem version. If you have any questions or comments you can create a new discussion thread.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants