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] Upgrading to v3.1.0 breaks Jest #627

Open
amattu2 opened this issue Aug 5, 2024 · 1 comment
Open

[BUG] Upgrading to v3.1.0 breaks Jest #627

amattu2 opened this issue Aug 5, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@amattu2
Copy link

amattu2 commented Aug 5, 2024

Describe the bug

After updating from v2.16.0 to v3.1.0, no Jest test files will execute.

    /Users/ExampleUser/Downloads/my-app/node_modules/usehooks-ts/dist/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { useState, useCallback, useLayoutEffect, useEffect, useRef, useMemo } from 'react';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import React from 'react';
    > 2 | import { useBoolean } from 'usehooks-ts';

To Reproduce

This can be reproduced using the base create-react-app project, adding the usehooks-ts dependency, and then executing the base test case after importing usehooks-ts.

Setup Project

npx create-react-app my-app --template typescript

Add usehooks-ts Dependency

npm i usehooks-ts

Import the package

Modify the App.tsx file to import usehooks-ts. The hook doesn't matter, this example uses useBoolean

import React from 'react';
import { useBoolean } from 'usehooks-ts';
import logo from './logo.svg';
import './App.css';

function App() {
  const example = useBoolean(true);

  // .... existing code
}

Run Tests

npm run test

The tests will now fail. If you comment out the import, it works as expected.

Expected behavior

Upgrading to/beyond v3.0.0 should not impact existing projects or the execution of tests using Jest.

Additional context

Likely related to #502

@eryue0220
Copy link

Currently, jest does not support esm syntax by default. If you want to test esm module, update the test script like the following code:

{
  "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
}

More detail can check: https://jestjs.io/docs/ecmascript-modules

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