aleph.js icon indicating copy to clipboard operation
aleph.js copied to clipboard

Testing environment

Open douglasduteil opened this issue 3 years ago • 8 comments

Hi

I would like to know if you have any suggestion on how to add a friendly testing environment. I found it difficult to take advance of the existing testing libraries for React in the Deno ecosystem.

I was thinking about sharing some code to test the examples/hello-world sources.


My latest attempt highlighted that :

douglasduteil avatar May 31 '21 20:05 douglasduteil

@douglasduteil I can't completely answer your questions, but I want to share some of my findings.

  • jspm version of jsdom seems working with deno
import jsdom from "https://dev.jspm.io/jsdom";
const { JSDOM } = jsdom;
const doc = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
  • I couldn't import @testing-library/react from esm.sh, skypack, jspm.io, but umd version in the published package seems working. I was able to run the test like the below:

test.jsx

import "https://unpkg.com/[email protected]/umd/react.development.js";
import "https://unpkg.com/[email protected]/umd/react-dom.development.js";
import "https://unpkg.com/@testing-library/[email protected]/dist/@testing-library/react.umd.js";
import jsdom from "https://dev.jspm.io/jsdom";
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
const { render } = TestingLibraryReact; // <- Umd testing-library exports this in global

Deno.test("test", async () => {
  const { JSDOM } = jsdom;
  const doc = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
  globalThis.document = doc.window.document;
  globalThis.HTMLIFrameElement = doc.window.HTMLIFrameElement; // Needed for testing-library
  const { getByText } = render(<div>hello</div>);
  assert(getByText("hello") instanceof doc.window.HTMLDivElement);

  // Needs to wait a little bit to prevent the leaking of the timer.
  await new Promise(r => setTimeout(r, 10));
});
deno test test.jsx

I'm not sure if it's possible to make this work with esm.sh version of react/react-dom at the moment

kt3k avatar Jun 01 '21 03:06 kt3k

That helps a lot @kt3k Thanks

I'm not sure how one should handle this but having a import map with your recommendations above setup inside alephjs test will certainly help going forward with tested projects today :)

douglasduteil avatar Jun 01 '21 14:06 douglasduteil

In the meantime, I'm committing in the https://github.com/denoland/deno_std to add node builtins missing pieces 👍

douglasduteil avatar Jun 01 '21 14:06 douglasduteil

I'm not sure if it's possible to make this work with esm.sh version of react/react-dom at the moment

It probably won't be able to work.

It's probably going to take a while in general to add testing features, because there is no really more standard testing library for browsers with Deno.

shadowtime2000 avatar Jun 01 '21 19:06 shadowtime2000

i decide to create new module to support react testing in Deno with esm.sh/react!

ije avatar Aug 30 '21 12:08 ije

it looks like:

import React from "https://esm.sh/react"
import { render } from "https://deno.land/x/obscura/react.ts"
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

function App(props) {
  return <h1>{props.title}</h1>
}

Deno.test('"Hi :)" should be rendered', async () => {
  const page = await render(<App title="Hi :)" />)
  assertEquals(page.select('App').props.title, "Hi :)")
  assertEquals(page.select('App > h1').text, "Hi :)")
  page.close()
})

use https://deno.land/x/[email protected] as the dom observer

ije avatar Aug 30 '21 13:08 ije

Can't wait for this https://github.com/alephjs/obscura @ije ;)

douglasduteil avatar Aug 30 '21 16:08 douglasduteil

just tried, and it seem works (WIP)

Screenshot 2021-08-31 at 02 49 44 Screenshot 2021-08-31 at 02 50 03

ije avatar Aug 30 '21 18:08 ije