Mocha este un cadru de testare complet pentru JavaScript pe Node.js si in browser, optimizat pentru cod asincron.
Interfata BDD (BDD Interface)
const { expect } = require('chai');
describe('User Authentication', () => {
before(() => {
// Ruleaza o singura data inainte de toate testele
});
after(() => {
// Ruleaza o singura data dupa finalizarea testelor
});
beforeEach(() => {
// Ruleaza inainte de fiecare test
});
afterEach(() => {
// Ruleaza dupa fiecare test
});
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', () => {
// Omite temporar executia acestui test
});
});
Teste asincrone (Asynchronous Tests)
Promisiuni si Async/Await
it('should fetch user profile from API', async () => {
const profile = await fetchProfile(101);
expect(profile.name).to.equal('Alice');
});
Functii callback cu done()
it('should save document via callback', (done) => {
document.save((err) => {
if (err) return done(err);
done();
});
});
Ghid rapid de asertiuni Chai
// Stilul Should
chai.should();
user.should.have.property('name').equal('Alice');
list.should.have.lengthOf(3);
// Stilul Expect
expect(user).to.be.an('object');
expect(list).to.include(42);