EGamalielGZE
Denoβ€’15mo agoβ€’
6 replies
EGamalielGZ

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:

error: ReferenceError: document is not defined

Here is the code of the test I'm doing and the deno.json at root and workspace

Code:
import { renderHook } from "@testing-library/preact";
import { useFetch } from "@/hooks/use-fetch.ts";
import { expect } from "@std/expect";

Deno.test("Use fetch", () => {
    const { result } = renderHook(()=> useFetch("https://jsonplaceholder.typicode.com/todos/1"))
    expect(result.current.data).not.toEqual(null);
});


deno.json (root)
{
  "workspace": [
    "./packages/pretch-core",
    "./packages/pretch-preact",
    "./packages/pretch-react"
  ],
  "compilerOptions": {
    "lib": [
      "dom",
      "dom.iterable",
      "dom.asynciterable",
      "deno.ns"
    ],
    "jsx": "react-jsx",
    "jsxImportSource": "preact"
  },
  "lint": {
    "rules": {
      "include": [
        "no-console",
        "ban-types",
        "camelcase",
        "verbatim-module-syntax"
      ]
    }
  },
  "exclude": [
    ".git",
    ".vscode"
  ],
  "imports": {
    "@std/expect": "jsr:@std/expect@^1.0.5",
    "@testing-library/dom": "npm:@testing-library/dom@^10.4.0"
  }
}


deno.json (workspace)
{
  "name": "@prefetch/preact",
  "version": "0.1.2",
  "tasks": {
    "doc": "deno doc --html --name='Pretch' src"
  },
  "imports": {
    "@preact/signals": "npm:@preact/signals@^1.3.0",
    "@std/assert": "jsr:@std/assert@1",
    "preact": "npm:preact@^10.24.1",
    "@/": "./",
    "@std/expect": "jsr:@std/expect@^1.0.5",
    "@testing-library/preact": "npm:@testing-library/preact@^3.2.4"
  },
  "exports": {
    ".": "./mod.ts"
  }
}
Was this page helpful?