Using JSDOM in jest tests

814 views Asked by At

I wanted to use JSDOM in my jest test suit. However, while JSDOM standalone works perfect, when i run it within jest test, it throws strange errors about encoding. Look at that snippet

const { JSDOM } = require("jsdom");

describe("suit", () => {
  test("test", () => {
    JSDOM.fromURL("your_url", {}).then(dom => {
      expect(1).toBe(1);
    });
  });
});

Snippet above depending on the your_url throws Error: Encoding not recognized: 'ISO-8859-2' (searched as: 'iso88592') ( for example https://gazeta.pl ) or Error: Encoding not recognized: 'UTF-8' (searched as: 'utf8') ( for example https://github.com). There is no url i was able to make it working, always one out of two errors described above. However when you run :

const { JSDOM } = require("jsdom");
JSDOM.fromURL("https://example.com/", {}).then(dom => {
  console.log(dom.serialize());
});

it works as it should. Reproduction repo : https://github.com/pociej/jsdom_problem_reproduction.

environment info :

  System:
    OS: macOS Mojave 10.14.3
    CPU: (4) x64 Intel(R) Core(TM) i7-4578U CPU @ 3.00GHz
  Binaries:
    Node: 12.14.0 - ~/.nvm/versions/node/v12.14.0/bin/node
    Yarn: 1.21.1 - /usr/local/bin/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v12.14.0/bin/npm
  npmPackages:
    jest: ^24.9.0 => 24.9.0

full error listing :

 npm test

> [email protected] test /Users/grzegorz/Projects/Meteor/playground/test_jsdom
> jest

 PASS  __tests__/test.js
  suit
    ✓ test (35ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.93s
Ran all test suites.
(node:13599) UnhandledPromiseRejectionWarning: Error: Encoding not recognized: 'ISO-8859-2' (searched as: 'iso88592')
(node:13599) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:13599) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. 
0

There are 0 answers