简体   繁体   中英

Configuring JSDom in an async mocha test

I'm trying to load my dependencies into a JSDom environment then execute a basic test.

When I run this file with mocha, it tells me that the maximum timeout of 2000 milliseconds has been exceeded.

// Node Dependencies
import { readFileSync } from 'fs';

// NPM Dependencies
import { expect } from 'expect';
import { jsdom } from 'jsdom';

// JSDom Configuration
const html    = '<!doctype html><html><body></body></html>';
const dep1    = readFileSync("./dep1.js", "utf-8");
const dep2    = readFileSync("./dep2.js", "utf-8");
const scripts = [ dep1, dep2 ];

describe('App Actions', function(){
  it('sample test', function(done){

    // Use JSDom to mock a browser environment,
    // loading the necessary scripts, then executing the callback.
    jsdom(html, scripts, callback);

    function callback(err, window){
      expect(false).toEqual(true);
      done();
    }

  });
});

Any ideas?

I think it has something do with jsdom, since if I change the callback to look like this:

function callback(err, window){
  console.log(window);
  expect(false).toEqual(true);
  done();
}

It never runs the console.log .

You are importing jsdom.jsdom but using the syntax for jsdom.env (which does expect a callback as a third parameter). Change this line:

import { jsdom } from 'jsdom';

To:

import { env } from 'jsdom';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM