-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deno support #669
Comments
Booo! 😆 lol, but in all seriousness, I'd be curious to know what it takes to support Deno, but I don't think we need an issue open about this as it's not a priority until someone could reasonably make JSDOM (or similar) work in Deno, so I'm going to close this. |
Hey @kentcdodds , I've started messing about with deno & react, using aleph.js and wanted to look at using react-testing-library or a deno equivalent, and stumbled across this. We can use JSDOM in deno, example here https://github.com/YOU54F/deno-examples/blob/main/jsdom/jsdom.test.ts Only started using deno yesterday, out of curiosity to build an express like provider, and react based consumer equiv and exploring the available testing tools. |
You could also check https://github.com/b-fuze/deno-dom in a similar style, a more deno native lib. |
Hello. Libraries like JSDOM are working in Deno. I am trying to get testing-library to work and keep getting the error:
I have tried globalThis.document = document;
globalThis.window.document = document; I keep getting that error. I'm not sure how to patch it correctly, since Deno provides a When using import 'npm:global-jsdom/register' But that throws the error:
The DOM libraries are working by themselves. They just can't be put on the global object easily. Maybe there can be a way to configure/pass the document object to testing-library? That would make it easy to swap the DOM library. |
@kentcdodds Can we open this ticket again? Being able to do proper component tests will make Deno a much more reliable platform for doing React apps. Would be great if the https://github.com/denoland/fresh starter would include some component tests as an example and used |
If someone wants to work on that it's great by me! I don't have the bandwidth to work on it personally. As far as I'm aware, nothing has changed since my last comment on this so I don't see a reason to reopen 🤷♂️ |
I was eventually able to get this working but it's very messy, summarized here https://github.com/john-griffin/deno-dom-test |
I just revisited this and found that Deno does support using react testing library now. I believe improvements to the npm compatibility over the past year is what got it working. I'm not sure the minimum version of Deno that is required, I just know it works in the current version (1.45.3). To get react working, you need these compiler options and imports in your deno configuration file. {
"compilerOptions": {
"lib": ["esnext", "dom", "dom.iterable", "dom.asynciterable", "deno.ns"],
"jsx": "react-jsx",
"jsxImportSource": "react",
"jsxImportSourceTypes": "@types/react"
},
"imports": {
"react": "npm:react@18",
"@types/react": "npm:@types/react@18",
"react-dom": "npm:react-dom@18",
"@testing-library/react": "npm:@testing-library/react@16",
"global-jsdom": "npm:global-jsdom@24",
"@std/assert": "jsr:@std/assert@1",
"@std/testing": "jsr:@std/testing@0"
}
} Here is a simple component I wrote to test. export function Loading() {
return <div>Loading...</div>;
} Then here is an example of a test file. It uses Deno's test runner. import { assertEquals } from "@std/assert";
import { describe, it } from "@std/testing/bdd";
import "./global-jsdom.ts";
import { cleanup, render, screen } from "@testing-library/react";
import { Loading } from "./loading.tsx";
const loadingTests = describe({
name: "Loading",
afterEach() {
cleanup();
},
});
it(loadingTests, "renders loading message", () => {
render(<Loading />);
assertEquals(screen.getByText("Loading...").textContent, "Loading...");
}); I got it working based on your setup documentation for using without jest. I wasn't able to use global-jsdom/register though. When I tried importing it, I get the following error at runtime from Deno.
To workaround this, I looked at what register does and found all it does is import global-jsdom and call the default exported function. To ensure it runs before I import @testing-library/react, I put the equivalent of global-jsdom/register in a separate file and import that first. Below is the imported "./global-jsdom.ts" file from my test example. import jsdom from "global-jsdom";
jsdom(); Below is an example of the output from running deno test for this file. The One thing that I think would be a nice addition to react testing library would be if the render function was made to be disposable. That would make it so that the test would automatically cleanup after the context where render is called is left. I believe that could be done by simply updating the return for render to include a import { assertEquals } from "@std/assert";
import { describe, it } from "@std/testing/bdd";
import "./global-jsdom.ts";
import { cleanup, render, type RenderOptions } from "@testing-library/react";
import { Loading } from "./loading.tsx";
function customRender(
ui: React.ReactElement,
options?: Omit<RenderOptions, "queries">,
): ReturnType<typeof render> & Disposable {
const result = render(ui, options);
return {
...result,
[Symbol.dispose]() {
cleanup();
},
};
}
const loadingTests = describe("Loading");
it(loadingTests, "renders loading message", () => {
using screen = customRender(<Loading />);
assertEquals(screen.getByText("Loading...").textContent, "Loading...");
}); Here is the TypeScript documentation on using declarations and explicit resource management. |
Sorry, https://twitter.com/kentcdodds/status/1260716918526242816
:)
The text was updated successfully, but these errors were encountered: