Chore: Add some basic jest tests
This commit is contained in:
parent
a7be09e479
commit
e8b8150b56
5
apps/js-sdk/firecrawl/jest.config.cjs
Normal file
5
apps/js-sdk/firecrawl/jest.config.cjs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
||||||
|
module.exports = {
|
||||||
|
preset: 'ts-jest',
|
||||||
|
testEnvironment: 'node',
|
||||||
|
};
|
3622
apps/js-sdk/firecrawl/package-lock.json
generated
3622
apps/js-sdk/firecrawl/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -8,7 +8,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"publish": "npm run build && npm publish --access public",
|
"publish": "npm run build && npm publish --access public",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "jest src/**/*.test.ts"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -24,8 +24,11 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/mendableai/firecrawl#readme",
|
"homepage": "https://github.com/mendableai/firecrawl#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@jest/globals": "^29.7.0",
|
||||||
"@types/axios": "^0.14.0",
|
"@types/axios": "^0.14.0",
|
||||||
"@types/node": "^20.12.7",
|
"@types/node": "^20.12.7",
|
||||||
|
"jest": "^29.7.0",
|
||||||
|
"ts-jest": "^29.1.2",
|
||||||
"typescript": "^5.4.5"
|
"typescript": "^5.4.5"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
22
apps/js-sdk/firecrawl/src/__tests__/fixtures/scrape.json
Normal file
22
apps/js-sdk/firecrawl/src/__tests__/fixtures/scrape.json
Normal file
File diff suppressed because one or more lines are too long
48
apps/js-sdk/firecrawl/src/__tests__/index.test.ts
Normal file
48
apps/js-sdk/firecrawl/src/__tests__/index.test.ts
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import { describe, test, expect, jest } from '@jest/globals';
|
||||||
|
import axios from 'axios';
|
||||||
|
import FirecrawlApp from '../index';
|
||||||
|
|
||||||
|
import { readFile } from 'fs/promises';
|
||||||
|
import { join } from 'path';
|
||||||
|
|
||||||
|
// Mock jest and set the type
|
||||||
|
jest.mock('axios');
|
||||||
|
const mockedAxios = axios as jest.Mocked<typeof axios>;
|
||||||
|
|
||||||
|
// Get the fixure data from the JSON file in ./fixtures
|
||||||
|
async function loadFixture(name: string): Promise<string> {
|
||||||
|
return await readFile(join(__dirname, 'fixtures', `${name}.json`), 'utf-8')
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('the firecrawl JS SDK', () => {
|
||||||
|
|
||||||
|
test('Should require an API key to instantiate FirecrawlApp', async () => {
|
||||||
|
const fn = () => {
|
||||||
|
new FirecrawlApp({ apiKey: undefined });
|
||||||
|
};
|
||||||
|
expect(fn).toThrow('No API key provided');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Should return scraped data from a /scrape API call', async () => {
|
||||||
|
const mockData = await loadFixture('scrape');
|
||||||
|
mockedAxios.post.mockResolvedValue({
|
||||||
|
status: 200,
|
||||||
|
data: JSON.parse(mockData),
|
||||||
|
});
|
||||||
|
|
||||||
|
const apiKey = 'YOUR_API_KEY'
|
||||||
|
const app = new FirecrawlApp({ apiKey });
|
||||||
|
// Scrape a single URL
|
||||||
|
const url = 'https://mendable.ai';
|
||||||
|
const scrapedData = await app.scrapeUrl(url);
|
||||||
|
|
||||||
|
expect(mockedAxios.post).toHaveBeenCalledTimes(1);
|
||||||
|
expect(mockedAxios.post).toHaveBeenCalledWith(
|
||||||
|
expect.stringMatching(/^https:\/\/api.firecrawl.dev/),
|
||||||
|
expect.objectContaining({ url }),
|
||||||
|
expect.objectContaining({ headers: expect.objectContaining({'Authorization': `Bearer ${apiKey}`}) }),
|
||||||
|
)
|
||||||
|
expect(scrapedData.success).toBe(true);
|
||||||
|
expect(scrapedData.data.metadata.title).toEqual('Mendable');
|
||||||
|
});
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user