abbr / deasync

Turns async function into sync via JavaScript wrapper of Node event loop
MIT License
972 stars 73 forks source link

Example usage appears to hang mocha #169

Closed j1n3l0 closed 2 years ago

j1n3l0 commented 2 years ago

Following the first usage example in the readme.md I would expect the following to work:

// src/index.ts
import Consul from 'consul';

export const asyncFn = async (): Promise<string> =>
  ((await new Consul({ promisify: true }).kv.get('demo')) as { Value: string })
    .Value;
// test/deasync.spec.ts
import { expect } from 'chai';
import { describe } from 'mocha';
import deasync from 'deasync';
import nock from 'nock';
import { asyncFn } from '../src';

describe('Given an async function', () => {
  describe('using deasync', () => {
    const scope = nock('http://127.0.0.1:8500');

    it('should get the value out', () => {
      scope
        .get('/v1/kv/demo')
        .reply(200, [{ Value: Buffer.from('OK').toString('base64') }]);
      expect(deasync(asyncFn)()).to.equal('OK');
    });
  });
});

However this causes my tests to hang indefinitely when running with mocha.

Is this incorrect usage on my part or something else?

My environment is as follows:

$ node --version
v17.0.0-rc.3
$ npm --version
8.3.2
$ npx mocha --version
9.2.2

This example is taken from a demo repo here.

j1n3l0 commented 2 years ago

Decided one really ought not to be messing with this sort of magic so there is no need to try and resolve this.