ArnoSaine / suspense-overlay

5 stars 1 forks source link

Using Typescript, Jest fails to test any component containing <SuspenseOverlay /> #1

Open korbav opened 1 year ago

korbav commented 1 year ago

Example:

// App.tsx
...

function App() {
  return (
       <SuspenseOverlay>
          <SomeComponent />
       </SuspenseOverlay>
}

Then while testing with jest:

// App.test.tsx
import { render } from "@testing-library/react";

describe(('testing App') => {
    it('should render', () => {
        render(<App />);
        // ... assertions
    });
});

This simple test case will trigger the following error:

 FAIL  src/App.test.tsx
  ● 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:

    C:\Users\kbv\VS Code Projects\VEED.IO\node_modules\suspense-overlay\lib\main.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { jsx as _jsx } from "@emotion/react/jsx-runtime";
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { Box, CircularProgress, Container } from "@mui/material";
      2 | import { ErrorBoundary } from "react-error-boundary";
    > 3 | import Suspense from "suspense-overlay";
        | ^
      4 | import { useStore } from "./state/store";
      5 | import { AppStateContext } from "./context/AppStateContext";
      6 | import { Header } from "./components/Header";

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1495:14)
      at Object.<anonymous> (src/App.tsx:3:1)
      at Object.<anonymous> (src/App.test.tsx:2:1).
korbav commented 1 year ago

From my investigation,it seems that the problem is you somehow didn't include the tsconfig.json while publishing the package to npm

ArnoSaine commented 1 year ago

Hi, thanks for reporting the issue.

Which version of Jest are you using? Tests in latest CRA based app seem to work, but I found another issue with missing file extensions. It is now fixed in v1.0.2.

korbav commented 1 year ago

Hi @ArnoSaine ,

Unfortunately adding the .js to the imports is not the reason for the pre-quoted exception to be raised, I just update the package and it's the exact same.

I'm using jest 29.5.0 in conjunction with ts-jest

Note: I was able to make the package work with some modifications that you can find here :

https://github.com/korbav/suspense-overlay

In particular, I had to change "type": "module" to "type": "commonjs" in package.json