Skip to content

Instantly share code, notes, and snippets.

@icirellik
Created August 25, 2015 20:02
Show Gist options
  • Save icirellik/b9968abcecbb9e88dfb2 to your computer and use it in GitHub Desktop.
Save icirellik/b9968abcecbb9e88dfb2 to your computer and use it in GitHub Desktop.
Pass data between beforeEach, afterEach and it in mocha tests.
var expect = require('expect');
describe.only('Sample', function () {
beforeEach(function () {
this.currentTest.value = 'Winning!';
});
it('Uses current test data', function () {
expect(this.test.value).to.equal('Winning!');
this.test.value = 'Win Later';
});
afterEach(function () {
expect(this.currentTest.value).to.equal('Win Later');
});
});
@chumager
Copy link

chumager commented Jul 9, 2020

Hi, the this object only works inside a function, not outside, that's why you should use function instead of arrow in the describe or it.

Hello,
if i want to display the value of the current test in the description of it
for exemple:
it('Uses current test data "' + this.test.value + '"', function () { expect(this.test.value).to.equal('Winning!'); this.test.value = 'Win Later'; });
i get always this.test.value = 'undefined'
Is there any solution to use a variable outside of it ?
Also the same if i define a global variable it's always undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment