类型错误:QUnit beforeEach 在 setTitle 上失败:无法读取未定义的属性“getElementById”

问题描述

我正在使用 QUnit 并运行测试。

这是我的 add-tests.js

set output to (do shell script "bash -s <<'EOF'" & return & "paste \\<\\(commands processing file 1\\) \\<\\(commands processing file 2\\) | other processing commands" & return & "EOF")

我注释掉了 test.inbrowser,因为我找不到 localhost:8000。我需要解决这个问题,但现在这更重要。

这是我的包 JSON

    const add = require('../add.js');
    QUnit.test.inbrowser = require('qunit-in-browser');

    QUnit.module('add');

    QUnit.test('add two numbers',assert => {
        assert.equal(add(1,1),2);
    });


    QUnit.module("Set title",function (hooks) {

        var window = this.window;
        var document = this.document;

        hooks.beforeEach(function () {

            var fixture = this.document.getElementById('qunit-fixture');
            fixture.innerHTML = '<div id="pageTitle"></div>';
        });

        QUnit.test('Set title for John,Faiza and Stan',function (assert) {
            var fixture = document.getElementById('qunit-fixture');
            fixture.innerHTML = '<div id="pageTitle"></div>';
            setTitle(['John','Faiza','Stan']);
            var title = document.getElementById('pageTitle').innerText;
            assert.equal(title,'John,Faiza and Stan');
        });

        QUnit.test('Set title for John and Faiza',function (assert) {
            setTitle(['John','Faiza']);
            var title = document.getElementById('pageTitle').innerHTML;
            assert.equal(title,'John and Faiza');
        });

        QUnit.test('Set title for John',function (assert) {
            setTitle(['John']);
            var title = document.getElementById('pageTitle').innerHTML;
            assert.equal(title,'John');
        });

        // QUnit.test.inbrowser('Home page successfully renders','https://localhost:8000/',function(assert) {
        //   const content = document.getElementById('content');
        //   assert.ok(content,'main content is rendered!');
        // });


    });

现在,这不是一个 Angular 项目。它是 AEM,我拥有整个应用程序,而不仅仅是 AEM 中的片段。

我所做的只是测试应用中存在的 PURE JavaScript。

所以,当我在终端中运行 'qunit' 时,我得到了这个......

{
  "name": "acquisition-aem-global-components","version": "1.0.0","description": "This is for the Global Components Navigator","main": "index.js","scripts": {
    "lint": "eslint '**/*.js'","test": "qunit add.js --require ./windowRef.service.js ./testHelper.js ./tests.js","compodoc": "npx compodoc -p tsconfig.doc.json"
  },"repository": {
    "type": "git","url": "https://github.optum.com/gov-prog-digital/acquisition-aem-global-components.git"
  },"author": "UHG - Peter Borreggine","license": "MIT","dependencies": {
    "@angular/cli": "^12.0.0","@angular/common": "^12.0.0","@angular/core": "^12.0.0","cli-table": "^0.3.6","fs-extra": "^10.0.0","mocha": "^8.4.0","puppeteer": "^9.1.1"
  },"devDependencies": {
    "jquery": "^3.6.0","jsdom": "^16.5.3","qunit": "^2.15.0","qunit-in-browser": "^1.0.0"
  }
}

所以,如您所见,我已经定义了窗口和文档。

但是,如果我尝试使用变量 document.getElementById 或 this.document.getElementById,则会出现相同的结果

这是我的 test.html

PS C:\Users\pborregg\repos\wwwroot\acquisition-global-components\src> qunit
TAP version 13
ok 1 add > add two numbers
not ok 2 Set title > Set title for John,Faiza and Stan
---
message: "beforeEach Failed on Set title for John,Faiza and Stan: Cannot read property 'getElementById' of undefined"
severity: Failed
actual  : null
expected: undefined

这应该在我取消注释时呈现,但没有。

    <!DOCTYPE html>
    <Meta charset="utf-8">
    <title>Test Suite</title>
    <link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.15.0.css">

    <body>
        <div id="qunit"></div>
        <div id="qunit-fixture"></div>
        <script src="https://code.jquery.com/qunit/qunit-2.15.0.js"></script>
        <script src="createDocument.js"></script>
        <script src="windowRef.service.js"></script>
        <script src="index.js"></script>
        <script src="tests.js"></script>


        <div id="pageTitle"></div>
        <div id="content"></div>


    </body>

谢谢

更新:

我试过了....

https://embed.plnkr.co/plunk/OJpuhX

得到这个....

C:\Users\pborregg\repos\wwwroot\acquisition-global-components\src\node_modules\socket.io\lib\store.js:35 Store.prototype.proto = EventEmitter.prototype; ^

        // QUnit.test.inbrowser('Home page successfully renders','main content is rendered!');
        // });

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...