【问题标题】:Chai assert.deepEqual throws error "TypeError: Right-hand side of 'instanceof' is not an object" when using atobChai assert.deepEqual 使用 atob 时抛出错误“TypeError:'instanceof' 的右侧不是对象”
【发布时间】:2017-12-07 09:47:00
【问题描述】:

我正在使用 Chai 和 Mocha 对我的辅助函数运行测试。我使用 JSDOM 来包含 atob 和 btoa。这是我的 setup.js 文件:

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

const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;

global.window = window;
global.document = window.document
global.btoa = window.btoa;
global.atob = window.atob;

当我尝试运行我的测试时,我得到了这个错误:

TypeError: Right-hand side of 'instanceof' is not an object.

我的测试功能:

describe('helpers', () => {
  const testObject = { id: 1 };
  const encodedObject = base64EncodeObject(testObject);
  const decodedObject = base64DecodeObject(encodedObject);
  
  describe('base64DecodeObject()', () => {
    it('decoded object should match original object', () => {
      assert.deepEqual(decodedObject, testObject);
    });
  });
});

目标函数:

const base64DecodeObject = (base64String) => {
  let object = atob(base64String);
  object = JSON.parse(object);
  return object;
}

【问题讨论】:

    标签: javascript mocha.js chai jsdom


    【解决方案1】:

    您的问题是由于您只是在模仿 Node.js 中的 DOM 环境的一部分。你设置了一些变量并停在那里,所以你最终得到的东西既不是普通的 Node 环境,也不是 DOM 环境。

    chai 使用deep-eql 进行深度比较,deep-eql 使用名为type-detect 的包来完成一些工作。 type-detect 执行一个测试,表明它正在一个 DOM 环境中运行,并最终尝试执行 this

    if (obj instanceof globalObject.HTMLElement && obj.tagName === 'BLOCKQUOTE') {
    

    由于您没有将HTMLElementwindow 复制到global,因此它会失败并出现错误。您可以通过添加来修复它:

    global.HTMLElement = window.HTMLElement;
    

    【讨论】:

      猜你喜欢
      • 2023-02-14
      • 2023-01-31
      • 1970-01-01
      • 2019-09-17
      • 2018-11-11
      • 1970-01-01
      • 2017-08-28
      • 2018-01-11
      • 2019-11-16
      相关资源
      最近更新 更多