如何在 JEST 最终运行之前测试 promise

How to test promise before it runs finally with JEST

我有这样的功能:

let settings = {
  isRun: false
}
function testPromise(promise) {
  settings.isRun = true;
  return promise.finally(() => settings.isRun = false;)
}

如何在 finally 表达式 运行 之前测试 isRun = true when 运行 testPromise。我试过

it('should isRun true when testPromise running', async () => {
   expect(settings.isRun).toBeFalsy();
   await testPromise(Promise.resolve('run')).then(() => {
     expect(settings.isRun).toBeTruthy();
   }
   expect(settings.isRun).toBeFalsy();

但是没用

我认为你可以执行但不等待承诺来观察你想要看到的间歇性价值变化。

it('should isRun true when testPromise running', () => {
    expect(settings.isRun).toBeFalsy();
    const promise = testPromise(Promise.resolve('run'));
    // At this point, promise is not resolved yet, so the callback in finally has not executed yet.
    expect(settings.isRun).toBeTruthy();
    // If you want to test it changed back to fasle
    promise.then(() => {
        expect(settings.isRun).toBeFalsy();
    });
}