javascript - node-qunit 不在范围内包含我的源文件

标签 javascript node.js qunit

我已经从 npm 安装了 node-qunit(稳定版),但似乎无法进行任何测试。我的源文件似乎没有包含在范围内。

./source/myscript.js:

var myObj = {
    a : true
}

./test/tests.js:

test("that a is true", function () {
     ok(myObj.a);
});

./test/runner.js:

var runner = require('qunit');
runner.run({
    code : './source/myscript.js',
    tests : './test/tests.js'
});

./生成文件:

test :
<tab>node ./test/testrunner.js

.PHONY: install test

如果我运行 make test,我会得到一个 'ReferenceError: myObj is not defined' 错误。源文件确实运行,因为它可能会抛出错误。它似乎没有像应该的那样包含在全局范围内。如果我按照 node-qunit readme 中的说明从命令行执行它,则它不起作用.有人知道如何让它工作吗?

最佳答案

您没有导出任何内容。在幕后,node-qunit 使用 require加载指定的模块。要在模块被 require 时公开变量,您必须将它们添加到 exports 中对象(或将您自己的对象分配给 exports 变量)

(对象字面量中也有语法错误——;)

这对我有用:

./source/myscript.js:

exports.myObj = {
  a: true
}

./test/tests.js:

QUnit.module('tests')

test("that a is true", function () {
  ok(myObj.a)
})

./test/runner.js:

var runner = require('qunit')

runner.run({
  code : './source/myscript.js'
, tests : './test/tests.js'
})

关于javascript - node-qunit 不在范围内包含我的源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8863068/

相关文章:

node.js - 为什么 MongoDB 无法连续两次插入相同的 JSON?

javascript - knockout.js 脱离浏览器测试并伪造出窗口

javascript - 如何在javascript中创建实体对象

javascript - 删除类时 getElementsByClassName() 数组的长度发生变化

javascript - 提交包含动态 ID 的 HTML 表单

javascript - node.js 请求和 restify 客户端有什么区别?

angularjs - 在 AngularJS 中使用 Passport oauth 和 JWT

javascript - 如何以编程方式将 html 注入(inject) qunit-fixture

javascript - QUnit 异步测试停止功能不起作用

javascript - jQuery 延迟无法正常工作