Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REW-4685 set global nock assertion timeout #12

Merged
merged 12 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!
gledrich marked this conversation as resolved.
Show resolved Hide resolved
```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);
gledrich marked this conversation as resolved.
Show resolved Hide resolved

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