Mocha e um framework de testes flexivel para Node.js e navegadores, tornando os testes assincronos intuitivos e poderosos.
Interface BDD (BDD Interface)
const { expect } = require('chai');
describe('User Authentication', () => {
before(() => {
// Executa uma vez antes de todos os testes do bloco
});
after(() => {
// Executa uma vez apos todos os testes do bloco
});
beforeEach(() => {
// Executa antes de cada teste individual
});
afterEach(() => {
// Executa apos cada teste individual
});
it('should authenticate user with valid credentials', () => {
const user = authenticate('[email protected]', 'secret');
expect(user).to.be.an('object');
expect(user.token).to.be.a('string');
});
it.skip('temporarily skipped test', () => {
// Pula a execucao deste teste temporariamente
});
});
Testes assincronos (Asynchronous Tests)
Promises e Async/Await
it('should fetch user profile from API', async () => {
const profile = await fetchProfile(101);
expect(profile.name).to.equal('Alice');
});
Callbacks com done()
it('should save document via callback', (done) => {
document.save((err) => {
if (err) return done(err);
done();
});
});
Guia rapido de assercoes Chai
// Estilo Should
chai.should();
user.should.have.property('name').equal('Alice');
list.should.have.lengthOf(3);
// Estilo Expect
expect(user).to.be.an('object');
expect(list).to.include(42);