Skip to content
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 test --require or --import (setup file) #26988

Open
birkskyum opened this issue Nov 21, 2024 · 3 comments
Open

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

birkskyum opened this issue Nov 21, 2024 · 3 comments
Labels
suggestion suggestions for new features (yet to be agreed) testing related to deno test and coverage

Comments

@birkskyum
Copy link
Contributor

birkskyum commented Nov 21, 2024

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

@birkskyum birkskyum changed the title deno test --require or --import deno test --require or --import (setup file) Nov 21, 2024
@birkskyum birkskyum changed the title deno test --require or --import (setup file) [node compat] deno test --require or --import (setup file) Nov 21, 2024
@birkskyum birkskyum changed the title [node compat] deno test --require or --import (setup file) deno test --require or --import (setup file) Nov 21, 2024
@kt3k
Copy link
Member

kt3k commented Nov 26, 2024

How do you load mock-browser.ts in your deno script?

What happens if you put the import statement of import "./mock-browser.ts at the beginning of the test script?


I test my frontend tool in Deno by polyfilling dom environment like this way https://github.com/kt3k/cell/blob/8ef924809b29e6f3c43d8b5540136ff288f00561/test.ts#L5

By placing polyfill-import before the actual library import, the library can have access to global 'document' object.

@bartlomieju bartlomieju added suggestion suggestions for new features (yet to be agreed) testing related to deno test and coverage labels Nov 26, 2024
@birkskyum
Copy link
Contributor Author

birkskyum commented Nov 26, 2024

@kt3k , it does fix the test to add it at the top, but i have a ton of test files, and have to import it at the top of all of them

@kt3k
Copy link
Member

kt3k commented Nov 27, 2024

The ability to specify common setup script makes sense to me. That's something often provided by testing frameworks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
suggestion suggestions for new features (yet to be agreed) testing related to deno test and coverage
Projects
None yet
Development

No branches or pull requests

3 participants