Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doesn't work with jest #156

Open
its-dibo opened this issue Nov 5, 2021 · 5 comments
Open

doesn't work with jest #156

its-dibo opened this issue Nov 5, 2021 · 5 comments

Comments

@its-dibo
Copy link

its-dibo commented Nov 5, 2021

when running the following code with node file.js it works, but when running the same code with jest it just runs continuously

  console.log('===1===');
  Promise.resolve().then((_data) => {
    console.log('===2===');
    data = _data;
    done = true;
  });
  deasync.loopWhile(() => !done);

console displays '====1===', but doesn't display '===2==='

@victorreinor
Copy link

I'm having the same problem, is there any way to solve this or is there a fix already?

@shmuel-krakower
Copy link

Same here, any workarounds are welcome

@ttxs25830
Copy link

ttxs25830 commented Aug 17, 2022

same on me

@radiorz
Copy link

radiorz commented Aug 4, 2023

any solution ?

@wll8
Copy link

wll8 commented Jan 2, 2025

In native Node.js tests, it can also result in an infinite loop. For example, the following three code snippets, a and c are the same, but c cannot run successfully because it is inside a test.

node - v18.19.0
os - win10 x64
deasync - 0.1.30

const deasync = require('deasync');
const test = require('node:test');

function async2sync (fn) {
  return (...args) => {
    var done = false;
    var data;
    fn(...args).then((res) => {
      data = res
    }).catch(err => {
      throw err
    }).finally(() => {
      done = true
    })
    deasync.loopWhile(() => !done);
    return data
  }
}


{ // a
  console.log(1)
  console.log(async2sync(async () => 2)())
  console.log(3)
}


{ // b
  test('synchronous passing test', (t) => {
    console.log(1)
    console.log(2)
    console.log(3)
  });
}

{ // c
  test('synchronous passing test', (t) => {
    console.log(1)
    console.log(async2sync(async () => 2)())
    console.log(3)
  });
}

Using this code snippet also faces the same issue.

function async2sync (fn) {
  return (...args) => {
    return deasync(async (cb) => {
      const res = await fn(...args)
      cb(undefined, res)
    })()
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants