无法在nodejs中导出变量

Not able to export variable in nodejs

使用 mocha 尝试从 testHook 文件导出变量但在测试文件中未定义,我的代码是 ::

测试文件:

 var xyz = require("testHook").xyz;

 class test1 {
 execute() {
  describe("test suite 1", async () => {
    it("test 1", async () => {
      console.log(xyz);
    });
    });
  }
}
new test1().execute();

testHook.js

 function abc()
 {
 //do some stuff and assume value to be returned is 10 
 exports.xyz = 10;;
 }
 beforeEach(() => {
  abc();
  console.log(this.xyz);
 });

输出::

 test suite 1
 10
 undefined
 ✓ test 1: 1ms
 Suite duration: 0.009 s, Tests: 1
 1 passing (10ms)

你应该导入 TestHook;

var testHook = require("testHook");

class test1 {
 execute() {
   describe("test suite 1", async () => {
     it("test 1", async () => {
       console.log(testHook.xyz);
     });
   });
 }
}
new test1().execute();

导入xyz时,测试还没有开始,所以还没有调用beforeEach。所以,导入的xyz是原始变量设置前的副本。