Jest testing Element type is invalid #35028
Replies: 5 comments 19 replies
-
Hello. Could you share the content of the It's likely that you forgot to export your component as default or that the render value is incorrect. Thank you! |
Beta Was this translation helpful? Give feedback.
-
I'm suffering from the same issue. There was no issue with Next.js 11 + Jest 27, but after I upgraded both to Next.js 12 + Jest 28 some of components fail the test with such error message. (of course there is nothing wrong in actual import statements because it has been used for a long time). |
Beta Was this translation helpful? Give feedback.
-
Has somebody able to solve this issue? |
Beta Was this translation helpful? Give feedback.
-
Are you all experiencing this with SVG imports? Please try: // jest.config.js
const nextJest = require("next/jest");
const createJestConfig = nextJest({
dir: "./",
});
/** @type {import('jest').Config} */
const customJestConfig = {
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},
// Since Jest 28, IIRC, this has to be installed separately
testEnvironment: "jest-environment-jsdom",
};
const jestConfigWithOverrides = async (...args) => {
const fn = createJestConfig(customJestConfig);
const res = await fn(...args);
res.moduleNameMapper = {
"\\.svg": "<rootDir>/src/__mocks__/svgMock.js",
...res.moduleNameMapper,
};
return res;
};
module.exports = jestConfigWithOverrides; And then define your mock as: // src/__mocks__/svgMock.js
// eslint-disable-next-line import/no-anonymous-default-export
const SvgUrl = () => "svgrurl";
export default SvgUrl; |
Beta Was this translation helpful? Give feedback.
-
that worked for me:
|
Beta Was this translation helpful? Give feedback.
-
I've faced with error
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports. Check the render method of
ContactUsPage
.during testing like
I have no idea why and I'd appreciate for any help here
If I try to render not the next.js page but the inside component only, I receive the same error for inner components. Like in
I haveimport { Row, useScreenClass } from 'react-grid-system'
Despite it is uncomfortable, I can change all destructure imports to defaults but with 3d-party libraries, there is no opportunity to do the same.
Beta Was this translation helpful? Give feedback.
All reactions