Replies: 1 comment 3 replies
-
Adding some findings. Apparently, ESM.sh has a way to import a dependency as if it was built with the https://cdn.esm.sh/#development-mode import "https://esm.sh/raf/polyfill";
import React from "https://esm.sh/[email protected]";
import { act, create } from "https://esm.sh/[email protected]?dev";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
const GreeterComponent: React.FC<{ name: string }> = ({ name }) =>
<h1>Hello, {name}!</h1>;
Deno.test("react component test", () => {
const expectedText = "Hello, World!";
let rendered: ReturnType<typeof create> = null as unknown as ReturnType<
typeof create
>;
act(() => {
rendered = create(<GreeterComponent name="World" />);
});
const h1 = rendered.root.findByType("h1");
const result = h1.props.children.join("");
assertEquals(result, expectedText);
}); |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It is well-documented how to use React with Deno. However, it is not well-documented on how to go about unit testing components. At first impulse, it seems the simplest solution would be to use the
react-test-renderer
.However, the
act
method only works if the global variable__DEV__
is truthy (link)When
__DEV__
is falsy,act
outputs the following error message:Note:⚠️ I don't like global variables. This issue is not advocating to use them, but rather this issue is primarily concerned with healthy testing practices when developing React applications.
How one might go about getting the following test to work?
Beta Was this translation helpful? Give feedback.
All reactions