https://github.com/featurist/finished-promise
Synchronous implementation of Promise for use in tests
https://github.com/featurist/finished-promise
Last synced: 5 months ago
JSON representation
Synchronous implementation of Promise for use in tests
- Host: GitHub
- URL: https://github.com/featurist/finished-promise
- Owner: featurist
- Created: 2016-12-03T15:15:14.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-28T08:57:42.000Z (about 7 years ago)
- Last Synced: 2024-11-10T02:52:37.345Z (6 months ago)
- Language: JavaScript
- Size: 9.77 KB
- Stars: 1
- Watchers: 6
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# finished-promise
Synchronous implementation of [Promise](https://promisesaplus.com/) for use in tests.
Allows testing of asynchronous code in synchronous tests.
Instead of this:
```js
describe('Promise', () => {
it('finishes asynchronously, so we can only write asynchronous tests for code that uses Promise', () => {
let result
Promise.resolve(123).then(resolved => {
result = resolved
})
assert.equal(result, undefined)
return soon(() => assert.equal(result, 123))
})
})
```We can do this:
```js
describe('FinishedPromise', () => {
it('finishes synchronously, so we can write synchronous tests when we use it in place of Promise', () => {
let result
FinishedPromise.resolve(123).then(resolved => {
result = resolved
})
assert.equal(result, 123)
})
})
```## async / await
Overriding `global.Promise` has no effect on `async` functions - they will use
the native v8 `Promise` regardless.To circumvent this limitation we can transpile the source code prior to running
it. Babel can do this, although rather slowly, which would defeat the purpose
of this library - fast tests!Instead we can use [async-to-gen](https://github.com/leebyron/async-to-gen) which
is actually very fast. See `./mocha` for an example.## We're Hiring!
Featurist provides full stack, feature driven development teams. Want to join us? Check out [our career opportunities](https://www.featurist.co.uk/careers/).