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.4 KB
Raw Blame
import { LaunchContext } from '@crawlee/browser-pool';

const createLaunchContext = () =>
    new LaunchContext({ id: 'id', browserPlugin: {} as never, launchOptions: {} as never });

describe('LaunchContext', () => {
    test('extend() attaches custom fields', () => {
        const context = createLaunchContext();
        context.extend({ one: 1, two: 'two' });

        expect(context.one).toBe(1);
        expect(context.two).toBe('two');
    });

    test('extend() rejects every declared field as reserved', () => {
        const context = createLaunchContext();

        // Includes `fingerprint` and `proxyUrl`, which slipped through the check in v3.
        const reservedKeys = ['id', 'browserPlugin', 'launchOptions', 'fingerprint', 'proxyUrl', 'extend'];

        for (const key of reservedKeys) {
            expect(() => context.extend({ [key]: 'anything' })).toThrow(
                `Cannot extend LaunchContext with key: ${key}, because it's reserved.`,
            );
        }
    });

    test('declared fields are set directly instead', () => {
        const context = createLaunchContext();

        const fingerprint = { fingerprint: {}, headers: {} } as never;
        context.fingerprint = fingerprint;
        expect(context.fingerprint).toBe(fingerprint);

        context.proxyUrl = 'http://proxy.com:1111/';
        // The setter normalizes the URL (no trailing slash).
        expect(context.proxyUrl).toBe('http://proxy.com:1111');
    });
});