react-hooks-testing-library icon indicating copy to clipboard operation
react-hooks-testing-library copied to clipboard

TypeError: Cannot read property 'prototype' of undefined

Open sebpowell opened this issue 4 years ago β€’ 8 comments

I've tried setting this up using the example provided in the documentation:

Screenshot 2021-10-10 at 20 45 57 Screenshot 2021-10-10 at 20 46 27

Whenever I try running the using Jest, I get the following error:

Screenshot 2021-10-10 at 20 47 13

It seems like it's something related to Babel, but I couldn't find anything about this error online. It also seems to be related to this package, as my other tests are running without any problems.

I'm using Next.js with Typescript (with the exception of the above test files, which I left in JS to rule out any compilation errors).

Any pointers would be greatly appreciated!

sebpowell avatar Oct 10 '21 19:10 sebpowell

My first idea would be to try and declare a custom babel config that is only used with your tests. So in your jest.config.js (if you haven't got one make one), you'd add:

module.exports = {
  transform: {
    '\\.js$': ['babel-jest', { configFile: './babel.config.jest.js' }]
  },
};

or whatever the custom babel config is and it's location.

Then create a new babel config that could look something like this:

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: 'current'
        }
      }
    ]
  ]
}

I'd start there, this could be an issue with nextjs inbuilt babel config. What version of next are you using?

joshuaellis avatar Oct 11 '21 07:10 joshuaellis

@joshuaellis – thanks for the suggestion! I gave that a go, but no luck – still getting the same error.

I'm using the latest version of Next.js (11.1.2).

sebpowell avatar Oct 11 '21 09:10 sebpowell

In case it helps, this is what my jest.config.js file looks like:

Screenshot 2021-10-11 at 10 30 57

And this is my .babelrc file (updated with your above suggestion): Screenshot 2021-10-11 at 10 31 48

sebpowell avatar Oct 11 '21 09:10 sebpowell

Hey @sebpowell, is this still an issue for you?

It looks to me like whatever version of @babel/runtime that is intalled in your root directory is not compatible with what react-error-boundary (our dependency) is expecting and it's not getting it's own version installed to use instead. That or something is causing it to resolve to the root version instead somehow (perhaps the next/babel preset?).

What does running npm ls @babel/runtime output for you (or the yarn equivalent)?

mpeyper avatar Oct 19 '21 14:10 mpeyper

Hey @mpeyper, thanks for your message!

It is, yes – I haven't yet found the solution. It definitely looks like it's related to Babel. I've tried uninstalling / reinstalling, but no luck so far. I'm going to try setting it up in a completely new Next.js project to rule out any problems caused by other packages, but I haven't got round to it yet.

Here's what npm ls @babel/runtime gives me – let me know if you can spot anything:

β”œβ”€β”¬ @reduxjs/[email protected]
β”‚ └─┬ [email protected]
β”‚   └── @babel/[email protected] deduped
β”œβ”€β”¬ @storybook/[email protected]
β”‚ β”œβ”€β”¬ @storybook/[email protected]
β”‚ β”‚ β”œβ”€β”¬ [email protected]
β”‚ β”‚ β”‚ └── @babel/[email protected] deduped
β”‚ β”‚ └─┬ [email protected]
β”‚ β”‚   └── @babel/[email protected] deduped
β”‚ β”œβ”€β”¬ @storybook/[email protected]
β”‚ β”‚ β”œβ”€β”¬ @emotion/[email protected]
β”‚ β”‚ β”‚ └── @babel/[email protected] deduped
β”‚ β”‚ β”œβ”€β”¬ @emotion/[email protected]
β”‚ β”‚ β”‚ └─┬ @emotion/[email protected]
β”‚ β”‚ β”‚   └── @babel/[email protected] deduped
β”‚ β”‚ └─┬ [email protected]
β”‚ β”‚   └── @babel/[email protected] deduped
β”‚ β”œβ”€β”¬ [email protected]
β”‚ β”‚ └── @babel/[email protected] deduped
β”‚ └─┬ [email protected]
β”‚   └── @babel/[email protected] deduped
β”œβ”€β”¬ @storybook/[email protected]
β”‚ └─┬ @storybook/[email protected]
β”‚   β”œβ”€β”¬ @babel/[email protected]
β”‚   β”‚ └─┬ @babel/[email protected]
β”‚   β”‚   └─┬ [email protected]
β”‚   β”‚     └── @babel/[email protected] deduped
β”‚   └─┬ @storybook/[email protected]
β”‚     β”œβ”€β”¬ @storybook/[email protected]
β”‚     β”‚ └─┬ [email protected]
β”‚     β”‚   └── @babel/[email protected] deduped
β”‚     └─┬ [email protected]
β”‚       └── @babel/[email protected] deduped
β”œβ”€β”¬ @storybook/[email protected]
β”‚ └─┬ @storybook/[email protected]
β”‚   β”œβ”€β”¬ [email protected]
β”‚   β”‚ └── @babel/[email protected] deduped
β”‚   β”œβ”€β”¬ [email protected]
β”‚   β”‚ └── @babel/[email protected] deduped
β”‚   β”œβ”€β”¬ [email protected]
β”‚   β”‚ β”œβ”€β”€ @babel/[email protected] deduped
β”‚   β”‚ └─┬ [email protected]
β”‚   β”‚   └── @babel/[email protected] deduped
β”‚   β”œβ”€β”¬ [email protected]
β”‚   β”‚ └── @babel/[email protected] deduped
β”‚   └─┬ [email protected]
β”‚     └── @babel/[email protected] deduped
β”œβ”€β”¬ @storybook/[email protected]
β”‚ β”œβ”€β”¬ @storybook/[email protected]
β”‚ β”‚ β”œβ”€β”¬ [email protected]
β”‚ β”‚ β”‚ └── @babel/[email protected] deduped
β”‚ β”‚ └─┬ [email protected]
β”‚ β”‚   └── @babel/[email protected] deduped
β”‚ └─┬ [email protected]
β”‚   └─┬ [email protected]
β”‚     └── @babel/[email protected] deduped
β”œβ”€β”¬ @testing-library/[email protected]
β”‚ β”œβ”€β”€ @babel/[email protected]
β”‚ └─┬ [email protected]
β”‚   └── @babel/[email protected] deduped
β”œβ”€β”¬ @testing-library/[email protected]
β”‚ β”œβ”€β”€ @babel/[email protected] deduped
β”‚ └─┬ @testing-library/[email protected]
β”‚   └── @babel/[email protected] deduped
β”œβ”€β”¬ @testing-library/[email protected]
β”‚ └── @babel/[email protected] deduped
β”œβ”€β”¬ [email protected]
β”‚ └── @babel/[email protected]
β”œβ”€β”¬ [email protected]
β”‚ └── @babel/[email protected] deduped
β”œβ”€β”¬ [email protected]
β”‚ β”œβ”€β”€ @babel/[email protected] deduped
β”‚ └─┬ [email protected]
β”‚   └── @babel/[email protected] deduped
β”œβ”€β”¬ [email protected]
β”‚ └── @babel/[email protected]
β”œβ”€β”¬ [email protected]
β”‚ └── @babel/[email protected] deduped
β”œβ”€β”¬ [email protected]
β”‚ └── @babel/[email protected] deduped
β”œβ”€β”¬ [email protected]
β”‚ β”œβ”€β”€ @babel/[email protected] deduped
β”‚ └─┬ [email protected]
β”‚   └── @babel/[email protected]
└─┬ [email protected]
  └── @babel/[email protected] deduped

sebpowell avatar Oct 20 '21 06:10 sebpowell

Have you seen this? https://nextjs.org/docs/testing#manual-setup-1

I think you need to modify your transform key in the jest-config to include next/babel:

transform: {
    /* Use babel-jest to transpile tests with the next/babel preset
    https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object */
    '^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
  },

joshuaellis avatar Oct 20 '21 07:10 joshuaellis

@joshuaellis – thanks for the above! Good suggestion, I gave it a go, but same things happens. This is what my jest.config.js file looked like after the changes:

module.exports = {
  collectCoverageFrom: [
    "**/*.{js,jsx,ts,tsx}",
    "!**/*.d.ts",
    "!**/node_modules/**",
  ],
  moduleDirectories: ["node_modules", "./"],
  setupFilesAfterEnv: ["<rootDir>/config/setupTests.js"],
  testPathIgnorePatterns: ["/node_modules/", "/.next/"],
  transform: {
    /* Use babel-jest to transpile tests with the next/babel preset
    https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object */
    '^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
  },

  testEnvironment: "jest-environment-jsdom",
  transformIgnorePatterns: [
    "/node_modules/",
    "^.+\\.module\\.(css|sass|scss)$",
  ],
  moduleNameMapper: {
    "\\.(css|less|scss|sass)$": "identity-obj-proxy",
  },
};

This weekend I'll try getting it up and running in a fresh Next.js project to see if it might have anything to do with other packages, will let you know if I find anything (or let me know if you have any other ideas of what it might be).

sebpowell avatar Oct 21 '21 06:10 sebpowell

εŽι’ζ˜―ζ€ŽδΉˆθ§£ε†³ηš„ε‘’

chuntaoXu avatar Jul 22 '22 02:07 chuntaoXu

Closing due to inactivity

joshuaellis avatar Sep 07 '22 07:09 joshuaellis