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');
});
});
@ninnepinne
Copy link

Thanks! So easy, but yet so hard to find.

@anabellaspinelli
Copy link

anabellaspinelli commented Nov 6, 2017

Awesome!!! Do you know if you can do something like this with a before() hook?

Update: nevermind, in the before() hook you can just drop data at this.something level and then retrieve it from any test :D

@egebilican
Copy link

Thank you !

@hadrich-hatem
Copy link

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.

@Haisum92
Copy link

Haisum92 commented Oct 31, 2019

Thanks Buddy!

Following your shared example I did write some basic code

describe('User Authentication', () => {

    before(async () => {
        let oObj = await admin.newAdmin();
        this.username = oObj.login; 
        this.password = oObj.password;
    });

    it('If the credentials exists in the system it should return the token generated against it.', (done) => {
        chai.request(server)
        .post("/application/login")
        .set("Content-Type", "application/x-www-form-urlencoded")
        .send({username: this.username,password:this.password})
        .end((err, res) => {
            this.token = res.body;
            res.should.have.status(200);
            res.body.should.be.a("string");
            done();
        });
    });


    it('Get All Books.', (done) => {
        chai.request(server)
        .get("/books/")
        .set("Content-Type", "application/json")
        .set("access_token", this.token)
        .end((err, res) => {
            console.log(res.body);
            res.should.have.status(200);
            res.should.be.an("object");
            res.should.have.property('body').should.be.an('object')
            // res.body.should.be.an('object')
            res.body.should.have.property('results')
            res.body.results.should.be.an('array').to.not.equal(0)
            // res.body.results.should.to.not.equal(0)
            res.body.should.have.property('total').not.equal(0)
            // res.body.total.should.not.equal(0)
            
            done();
        });
    });

});

@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