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 doesn't use the specified compilerOptions.lib #14565

Closed
RobertAKARobin opened this issue May 11, 2022 · 4 comments
Closed

deno test doesn't use the specified compilerOptions.lib #14565

RobertAKARobin opened this issue May 11, 2022 · 4 comments
Labels
question a question about the use of Deno

Comments

@RobertAKARobin
Copy link

RobertAKARobin commented May 11, 2022

Tiny example repo:

https://github.com/RobertAKARobin/deno-test-err

It consists of:

// index.ts
export const div = document.createElement('div');
// index.test.ts
import { div } from './index.ts';
console.log(div);
// deno.jsonc
{
  "compilerOptions": {
    "lib": [
      "deno.ns",
      "dom"
    ]
  }
}

Steps to reproduce

  1. Run deno bundle index.ts. Observe it compiles and completes successfully.
  2. Run deno test index.test.ts

Expected result

deno test completes successfully, just like deno bundle.

Actual result

deno test fails with a fatal error:

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (13ms)

error: Uncaught ReferenceError: document is not defined
export const div = document.createElement('div');
                   ^
    at file:///Users/robertthomas/Programming/web/deno-test-err/index.ts:1:20

deno test doesn't "know" about document, which indicates it is not respecting that the configuration file's compilerOptions.lib includes "dom".

If I add the --compat option...:

$ deno test --allow-all --config deno.jsonc --compat  --unstable --no-check index.test.ts 

...then the error changes:

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (214ms)

error: Uncaught (in promise) SyntaxError: Cannot use import statement outside a module
  const [f, err] = core.evalContext(wrapper, filename);
                        ^
    at wrapSafe (https://deno.land/std@0.138.0/node/module.ts:1315:25)
    at Module._compile (https://deno.land/std@0.138.0/node/module.ts:251:29)
    at Object.Module._extensions..js (https://deno.land/std@0.138.0/node/module.ts:1334:10)
    at Module.load (https://deno.land/std@0.138.0/node/module.ts:239:34)
    at Function._load (https://deno.land/std@0.138.0/node/module.ts:558:14)
    at loadCjsModule ([deno:cli/compat/mod.rs:120:30]:3:22)

How can I write unit tests that include the DOM API?

@bartlomieju
Copy link
Member

bartlomieju commented May 11, 2022

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out (13ms)

error: Uncaught ReferenceError: document is not defined
export const div = document.createElement('div');
                   ^
    at file:///Users/robertthomas/Programming/web/deno-test-err/index.ts:1:20

This is an actual runtime error and not TypeScript error. If you configure compilerOptions and pass "dom" it tells built-in TypeScript compiler that these types are available but it doesn't actually make document available in Deno, you would have to polyfill it yourself.

@dsherret dsherret added the question a question about the use of Deno label May 11, 2022
@RobertAKARobin
Copy link
Author

Thanks @dsherret . Is there a workaround? It would be nice if I could unit test front-end code without needing to import a vendor library.

@bartlomieju
Copy link
Member

@RobertAKARobin see https://deno.land/manual/jsx_dom/linkedom#using-linkedom-with-deno

@bartlomieju
Copy link
Member

@RobertAKARobin FYI I opened denoland/docs#103 to improve our documentation on this topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question a question about the use of Deno
Projects
None yet
Development

No branches or pull requests

3 participants