Bir Söz ve Ayar aşımı olan bir işlevi test etmek, neden zaman aşımına uğradı?

0

Soru

Sahip olduğu bir işlevi test etmeye çalışıyorum setTimeout bir sözün içinde. Ancak zaman aşımına uğrar.

Bu işlev:

export const sleep = async (duration: number): Promise<void> => {
  await new Promise<void>((resolve) => {
    setTimeout(resolve, duration);
  });

  if (process.env.NODE_ENV === "test") {
    console.log("sleep end");
  }
};

Ve bu benim sınavım:

import { sleep } from "../../helpers/utils";

console.log = jest.fn();
jest.useFakeTimers();

test("calls sleep with correct argument and calls console.log", async () => {
  const NODE_ENV = "test";
  const SLEEP_DURATION = "100";

  process.env = { ...process.env, NODE_ENV, SLEEP_DURATION };

  const timeoutSpy = jest.spyOn(global, "setTimeout");

  await sleep(+SLEEP_DURATION);

  jest.runAllTimers();

  expect(sleep).toHaveBeenCalledWith(+SLEEP_DURATION);
  expect(timeoutSpy).toHaveBeenCalledWith(+SLEEP_DURATION);
  expect(console.log).toHaveBeenCalledWith("sleep end");
});

Sorun şu ki, bunu çalıştırmaya çalıştığımda test başarısız oluyor ve bu mesajı veriyor:

thrown: "Exceeded timeout of 5000 ms for a test.
    Use jest.setTimeout(newTimeout) to increase the timeout value, if this is a long-running test."

Denedim jest.setTimeout(10000) bu sadece bir hata atar Exceeded timeout of 10000ms ...

Neden olarak herhangi bir fikir oluyor? Ya da nasıl düzeltebilirim?

Teşekkürler!

javascript jestjs settimeout timeout
2021-11-24 01:41:50
1

En iyi cevabı

1

İşte, ne için gittiğinize daha da yaklaşan bir çözüm. Önemli olan, yapamazsın await sahte zamanlayıcılar kullanarak sözün çözümü ya da asla çözülmeyecek. Bunun yerine, arayabileceğiniz assign dönüş değeri sleep bir değişkene işlev verin, sonra zamanlayıcıları çalıştırın, sonra değişkeni bekleyin.

Ayrıca ayarladım senin expect iki argüman gerektirdiğinden zaman aşımı casusu için ifade. Son olarak, bu expectationt senin kaldırıldı sleep süre ile çağrılır, çünkü bunu tam anlamıyla testte yapıyorsunuz, bu yüzden bu iddiayı yapmaya değmez.

console.log = jest.fn();
jest.useFakeTimers();

test("calls sleep with correct argument and calls console.log", async () => {
  const NODE_ENV = "test";
  const SLEEP_DURATION = "100";

  process.env = { ...process.env, NODE_ENV, SLEEP_DURATION };

  const timeoutSpy = jest.spyOn(global, "setTimeout");

  const sleepFn = sleep(+SLEEP_DURATION);

  jest.runAllTimers();

  // Now that we ran timers we can await the promise resolving 
  await sleepFn;

  // timeout takes two arguments
  expect(timeoutSpy).toHaveBeenCalledWith(
    expect.any(Function),
    +SLEEP_DURATION
  );
  expect(console.log).toHaveBeenCalledWith("sleep end");
});
2021-11-24 02:03:50

Bilgilendirici cevabınız için teşekkürler, bu şaka zamanlayıcılarını ilk kez kullanıyorum, bu yüzden kesinlikle biraz çalışmam gerekiyor!
ffx292

Diğer dillerde

Bu sayfa diğer dillerde

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................