首页 > 解决方案 > 尝试运行 QUnit 测试但找不到明确定义的函数

问题描述

我已经在我正在运行的测试中调整了这个例子

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

在测试中,导入功能失败,但我找到了解决方法。

这是该示例中的代码块:

索引.html

    <!DOCTYPE html>
    <html>

    <head>
        <link data-require="qunit@*" data-semver="1.17.1" rel="stylesheet"
            href="http://code.jquery.com/qunit/qunit-1.17.1.css" />
        <script data-require="qunit@*" data-semver="1.17.1" src="http://code.jquery.com/qunit/qunit-1.17.1.js"></script>
    </head>


    <body>
        <div id="qunit"></div>
        <div id="qunit-fixture"></div>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
        <script src="hello.js"></script>
        <script src="hello.test.js"></script>
    </body>

    </html>

添加.js

export class showAlert {
    static msg() {
        alert('Hello World!');
        // retmsg = 'Hello World!'
        // return retmsg;
    }
}

你好.test.js

import ('hello.ts');

QUnit.test("hello test", function (assert) {
    assert.ok(hello.hello() === 'Hello World!', 'Passed!');
});

hello.ts(以前的 hello.js),如果它是 ts 由于 IMPORT 不能在模块之外工作,我无法导入

var hello: any;

import { showAlert as _alert } from './add';

   hello = function hello() {
      alert('MakeTest: ' + _alert);
      return 'Hello World!';
   };
   alert('Hello var: ' + hello);

我经常得到的错误是:

PS C:\Users\pborregg\repos\wwwroot\acquisition-global-components\src> npm test

> acquisition-aem-global-components@1.0.0 test C:\Users\pborregg\repos\wwwroot\acquisition-global-components\src
> karma start

START:
19 05 2021 13:31:35.547:WARN [filelist]: Pattern "C:/Users/pborregg/repos/wwwroot/acquisition-global-components/src/hello.js" does not match any file.
19 05 2021 13:31:35.600:INFO [karma-server]: Karma v6.3.2 server started at http://localhost:9876/
19 05 2021 13:31:35.643:INFO [launcher]: Launching browsers Chrome with concurrency unlimited
19 05 2021 13:31:35.704:INFO [launcher]: Starting browser Chrome
19 05 2021 13:31:40.045:INFO [Chrome 90.0.4430.212 (Windows 10)]: Connected on socket lmNGV4w5POgtxbWPAAAB with id 46008113
× hello test
Finished in 0.018 secs / 0.003 secs @ 13:31:40 GMT-0700 (Pacific Daylight Time)

SUMMARY:
√ 0 tests completed
× 1 test failed

FAILED TESTS:
× hello test
Chrome 90.0.4430.212 (Windows 10)
Died on test #1     at hello.test.js:12:7: hello is not defined
ReferenceError: hello is not defined
  at Object.<anonymous> (hello.test.js:13:15)
  at runTest (C:/Users/pborregg/node_modules/qunit/qunit/qunit.js:2251:37)
  at Test.run (C:/Users/pborregg/node_modules/qunit/qunit/qunit.js:2239:9)
  at C:/Users/pborregg/node_modules/qunit/qunit/qunit.js:2461:16
  at processTaskQueue (C:/Users/pborregg/node_modules/qunit/qunit/qunit.js:1849:33)
  at C:/Users/pborregg/node_modules/qunit/qunit/qunit.js:1853:13
  npm ERR! Test failed.  See above for more details.

笔记:

要自己尝试一下,请将所有内容放在同一个文件夹中。

感谢您的帮助。

标签: javascripttypescriptkarma-jasminequnit

解决方案


推荐阅读