Skip to content

Commit ec02245

Browse files
feat: add a base for tests
1 parent d374eba commit ec02245

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

packages/actor-scraper/sitemap-scraper/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"start": "npm run start:dev",
2525
"start:prod": "node dist/main.js",
2626
"start:dev": "tsx src/main.ts",
27-
"build": "tsc"
27+
"build": "tsc",
28+
"test": "vitest run --config ../../../vitest.config.mts test"
2829
},
2930
"repository": {
3031
"type": "git",
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2+
3+
import { log } from '@crawlee/http';
4+
5+
import type { Input } from '../src/internals/consts.js';
6+
import { ProxyRotation } from '../src/internals/consts.js';
7+
import { CrawlerSetup } from '../src/internals/crawler_setup.js';
8+
9+
vi.mock('apify', () => ({
10+
Actor: {
11+
isAtHome: () => true,
12+
getEnv: () => ({}),
13+
createProxyConfiguration: async () => ({
14+
newUrl: async () => undefined,
15+
}),
16+
fail: async (message: string) => new Error(message),
17+
},
18+
}));
19+
20+
const createInput = (overrides: Partial<Input> = {}): Input => ({
21+
startUrls: [{ url: 'https://example.com' }],
22+
keepUrlFragments: false,
23+
respectRobotsTxtFile: true,
24+
pageFunction: '() => ({})',
25+
proxyConfiguration: { useApifyProxy: false },
26+
proxyRotation: ProxyRotation.Recommended,
27+
maxRequestRetries: 3,
28+
maxCrawlingDepth: 0,
29+
debugLog: false,
30+
customData: {},
31+
...overrides,
32+
});
33+
34+
describe('CrawlerSetup', () => {
35+
let initSpy: ReturnType<typeof vi.spyOn>;
36+
37+
beforeEach(() => {
38+
initSpy = vi
39+
.spyOn(CrawlerSetup.prototype as any, '_initializeAsync')
40+
.mockResolvedValue(undefined);
41+
});
42+
43+
afterEach(() => {
44+
initSpy.mockRestore();
45+
});
46+
47+
it('sets debug log level when debugLog is true', async () => {
48+
const setLevelSpy = vi.spyOn(log, 'setLevel');
49+
50+
new CrawlerSetup(createInput({ debugLog: true }));
51+
52+
expect(setLevelSpy).toHaveBeenCalledWith(log.LEVELS.DEBUG);
53+
});
54+
55+
it('stores rawInput as a JSON string', async () => {
56+
const input = createInput();
57+
const setup = new CrawlerSetup(input);
58+
59+
expect(setup.rawInput).toBe(JSON.stringify(input));
60+
});
61+
62+
it('uses the expected actor name', async () => {
63+
const setup = new CrawlerSetup(createInput());
64+
expect(setup.name).toBe('Sitemap Extractor');
65+
});
66+
});

0 commit comments

Comments
 (0)