[英]Javascript Mocha test TypeError: x is not a constructor
I'm using two js files to make a test with Mocha. 我正在使用两个js文件对Mocha进行测试。
My test.js file looks like this: 我的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');
});
and my operations.js file looks like this: 我的operations.js文件如下所示:
function Node(val, left, right) {
this.val = val;
this.left = left;
this.right = right;
}
although incomplete, the test fails to instantiate the Node objects, and exits with the message 尽管不完整,但是测试无法实例化Node对象,并退出并显示以下消息
"TypeError: operations.Node is not a constructor" “ TypeError:operations.Node不是构造函数”
I've already tried with 我已经尝试过
var leftLeft = new Node('left.left', null, null);
that is, without de operations.Node()
. 也就是说,没有de operations.Node()
。 I'm using strict mode. 我正在使用严格模式。
If you are going to require()
the file as a module, then you must export the function: 如果要将require()
文件作为模块,则必须导出函数:
function Node(val, left, right) {
this.val = val;
this.left = left;
this.right = right;
}
module.exports = Node;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.