document is not defined error while testing preact hooks
Currently I'm developing a preact hooks library. When I try to run a simple test using
deno test
, it throws me the next error:
Here is the code of the test I'm doing and the deno.json
at root and workspace
Code:
deno.json (root)
deno.json (workspace)
6 Replies
@marvinh. can you please take a look?
The way the
@testing-library/preact
works is that it expects to be run inside an environment where a subset of the DOM APIs are available. The idea behind it is that you're asserting against the rendered result.
By default, server runtimes don't ship with the DOM API as it's something mostly for users. This means you need to include a library like happy-dom
, deno_dom
, linkedom
or JSDom
to polyfill that.
Adding any of these libraries and setting up the document
global according to their documentation will resolve the issue.GitHub
fix: Add hint for missing
document
global in terminal error by ba...This came up on Discord as a question so I thought it's worth adding a hint for this
as it might be a common pitfall.
Ok, that make sense. In that case, please can you provide an example, or proceed with the PR that @bartlomieju did to help others that know this (in the error message or the documentation) at least as a suggestion?
Sure, copy & pasting the example on how to use
JSDOM
straight out of the readme gives you this:
Thanks for helping 😄