admin管理员组

文章数量:1357595

I would like to setup some variables first, before executing the test, and I found this solution, Running Mocha setup before each suite rather than before each test

But, I dont know how can I pass the variable into my callback, they way I did I will get undefined

makeSuite('hello', (context) => {
    it('should return', () => {
        assert.strictEqual(1, 1)
    })
})
makeSuite('world', (context) => {
    it('should return', () => {
        console.log(context) // undefined
        assert.strictEqual(1, 1)
    })
})

function makeSuite(name: string, cb: (context: any) => any) {
    let age: string;
    describe(name, () => {
        beforeEach(() => {
            age = '123'
        })

        cb(age);
    })
}

The reason why I want to pass the variable into the callback because, I will have many private variables that requires to setup at beforeEach hook, and I dont want to repeat my code for all the tests.

I would like to setup some variables first, before executing the test, and I found this solution, Running Mocha setup before each suite rather than before each test

But, I dont know how can I pass the variable into my callback, they way I did I will get undefined

makeSuite('hello', (context) => {
    it('should return', () => {
        assert.strictEqual(1, 1)
    })
})
makeSuite('world', (context) => {
    it('should return', () => {
        console.log(context) // undefined
        assert.strictEqual(1, 1)
    })
})

function makeSuite(name: string, cb: (context: any) => any) {
    let age: string;
    describe(name, () => {
        beforeEach(() => {
            age = '123'
        })

        cb(age);
    })
}

The reason why I want to pass the variable into the callback because, I will have many private variables that requires to setup at beforeEach hook, and I dont want to repeat my code for all the tests.

Share Improve this question edited May 23, 2017 at 12:00 CommunityBot 11 silver badge asked Feb 23, 2017 at 10:06 TimTim 3,8033 gold badges37 silver badges57 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

The callbacks passed to describe are called immediately but your beforeEach hook is called later when the test executes. So when cb(age) is called, age has for value undefined. age is later set to "123" but cb has already gotten its copy of the value earlier so it has no effect. In order for cb to see the change you'd have to pass a reference to an object that you then mutate. Something like this:

makeSuite('world', (context) => {
    it('should return', () => {
        console.log(context.age)
    })
})

function makeSuite(name, cb) {
    describe(name, () => {
        let context = {
            age: undefined,
        };
        beforeEach(() => {
            context.age = '123';
        });

        cb(context);
    });
}

(I've removed the TypeScript type annotations so that it runs as pure JavaScript. The annotations are not crucial to solving the problem anyway.)

There is another way, which is to inject the variable into mocha context

makeSuite('world', function() {
    it('should return', function() {
        // read the variable as shown below
        console.log((this as any).age); // 123
    })
})

function makeSuite(name: string, cb: () => any) {
    describe(name, function() {
        beforeEach(function() {
            // inject the desired variable into the mocha context
            (this as any).age = 123;
        })

        cb();
    })
}

But, I dont think is a good practise of injecting the variable into the mocha context, the answer provided by @Louis would be better solution

I started with what @Louis was doing but then found it was better (in my situation) to make the function around it. That way, I could toggle the after and before actions. I also needed the before, after, and it to be asynchronous.

function itWithSetup(name, callback, doBefore = true, doAfter = true) {

  let context = null;
  if (doBefore) {
    before(async () => {
     context = await getContext();
    });
  }
  it(name, async () => {
    await callback(context);
  });
  if (doAfter) {
    after(async () => {
      await clearContext();
    });
  }
}

describe("test with context", () => {

  itWithSetup("unit test", async (message) => {

    await testWithContext(context, otherConditions);

  }, true, true);
});

本文标签: javascriptSetup variable in mocha suite before each testStack Overflow