Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup Jest + React testing library using NextJS in TypeScript -- error setting upp jest.config.js

I am trying to set up Jest with a NextJS application, currently in jest.config.js:

module.exports = {
    testPathIgnorePatterns: ["<rootDir>/.next/", "node_modules/"],
    setupFilesAfterEnv: ["<rootDir>/__tests__/setupTests.tsx"],
    transform: {
      "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/babel-jest",
      "\\.(css|less|scss|sass)$": "identity-obj-proxy"
    }
  };

In my setupTests.tsx I have:

import "@testing-library/jest-dom/extend-expect";

However when I run npm run test I get the following error:

  Module identity-obj-proxy in the transform option was not found.
         <rootDir> is: ....

If I remove the line in jest.config.js I get:

Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

globals.css:2
    body {
         ^

    SyntaxError: Unexpected token '{'

    > 5 | import "../styles/globals.css";

What configuration should I use for the css and scss files?

** EDIT **

I made sure that identity-obj-proxy is in my devDependencies however, now I get an error:

Test suite failed to run

   Test suite failed to run

    TypeError: Jest: a transform must export a `process` function.

    > 5 | import "../styles/globals.css";
        | ^
      6 | import React from "react";
like image 546
ashes999 Avatar asked Jul 09 '26 21:07

ashes999


1 Answers

You need to install identity-obj-proxy. Run >>> yarn add -D identity-obj-proxy

like image 85
luisdelgado Avatar answered Jul 13 '26 21:07

luisdelgado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!