-
-
Save icirellik/b9968abcecbb9e88dfb2 to your computer and use it in GitHub Desktop.
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'); | |
}); | |
}); |
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
Thank you !
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.
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();
});
});
});
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.
Thanks! So easy, but yet so hard to find.