question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

URL behaves differently when run with jest

See original GitHub issue

🐛 Bug Report

The object thrown by the constructor of URL is not an instance of Error.

To Reproduce

Create a file error.test.js with the following contents:

#!/usr/bin/env node
try {
  new URL("not_a_url");
} catch (anything) {
  process.exit(anything instanceof Error ? 0 : 1);
}

If you run node error.test.js the program exits successfully. However, if you use jest to run this file you get:

§ jest
  ●  process.exit called with "1"

      3 |   new URL("not_a_url");
      4 | } catch (anything) {
    > 5 |   process.exit(anything instanceof Error ? 0 : 1);
        |           ^
      6 | }
      7 |

      at Object.exit (error.test.js:5:11)

 RUNS  ./error.test.js

Expected behavior

I expect the error raised by the call to URL to be an instance of Error.

envinfo

  System:
    OS: macOS Mojave 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
  Binaries:
    Node: 12.1.0 - /usr/local/bin/node
    Yarn: 1.22.0 - ~/.yarn/bin/yarn
    npm: 6.13.7 - /usr/local/bin/npm

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17

github_iconTop GitHub Comments

1reaction
carsonreinkecommented, Aug 11, 2020

@lukasoppermann Something like const URLSearchParams = require('url').URLSearchParams, if you look at require('url') is actually this:

{
  Url: [Function: Url],
  parse: [Function: urlParse],
  resolve: [Function: urlResolve],
  resolveObject: [Function: urlResolveObject],
  format: [Function: urlFormat],
  URL: [Function: URL],
  URLSearchParams: [Function: URLSearchParams],
  domainToASCII: [Function: domainToASCII],
  domainToUnicode: [Function: domainToUnicode],
  pathToFileURL: [Function: pathToFileURL],
  fileURLToPath: [Function: fileURLToPath]
}
1reaction
SimenBcommented, Aug 10, 2020

This is #2549, yeah

@fredefox it is really strange, I have not really gotten to the bottom of it, but I did try using npmjs.com/package/jest-runner-mocha and it does NOT exhibit that behavior.

Yeah, this behavior is specific to Jest since we use the vm module to create sandboxes - mocha runs all the tests in the same context.

Latest update to #2549 is essentially https://github.com/nodejs/node/issues/31852

Read more comments on GitHub >

github_iconTop Results From Across the Web

Different behavior from Jest test when run locally and run via ...
Depends on your project. Since JSDOM behaves differently in, a reasonable explanation would be that test runs are performed with different Jest ......
Read more >
Globals - Jest
Runs a function after all the tests in this file have completed. If the function returns a promise or is a generator, Jest...
Read more >
Introduction to Nuxt Test Utils - Reflect.run
Here we provide an overview of Nuxt Test Utils, its features, and how to set up a test environment with `nuxt/test-utils`.
Read more >
How To Write Snapshot Tests For React Components With Jest
Snapshot testing allows you to ensure your output continues to behave as expected. This is useful because as you revisit your code to...
Read more >
Testing React Applications with Jest - Auth0
Jest run tests in parallel processes thus minimizing test runtime. ... This however is not an indicator that the component behaves as it ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found