我正在使用两个 js 文件对 Mocha 进行测试。
我的 test.js 文件如下所示:
const assert = require('assert');
const operations = require('./operations.js');
it('Calculates correct deserialization', () => {
var leftLeft = new operations.Node('left.left', null, null);
var left = new operations.Node('left', leftLeft, null);
var right = new operations.Node('right', null, null);
var root = new operations.Node('root', left, right);
assert.equal(operations.deserialize(serialize(root)).left.left.val == 'left.left');
});
我的operations.js 文件如下所示:
function Node(val, left, right) {
this.val = val;
this.left = left;
this.right = right;
}
虽然不完整,但测试未能实例化 Node 对象,并且 退出并显示消息
"TypeError: operations.Node is not a constructor"
我已经尝试过
var leftLeft = new Node('left.left', null, null);
也就是说,没有 de operations.Node()
。
我正在使用严格模式。
最佳答案
如果您要将文件作为模块require()
,那么您必须导出函数:
function Node(val, left, right) {
this.val = val;
this.left = left;
this.right = right;
}
module.exports = Node;
关于Javascript Mocha 测试 TypeError : x is not a constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50284727/