在异步摩卡测试中配置 JSDom

Configuring JSDom in an async mocha test

本文关键字:配置 JSDom 测试 异步 摩卡      更新时间:2023-09-26

我正在尝试将我的依赖项加载到 JSDom 环境中,然后执行基本测试。

当我使用 mocha 运行此文件时,它告诉我已超过 2000 毫秒的最大超时。

// 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();
    }
  });
});

有什么想法吗?

我认为它与 jsdom 有关,因为如果我将回调更改为如下所示:

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

它从不运行console.log

您正在导入jsdom.jsdom但使用jsdom.env语法(它确实需要回调作为第三个参数)。更改此行:

import { jsdom } from 'jsdom';

自:

import { env } from 'jsdom';