I have this component which uses react-use-wizard
The component looks like this
import { useWizard } from "react-use-wizard";
import { collection, getDocs } from "firebase/firestore";
import {
Box,
Text,
Flex,
Stack,
Radio,
RadioGroup,
Button,
Center,
} from "@chakra-ui/react";
import { useEffect, useState } from "react";
import { db } from "../../utils/fireStore";
import Image from "next/image";
const StepOne = ({ setEmirateDetails, emirateDetails }) => {
const [emirates, setEmirates] = useState([]);
const [selectedEmirate, setSelectedEmirate] = useState("");
const { previousStep, nextStep, isLastStep, isFirstStep } = useWizard();
const handleChange = (e) => {
return setSelectedEmirate(e.target.value);
};
const fetchEmirates = () => {
getDocs(collection(db, "emirates")).then((querySnapshot) => {
const newData = querySnapshot.docs.map((doc) => ({
...doc.data(),
id: doc.id,
}));
setEmirates(newData);
});
};
const getSelectedEmirateDetails = () => {
const _emirates = [...emirates];
const result = _emirates.find(
(_emirate) => _emirate?.name === selectedEmirate
);
return setEmirateDetails(result);
};
useEffect(() => {
fetchEmirates();
}, []);
useEffect(() => {
if (selectedEmirate) {
getSelectedEmirateDetails();
}
}, [selectedEmirate]);
return (
<Box>
<Flex alignItems="center">
<Box p={4} rounded="md" ml={100} height={"50vh"}>
<Text fontSize="3xl" fontWeight="bold">
In which emirate would you like to live in?
</Text>
<Flex pl={4} justifyContent="space-between">
<Box>
<RadioGroup defaultValue="1" name="emirate">
<Stack>
{emirates.map((_emirate) => (
<Radio
key={_emirate?.id}
size="lg"
name="emirate"
colorScheme="orange"
value={_emirate?.name}
onChange={handleChange}
>
{_emirate?.name}
</Radio>
))}
</Stack>
</RadioGroup>
</Box>
{emirateDetails?.image && (
<Box width="50%">
<Image src={emirateDetails?.image} width={250} height={150} />
<Text wordBreak="break-word">{emirateDetails?.overview}</Text>
</Box>
)}
</Flex>
</Box>
</Flex>
<Box>
<Center>
<Box>
<Button
mr={2}
onClick={() => previousStep()}
disabled={isFirstStep}
>
Previous
</Button>
<Button onClick={() => nextStep()} disabled={isLastStep}>
Next
</Button>
</Box>
</Center>
</Box>
</Box>
);
};
export default StepOne;
I have a test case written with jest that looks like this
import { render, screen } from '@testing-library/react';
import StepOne from '../components/Steps/StepOne';
test('renders StepOne component', () => {
render(<StepOne setEmirateDetails={() => {}} emirateDetails={{}} />);
expect(screen.getByText('Emirates')).toBeInTheDocument();
});
And my jest config looks like this
module.exports = {
testPathIgnorePatterns: ['<rootDir>/.next/', '<rootDir>/node_modules/'],
transform: {
"^.+\.m?jsx?$": "babel-jest",
"\.(jpg|jpeg|png|gif)$": "jest-transform-stub",
'\.[jt]sx?$': 'esbuild-jest'
},
testMatch: [
'**/spec/**/*.js?(x)', '**/?(*.)(spec|test).js?(x)',
'**/spec/**/*.mjs', '**/?(*.)(spec|test).mjs'
],
testPathIgnorePatterns: [
"/node_modules/",
"/dist/"
],
verbose: true,
collectCoverageFrom: [
"pages/**/*.js",
"tests/**/*.js",
"components/**/*.js"
],
testEnvironment: 'jsdom'
};
I also have my babel.rc that looks like this
{
"presets": ["next/babel"],
"plugins": []
}
but every time I run my test case I keep getting this error
● 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:
/Users/obafemiomotayo/Development/Personal/Personal/UEL-GRP-16/UELGroup16A-Property-search/node_modules/react-use-wizard/dist/react-use-wizard.mjs:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { createContext, useContext, memo, useState, useRef, Children, useCallback, useMemo, isValidElement, cloneElement, createElement } from 'react';
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | import { useWizard } from "react-use-wizard";
| ^
2 | import { collection, getDocs } from "firebase/firestore";
3 | import {
4 | Box,
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
at Object.require (components/Steps/StepOne.js:1:1)
at Object.require (tests/step-one.test.js:5:1)
The react-use-wizard module is transpiled to a .mjs files and it seems it cannot execute this line import { createContext, useContext, memo, useState, useRef, Children, useCallback, useMemo, isValidElement, cloneElement, createElement } from 'react';
in the code
Any Ideas, I have been stuck with this for days now
I have tried so many jest config manipulations and gynamstics but none seem to work. I am not sure what I should do next`