Change JSX mode to "jsx": "react"
for tsconfig.json
.
Add ts-jest package
Modify the jest.config.ts
like this:
export default {
preset: "ts-jest/presets/js-with-ts",
testEnvironment: "jsdom",
clearMocks: true,
collectCoverage: true,
coverageDirectory: "coverage",
coverageProvider: "v8"
}
npm run test -- -t "Store"
Test result:
sandbox@sse-sandbox-91opf:/sandbox$ npm run test -- -t "Store"
> music-quiz@0.1.0 test /sandbox
> jest "-t" "Store"
FAIL util/store.test.tsx (32.906 s)
● Store: › Should work
TestingLibraryElementError: Unable to find an accessible element with the role "h1"
Here are the accessible roles:
heading:
Name "":
<h1 />
--------------------------------------------------
Ignored nodes: comments, <script />, <style />
<body>
<div>
<h1 />
</div>
</body>
6 | it("Should work", () => {
7 | render(<h1></h1>);
> 8 | expect(screen.getAllByRole("h1")).toBeInTheDocument();
| ^
9 | });
10 | it("definitely work", () => {
11 | expect(true).toBe(true);
at Object.getElementError (node_modules/@testing-library/dom/dist/config.js:38:19)
at node_modules/@testing-library/dom/dist/query-helpers.js:90:38
at node_modules/@testing-library/dom/dist/query-helpers.js:130:15
at Object.<anonymous> (util/store.test.tsx:8:19)
----------|---------|----------|---------|---------|------------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|------------------------------------
All files | 36.17 | 100 | 0 | 36.17 |
maths.ts | 11.11 | 100 | 0 | 11.11 | 1-5,7-9
notes.ts | 37.87 | 100 | 0 | 37.87 | ...46,63-94,98-111,113-116,118-132
----------|---------|----------|---------|---------|------------------------------------
Test Suites: 1 failed, 2 skipped, 1 of 3 total
Tests: 1 failed, 30 skipped, 1 passed, 32 total
Snapshots: 0 total
Time: 37.472 s, estimated 49 s
Ran all test suites with tests matching "Store".
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! music-quiz@0.1.0 test: `jest "-t" "Store"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the music-quiz@0.1.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/sandbox/.npm/_logs/2021-11-25T04_42_32_701Z-debug.log
Test environment setup correctly, but your test case failed.
CodeSandbox