Socket
Book a DemoInstallSign in
Socket

finished-promise

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

finished-promise

Synchronous implementation of Promise for use in tests

latest
Source
npmnpm
Version
0.0.4
Version published
Maintainers
1
Created
Source

finished-promise

Synchronous implementation of Promise for use in tests.

Allows testing of asynchronous code in synchronous tests.

Instead of this:

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:

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 which is actually very fast. See ./mocha for an example.

FAQs

Package last updated on 30 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts