Skip to content

Commit

Permalink
REW-4685 set global nock assertion timeout (#12)
Browse files Browse the repository at this point in the history
* Implement global setTimeout

* Update README

* Minor bump

* Audit fix non-breaking

* Audit fix with breaking changes (jest v24)

* List default timeout in readme

* Be -> by

* Add semicolons to readme code

* Explain timeout

* Clarify

* Remove the

* Reduce example
  • Loading branch information
gledrich authored Jul 24, 2019
1 parent 889336b commit 3d8c72d
Show file tree
Hide file tree
Showing 4 changed files with 2,400 additions and 2,610 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,22 @@ it('requestedWithHeadersMatch', () => {
});
```

## Setting a Timeout
* By default, a timeout of 2 seconds is applied to assertions on nock requests. This means that if nock has not intercepted the request within the set time, the assertion will be false
* You can set a custom global timeout by calling `setTimeout` on the `chaiNock` object:
```javascript
const chaiNock = require('chai-nock');

chai.use(chaiNock);
// Set a timeout of 10 seconds
chaiNock.setTimeout(10000);
```
* WARNING: If not set already, the test timeout must be greater than that of chaiNock!
```javascript
jest.setTimeout(12000);
```


## Usage

```javascript
Expand Down
15 changes: 12 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

const equal = require('deep-equal');

module.exports = chai => {
const config = {
timeout: 2000,
};

const chaiNock = chai => {
const { Assertion } = chai;
const MAX_TIMEOUT = 2000;

function promisfyNockInterceptor(nock) {
return new Promise((resolve, reject) => {
Expand All @@ -13,7 +16,7 @@ module.exports = chai => {

const timeout = setTimeout(() => {
reject(new Error('The request has not been recieved by Nock'));
}, MAX_TIMEOUT);
}, config.timeout);

nock.once(
'request',
Expand Down Expand Up @@ -155,3 +158,9 @@ module.exports = chai => {
);
});
};

module.exports = chaiNock;

module.exports.setTimeout = timeout => {
config.timeout = timeout;
};
Loading

0 comments on commit 3d8c72d

Please sign in to comment.