Gulp加载本地保存的qunit

时间:2018-11-26 21:05:41

标签: gulp qunit

我具有以下树结构:

- tests
|-- simple_basic.html

然后我尝试像这样测试它:

const qunit = require('node-qunit-phantomjs');
const fs = require('fs');

gulp.task('test', (done) => {
    fs.readdir('./tests', (err, files) => {
        files.forEach(file => {
          qunit('./tests/'+file);
        });
        done();
    });
});

但是出现以下错误:

[23:00:41] Using gulpfile ~/Kwdikas/Javascript/no_url_page/gulpfile.js
[23:00:41] Starting 'test'...
Testing file:////home/pcmagas/Kwdikas/Javascript/no_url_page/tests/simple_basic.html
[23:00:41] Finished 'test' after 16 ms
ReferenceError: Can't find variable: QUnit

  file:////home/pcmagas/Kwdikas/Javascript/no_url_page/tests/simple_basic.html:14 in global code
ReferenceError: Can't find variable: QUnit

  undefined:6
The `QUnit` object is not present on this page.

  phantomjs://code/runner-json.js:78

但是如何使用本地保存的QUnit进行测试?

1 个答案:

答案 0 :(得分:0)

只需包含以下文件:

  • ../node_modules/qunit/qunit/qunit.css
  • ../node_modules/qunit/qunit/qunit.js

tests目录中的任何文件。这样simple_basic.html就会变成(放置虚拟测试):

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>QUnit Example</title>
  <link rel="stylesheet" href="../node_modules/qunit/qunit/qunit.css">
</head>
<body>
  <div id="qunit"></div>
  <div id="qunit-fixture"></div>
  <script src="../node_modules/qunit/qunit/qunit.js"></script>
  <script>
      // Replace the test with the one you really need
      QUnit.test( "a basic test example", function( assert ) {
        var value = "hello";
        assert.equal( value, "hello", "We expect value to be hello" );
      });
  </script>
</body>
</html>