question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Promise hangs when resolving a mocked instance

See original GitHub issue

When a Promise wraps a mocked instance, it never resolves but hangs.

const { mock, instance } = require('ts-mockito')

const MockConnector = mock()
const connector = instance(MockConnector)

function connectorFactory() {
  return Promise.resolve(connector) // <== This mock instance (Proxy object) never get resolved by Promise.
}

(async () => {
  const created = await connectorFactory() // <== This Promise hangs forever.
  console.log('Connector created') // <== Never reached
})()

This issue is same with #155 which never gets noticed nor replied. A runnable sandbox: https://runkit.com/embed/5ljjgubxm9fm

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:13
  • Comments:9

github_iconTop GitHub Comments

7reactions
hirikaratecommented, Sep 28, 2019

For now, this is my workaround

JavaScript:

const {
  mock, // Don't use this
  instance,
} = require('ts-mockito')
const { Mocker } = require('ts-mockito/lib/Mock') // Must be below require('ts-mockito')

function betterMock(clazz) {
    const mocker = new Mocker(clazz)
    mocker['excludedPropertyNames'] = ['hasOwnProperty', 'then']
    return mocker.getMock()
}

TypeScript:

import {
  mock, // Don't use this
  instance
} from 'ts-mockito'
import { Mocker } from 'ts-mockito/lib/Mock' // Must be below import 'ts-mockito'

function betterMock<T>(clazz?: (new(...args: any[]) => T) | (Function & { prototype: T }) ): T {
    const mocker = new Mocker(clazz)
    mocker['excludedPropertyNames'] = ['hasOwnProperty', 'then']
    return mocker.getMock()
}

Useage example:

const {
  mock, // Don't use this
  instance,
} = require('ts-mockito')
const { Mocker } = require('ts-mockito/lib/Mock') // Must be below require('ts-mockito')

function betterMock(clazz) {
    const mocker = new Mocker(clazz)
    mocker['excludedPropertyNames'] = ['hasOwnProperty', 'then']
    return mocker.getMock()
}

const MockConnector = betterMock() // Instead of mock()
const connector = instance(MockConnector)

function connectorFactory() {
  return Promise.resolve(connector) // <== This mock instance can be resolved by Promise.
}

(async () => {
  const created = await connectorFactory() // <== This Promise resolves.
  console.log('Connector created') // <== Reached!
})()

3reactions
murbanowiczcommented, Oct 2, 2020

@NagRock why this issue has not been addressed for such a long time?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I resolve Promise in mocked function? - Stack Overflow
If you want to create a promise which is already resolved, you can simply use Promise.resolve(someData). In your case it should be something ......
Read more >
Asynchronous Behavior | Vue Test Utils
For scenarios like this, Vue Test Utils exposes flushPromises , which causes all outstanding promises to resolve immediately. Let's see an example:.
Read more >
API Docs | fetch-mock - Wheresrhys
11.0. Returns a Promise that resolves once all fetches handled by fetch-mock have resolved. Useful for testing code that uses ...
Read more >
A Helpful Guide to Testing Promises Using Mocha - Testim Blog
Check out this example: Async/await promise test. And those are really the quickest ways to get your Mocha tests working with async code....
Read more >
How to implement a request timeout using the Promise.race ...
Learn the 24 patterns to solve any coding interview question without getting ... As a result, our request will be left hanging. ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found