Created
August 25, 2015 20:02
-
-
Save icirellik/b9968abcecbb9e88dfb2 to your computer and use it in GitHub Desktop.
Pass data between beforeEach, afterEach and it in mocha tests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}); | |
}); |
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 ofit
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 ofit
?
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
Thanks Buddy!
Following your shared example I did write some basic code