当需要来自同一文件时,导出同一目录下的两个模块不起作用

Exporting two module under same directory not working when requiring from same file

我在同一目录下有两个js文件: main/file1.jsmain/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');
    });
})

当您应该需要文件名时,您需要函数名。你的要求应该是这样的:

const steps = require('../main/file1');
const steps2 = require('../main/file2');