I’ve refactored my code to load my helloworld page dynamically by setting up jest-next-dynamic.
I cannot figure out how to fix the error: “TestingLibraryElementError: Unable to find an element with the text: Hello, Worlds!. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.”
Jest.setup.js file
import preloadAll from "jest-next-dynamic";
beforeAll(async () => {
await preloadAll();
});
index.tsx file
import processDirectory from 'utils/processDirectory';
export default function Home(): ReactElement {
return <processDirectory.HelloWorld.Component />;
}
processDirectory.tsx file
import dynamic from 'next/dynamic';
const processDirectory = {
HelloWorld: {
Component: dynamic(() => import('components/apps/HelloWorld'))
}
};
export default processDirectory;
HelloWorld.tsx file
import type { FC } from 'react';
import styled from 'styled-components';
const Title = styled.h1`
color: ${({ theme }) => theme.colors.primary};
font-size: 50px;
text-align: center;
`;
// eslint-disable-next-line react/function-component-definition
const HelloWorld: FC = () => <Title>Hello, Worlds!</Title>;
export default HelloWorld;
index.spec.tsx file
import { render } from '@testing-library/react';
import Index from 'pages/index';
import { ThemeProvider } from 'styled-components';
import defaultTheme from 'themes/default.json';
test('renders index page', () => {
const { getByText } = render(
<ThemeProvider theme={defaultTheme}>
<Index />
</ThemeProvider>
);
// eslint-disable-next-line testing-library/prefer-screen-queries
const helloWorldElement = getByText('Hello, Worlds!');
expect(helloWorldElement).toBeInTheDocument();
});
package.json file
{
"name": "x",
"version": "1.0.0",
"description": "x",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"eslint": "eslint .",
"stylelint": "stylelint **/*.tsx",
"prettier": "prettier --write .",
"test": "jest",
"test:watch": "jest --watch"
},
"repository": {
"type": "git",
"url": "x"
},
"author": "x",
"dependencies": {
"jest-environment-jsdom": "^28.1.1",
"next": "^12.1.6",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"styled-components": "^5.3.5"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@types/jest": "^28.1.1",
"@types/node": "^17.0.41",
"@types/react": "^18.0.12",
"@types/styled-components": "^5.1.25",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"babel-plugin-styled-components": "^2.0.7",
"eslint": "^8.17.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^2.7.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.5.3",
"eslint-plugin-jest-dom": "^4.0.2",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-react": "^7.30.0",
"eslint-plugin-react-hooks": "^4.5.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"eslint-plugin-testing-library": "^5.5.1",
"husky": "^8.0.1",
"jest": "^28.1.1",
"jest-next-dynamic": "^1.0.1",
"lint-staged": "^13.0.1",
"prettier": "^2.6.2",
"stylelint": "^14.9.0",
"stylelint-config-prettier": "^9.0.3",
"stylelint-config-sass-guidelines": "^9.0.1",
"stylelint-config-standard": "^26.0.0",
"stylelint-config-styled-components": "^0.1.1",
"stylelint-processor-styled-components": "^1.10.0",
"typescript": "^4.7.3"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*": "prettier --write",
"*.{.js,ts,tsx}": "eslint --fix",
"*.tsx": "stylelint --fix"
}
}