mirror of
https://github.com/goldbergyoni/nodebestpractices.git
synced 2025-10-27 10:55:55 +08:00
81 lines
2.6 KiB
Markdown
81 lines
2.6 KiB
Markdown
# Test error flows using your favorite test framework
|
||
|
||
### One Paragraph Explainer
|
||
|
||
Testing ‘happy’ paths is no better than testing failures. Good testing code coverage demands to test exceptional paths. Otherwise, there is no trust that exceptions are indeed handled correctly. Every unit testing framework, like [Mocha](https://mochajs.org/) & [Chai](http://chaijs.com/), supports exception testing (code examples below). If you find it tedious to test every inner function and exception you may settle with testing only REST API HTTP errors.
|
||
|
||
### Code example: ensuring the right exception is thrown using Mocha & Chai
|
||
|
||
<details>
|
||
<summary><strong>Javascript</strong></summary>
|
||
|
||
```javascript
|
||
describe('Facebook chat', () => {
|
||
it('Notifies on new chat message', () => {
|
||
const chatService = new chatService();
|
||
chatService.participants = getDisconnectedParticipants();
|
||
expect(chatService.sendMessage.bind({ message: 'Hi' })).to.throw(ConnectionError);
|
||
});
|
||
});
|
||
```
|
||
</details>
|
||
|
||
<details>
|
||
<summary><strong>Typescript</strong></summary>
|
||
|
||
```typescript
|
||
describe('Facebook chat', () => {
|
||
it('Notifies on new chat message', () => {
|
||
const chatService = new chatService();
|
||
chatService.participants = getDisconnectedParticipants();
|
||
expect(chatService.sendMessage.bind({ message: 'Hi' })).to.throw(ConnectionError);
|
||
});
|
||
});
|
||
```
|
||
</details>
|
||
|
||
### Code example: ensuring API returns the right HTTP error code
|
||
|
||
<details>
|
||
<summary><strong>Javascript</strong></summary>
|
||
|
||
```javascript
|
||
it('Creates new Facebook group', () => {
|
||
const invalidGroupInfo = {};
|
||
return httpRequest({
|
||
method: 'POST',
|
||
uri: 'facebook.com/api/groups',
|
||
resolveWithFullResponse: true,
|
||
body: invalidGroupInfo,
|
||
json: true
|
||
}).then((response) => {
|
||
expect.fail('if we were to execute the code in this block, no error was thrown in the operation above')
|
||
}).catch((response) => {
|
||
expect(400).to.equal(response.statusCode);
|
||
});
|
||
});
|
||
```
|
||
</details>
|
||
|
||
<details>
|
||
<summary><strong>Typescript</strong></summary>
|
||
|
||
```typescript
|
||
it('Creates new Facebook group', async () => {
|
||
let invalidGroupInfo = {};
|
||
try {
|
||
const response = await httpRequest({
|
||
method: 'POST',
|
||
uri: 'facebook.com/api/groups',
|
||
resolveWithFullResponse: true,
|
||
body: invalidGroupInfo,
|
||
json: true
|
||
})
|
||
// if we were to execute the code in this block, no error was thrown in the operation above
|
||
expect.fail('The request should have failed')
|
||
} catch(response) {
|
||
expect(400).to.equal(response.statusCode);
|
||
}
|
||
});
|
||
```
|
||
</details> |