Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Fix flaky test

Ondra Urban9db7d44abf0a
diff --git a/test/events.test.js b/test/events.test.js
index 96c2d64b2..0bbd8193f 100644
--- a/test/events.test.js
+++ b/test/events.test.js
@@ -115,18 +115,23 @@ describe('Apify.events', () => {
});
test('should send persist state events in regular interval', async () => {
+ const clock = sinon.useFakeTimers();
process.env.APIFY_TEST_PERSIST_INTERVAL_MILLIS = 1;
- const eventsReceived = [];
- Apify.events.on(ACTOR_EVENT_NAMES_EX.PERSIST_STATE, data => eventsReceived.push(data));
- await Apify.initializeEvents();
- await sleep(10);
- await Apify.stopEvents();
- const eventCount = eventsReceived.length;
- expect(eventCount).toBeGreaterThan(2);
- await sleep(10);
- expect(eventsReceived.length).toEqual(eventCount);
-
- delete process.env.APIFY_TEST_PERSIST_INTERVAL_MILLIS;
+ try {
+ const eventsReceived = [];
+ Apify.events.on(ACTOR_EVENT_NAMES_EX.PERSIST_STATE, data => eventsReceived.push(data));
+ await Apify.initializeEvents();
+ clock.tick(1);
+ clock.tick(1);
+ clock.tick(1);
+ await Apify.stopEvents();
+ const eventCount = eventsReceived.length;
+ expect(eventCount).toBe(3);
+ expect(eventsReceived.length).toEqual(eventCount);
+ } finally {
+ clock.restore();
+ delete process.env.APIFY_TEST_PERSIST_INTERVAL_MILLIS;
+ }
});
});