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.

1.5 KB
Raw Blame
import { MemoryStorageBackend } from '@crawlee/core';
import type { RequestQueueBackend } from '@crawlee/types';

describe('RequestQueue handledRequestCount should update', () => {
    const storage = new MemoryStorageBackend();

    let requestQueue: RequestQueueBackend;

    beforeAll(async () => {
        requestQueue = await storage.createRequestQueueBackend({ name: 'handledRequestCount' });
    });

    test('after marking a request as handled, it should increment the handledRequestCount', async () => {
        await requestQueue.addBatchOfRequests([{ url: 'http://example.com/1', uniqueKey: '1' }]);

        const request = await requestQueue.fetchNextRequest();
        expect(request).not.toBeNull();

        await requestQueue.markRequestAsHandled({
            url: 'http://example.com/1',
            uniqueKey: '1',
            id: request!.id!,
        });

        const updatedStatistics = await requestQueue.getMetadata();
        expect(updatedStatistics.handledRequestCount).toEqual(1);
    });

    test('adding an already handled request should increment the handledRequestCount', async () => {
        await requestQueue.addBatchOfRequests([
            {
                url: 'http://example.com/2',
                uniqueKey: '2',
                handledAt: new Date().toISOString(),
            },
        ]);

        const updatedStatistics = await requestQueue.getMetadata();
        expect(updatedStatistics.handledRequestCount).toEqual(2);
    });
});