【发布时间】:2018-10-24 05:32:57
【问题描述】:
我在同一个目录下有两个js文件:
main/file1.js 和 main/file2.js。然后我在我的测试文件夹下调用它test/files.js
如果我的 file1.js 中有这个:
function file1(){
let result = "a";
return result;
}
module.exports = file1
然后是我的 file2.js:
let v = "c" //this is the error that make file2 undefined. scope issue.
function file2(){
let result = "b";
return v;
}
module.exports = file2
然后在我的测试文件中我需要这两个文件。 file1 的 steps 函数工作正常,但 file2 的 steps2 未定义。有什么想法吗?
const assert = require('assert');
const steps = require('../main/steps');
const steps2 = require('../main/steps2');
describe('steps', function(){
it('make steps', function(){
assert.equal(file1(), 'a');
});
it('make steps2', function(){
assert.equal(file2(), 'b');
});
})
【问题讨论】:
-
如果你颠倒订单会发生什么?
-
您的文件名为
file1.js和file2.js,但您需要steps.js和steps2.js。这只是示例中的拼写错误,还是实际代码? -
@Cully 不是错字,我需要
const steps2 = require('../main/steps2');,因为step2正在被导出到file2 中。 -
@Bravo,颠倒测试文件中的要求顺序?
-
@MingHuang 你不需要函数名,你需要文件名。应该是
const steps = require('../main/file1'); const steps2 = require('../main/file2');
标签: javascript node.js mocha.js