https://github.com/recursivefunk/empty-promises
Promise mocking utility. Mostly used for testing....mostly.
https://github.com/recursivefunk/empty-promises
Last synced: 3 months ago
JSON representation
Promise mocking utility. Mostly used for testing....mostly.
- Host: GitHub
- URL: https://github.com/recursivefunk/empty-promises
- Owner: recursivefunk
- License: mit
- Created: 2015-12-16T20:49:08.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-12-29T15:07:31.000Z (over 10 years ago)
- Last Synced: 2025-09-28T04:44:23.900Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Install
```
npm install empty-promises
```
Quick Guide
```javascript
const emptyPromises = require('empty-promises');
const asyncThing = emptyPromises();
asyncThing()
.then((foo) => {
t.notOk(foo);
t.end();
})
.catch((e) => {
throw e;
});
const asyncThingWithArgs = emptyPromises(['foo', 'bar']);
asyncThingWithArgs()
.spread((foo, bar) => {
t.equal(foo, 'foo');
t.equal(bar, 'bar');
t.end();
})
.catch((e) => {
throw e;
});
const err = new Error('oh no!');
const asyncThingWithErr = emptyPromises(null, err);
asyncThing()
.then(() => {
t.fail('I should have caught an error');
})
.catch((e) => {
t.equal(e.message, 'oh no!');
t.end();
});
```
Practical Usage
```javascript
// in mocks.js
exports.mockSuccessObj = {
doATask: emptyPromises()
};
exports.mockFailObj = {
doATask: emptyPromises(null, new Error('Task failed!'))
};
// in test.js
const test = require('tape');
const mocks = require('./mocks');
test('it works when it works', (t) => {
const aBusinessObject = aBusinessObjectFactory(mocks.mockSuccessObj);
aBusinessObject.doStuffThatInvolvesMock()
.then(() => {
t.end('Yay!');
})
.catch((e) => {
t.fail('I should not be here');
});
});
test('it fails when it fails', (t) => {
const aBusinessObject = aBusinessObjectFactory(mocks.mockFailObj);
aBusinessObject.doStuffThatInvolvesMock()
.then(() => {
t.fail('I should not be here');
})
.catch((e) => {
t.equal(e.message, 'Task failed!');
t.end('You have failed and that is a good thing!');
});
});
```