Created
January 11, 2024 12:29
-
-
Save mizchi/43980c5412378924362f195c3fd89f51 to your computer and use it in GitHub Desktop.
deno で react testing library を使う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// deno test --no-check --allow-env counter.tsx | |
import { useCallback, useState } from "react"; | |
// increment counter by click | |
export default function Counter() { | |
const [count, setCount] = useState(0); | |
const onClick = useCallback(() => setCount(count + 1), [count]); | |
return <div> | |
<span data-testid="count">{count}</span> | |
<button type="button" onClick={onClick} data-testid="increment-button">+</button> | |
</div>; | |
} | |
if (!import.meta.main) { | |
await import("npm:global-jsdom/register"); | |
const { expect } = await import("https://deno.land/std@0.211.0/expect/expect.ts"); | |
const { userEvent } = await import('@testing-library/user-event'); | |
const { render, screen } = await import('@testing-library/react'); | |
document.body.innerHTML = '<div id="root"></div>'; | |
Deno.test("Usage", async () => { | |
render(<Counter />); | |
expect(screen.getByTestId('count').textContent).toEqual('0'); | |
await userEvent.click(screen.getByText('+')); | |
expect(screen.getByTestId('count').textContent).toEqual('1'); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"compilerOptions": { | |
"lib": [ | |
"deno.window", | |
"deno.ns", | |
"deno.fetch", | |
"dom", | |
"dom.iterable", | |
"esnext" | |
], | |
"jsx": "react-jsx", | |
"jsxImportSource": "react" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"devDependencies": { | |
"@testing-library/react": "^14.1.2", | |
"@testing-library/user-event": "^14.5.2", | |
"@types/react": "^18.2.47", | |
"@types/react-dom": "^18.2.18", | |
"react": "^18.2.0", | |
"react-dom": "^18.2.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment