denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
98.01k stars 5.39k forks source link

deno test `--require` or `--import` (setup file) #26988

Open birkskyum opened 18 hours ago

birkskyum commented 18 hours ago

I can't really figure how to do some browser mocking in deno.

I'm using the node command like this, which works:

node --experimental-strip-types --test --import ./test/mock-browser.ts (link to code)

But with deno i get:

deno test --no-check:

error: ReferenceError: document is not defined
      container: document.createElement("div"),

I have in my deno.json

    "lib": [
      "deno.window",
      "dom"
    ]

The mock-browser.ts being:

import MockBrowser from "mock-browser";
const mock = new MockBrowser.mocks.MockBrowser();

global.document = mock.getDocument();
global.window = {};

// Polyfill based on https://gist.github.com/paulirish/1579671
let lastTime = 0;
global.requestAnimationFrame = function (fn) {
  const now = Date.now();
  const nextTime = Math.max(lastTime + 16, now);
  setTimeout(() => fn(lastTime = nextTime), nextTime - now);
};

For node compat, I believe I need a way to do a similar --import in deno test, or another way to give a setup file, which I believe vitest calls it