Created with pnpm create vite
-
Install
jest
and@types/jest
pnpm add -D jest @types/jest
-
Add script
test
"test": "jest --watchAll"
-
Run test
In
getGreeting.test.js
:SyntaxError: Cannot use import statement outside a module
Configure Babel to support
ES6
inNode
. Here we usebabel-jest
to perform the conversions. https://jestjs.io/docs/getting-started#using-babelInstall
babel-jest
@babel/core
@babel/preset-env
:pnpm add -D babel-jest @babel/core @babel/preset-env
And Configure Babel to target your current version of Node by creating a babel.config.js file in the root of your project.
babel.config.js
In
getGreeting.test.js
You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.
babel.config.cjs.
cjs
is what is applicable for Nodejs when using"type"="module"
-
Run test (with empty test suite)
Information.test.js
import Information from '../../src/components/Information'; describe('Test in <Information />', () => { test('should render component', () => { }) })
In
Information.jsx
:SyntaxError: Support for the experimental syntax 'jsx' isn't currently enabled
Configure Babel to support react syntax (
jsx
) inNode
.Install
@babel/preset-react
:pnpm add -D @babel/preset-react
And add config to
babel.config.cjs
-
To render component in testing, we will use
React Testing Library
Install
react-testing-library
:pnpm add -D @testing-library/react
-
Re-run test (with component rendered)
Information.test.js
import { render, screen } from "@testing-library/react"; import Information from '../../src/components/Information'; describe('Test in <Information />', () => { test('should render component', () => { render(<Information title='Hi!' name='Franz' />); screen.debug(); }) })
In
Information.jsx
:The error below may be caused by using the wrong test environment, see see https://jestjs.io/docs/configuration#testenvironment-string. Consider using the "jsdom" test environment.
Create
jest.config.cjs
file in the root of your project.Install
jest-environment-jsdom
:pnpm add -D jest-environment-jsdom
Add setup
testEnvironment
tojest-environment-jsdom
injest.config.cjs
.
-
Install
whatwg-fetch
;pnpm add -D whatwg-fetch
-
Import
whatwg-polyfill
in a setup file provided injest.config.cjs
.