goldbergyoni / goldbergyoni/javascript-testing-best-practices

Use Mocha's context (alias for describe)

Open
#25 3 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
24.6k
Forks
2.1k
PR merge metrics
No merged PRs in 30d

Description

I like Mocha's `context` alias for `describe`. Consider the following example (from 1.1):

```JS
//1. unit under test
describe('Products Service', function() {
describe('Add new product', function() {
//2. scenario and 3. expectation
it('When no price is specified, then the product status is pending approval', ()=> {
const newProduct = new ProductService().add(...);
expect(newProduct.status).to.equal('pendingApproval');
});
});
});
```

It could be rewritten:

``` JS
//1. unit under test
describe('Products Service', () => {
// 2. specific capability
describe('Add new product',() => {
// 3. scenario
context('When no price is specified', () => {
// 4. expectation
it('Should have product status as pending approval', () => {
const newProduct = new ProductService().add(...);
expect(newProduct.status).to.equal('pendingApproval');
});
});
});
});
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.