javascript - 如何使用 Jest 测试节点列表

标签 javascript unit-testing jestjs jsdom

我尝试使用 Jest 测试一个 JavaScript 函数来检查变量是否是节点列表。 这是一个浏览器功能,将在浏览器中使用。 这是函数:

/**
 * @description Check if input is a nodelist and return true or false.
 * @export
 * @param {*} input
 * @returns {boolean}
 */
export function $isNodeList(input) {
    return NodeList.prototype.isPrototypeOf(input);
}

该函数在浏览器中运行良好,但如果我尝试用 jest 测试它,则会收到以下错误:ReferenceError:NodeList 未定义

测试文件是:

const esmImport = require("esm")(module);
const mod = esmImport("../tools/is_nodelist.js");

const html = '<!DOCTYPE html>\
                <html>\
                    <body>\
                        <div>Hello world</div>\
                        <div>Hello</div>\
                        <div>...</div>\
                    </body>\
                </html>';

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { document } = (new JSDOM(html)).window;

global.document = document;

test("expect nodeList to be nodelist", () => {
    let nodeList = global.document.querySelectorAll("div");
    expect(mod.$isNodeList(nodeList)).toBe(true);
});

test("expect htmlCollection not to be nodelist", () => {
    let htmlCollection = global.document.getElementsByTagName("div");
    expect(mod.$isNodeList(htmlCollection)).toBe(false);
});

我的测试出了什么问题以及如何正确测试该功能?

最佳答案

您可以设置testenvironment jest.config.js 文件的配置。这样您就不需要在测试文件中显式使用 jsdom

例如

index.js:

export function $isNodeList(input) {
  return NodeList.prototype.isPrototypeOf(input);
}

index.test.js:

import * as mod from '.';

describe('59947808', () => {
  test('expect nodeList to be nodelist', () => {
    let nodeList = document.querySelectorAll('div');
    console.log('nodeList: ', nodeList);
    expect(mod.$isNodeList(nodeList)).toBe(true);
  });

  test('expect htmlCollection not to be nodelist', () => {
    let htmlCollection = document.getElementsByTagName('div');
    console.log('htmlCollection: ', htmlCollection);
    expect(mod.$isNodeList(htmlCollection)).toBe(false);
  });
});

单元测试结果:

 PASS  src/stackoverflow/59947808/index.test.js (8.528s)
  59947808
    ✓ expect nodeList to be nodelist (11ms)
    ✓ expect htmlCollection not to be nodelist (1ms)

  console.log src/stackoverflow/59947808/index.test.js:6
    nodeList:  NodeList {}

  console.log src/stackoverflow/59947808/index.test.js:12
    htmlCollection:  HTMLCollection {}

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        9.773s

jest.config.js:

module.exports = {
  preset: 'ts-jest/presets/js-with-ts',
  testEnvironment: 'jsdom',
};

源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59947808

关于javascript - 如何使用 Jest 测试节点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59947808/

相关文章:

reactjs - 如何模拟 react 自定义钩子(Hook)返回值?

javascript - require() 和 new require() 有什么区别?

php - 为什么 PHP 不使用像 PHPUnit 这样的单元测试框架?

unit-testing - 带有 Sonar 的 Jenkins 上的 spring-test-mvc 问题

mongodb - Jest : Expect() only unique elements in an array

reactjs - 使用 Jest 和 React Native 测试库测试 DateTimePickerModal 的问题

javascript - Jasmine javaScript 异常

javascript - 如何将 Angular 4(点击)事件绑定(bind)到动态创建的 div

Javascript - iPhone 中当前 iframe 的大小错误

unit-testing - 使用 MockMvc post 方法进行 Spring Controller 测试