Merge pull request #68 from mdp/mdp/dotenv_jest
[Bugfix] JS-SDK: Remove dotenv and add tests
This commit is contained in:
commit
ebd9be3d9c
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',
|
||||||
|
};
|
3631
apps/js-sdk/firecrawl/package-lock.json
generated
3631
apps/js-sdk/firecrawl/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,14 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "@mendable/firecrawl-js",
|
"name": "@mendable/firecrawl-js",
|
||||||
"version": "0.0.15",
|
"version": "0.0.16",
|
||||||
"description": "JavaScript SDK for Firecrawl API",
|
"description": "JavaScript SDK for Firecrawl API",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"types": "types/index.d.ts",
|
"types": "types/index.d.ts",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"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",
|
||||||
@ -17,17 +17,18 @@
|
|||||||
"author": "Mendable.ai",
|
"author": "Mendable.ai",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.6.8",
|
"axios": "^1.6.8"
|
||||||
"dotenv": "^16.4.5"
|
|
||||||
},
|
},
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/mendableai/firecrawl/issues"
|
"url": "https://github.com/mendableai/firecrawl/issues"
|
||||||
},
|
},
|
||||||
"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/dotenv": "^8.2.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');
|
||||||
|
});
|
||||||
|
})
|
@ -1,6 +1,4 @@
|
|||||||
import axios, { AxiosResponse, AxiosRequestHeaders } from 'axios';
|
import axios, { AxiosResponse, AxiosRequestHeaders } from 'axios';
|
||||||
import dotenv from 'dotenv';
|
|
||||||
dotenv.config();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration interface for FirecrawlApp.
|
* Configuration interface for FirecrawlApp.
|
||||||
@ -65,7 +63,7 @@ export default class FirecrawlApp {
|
|||||||
* @param {FirecrawlAppConfig} config - Configuration options for the FirecrawlApp instance.
|
* @param {FirecrawlAppConfig} config - Configuration options for the FirecrawlApp instance.
|
||||||
*/
|
*/
|
||||||
constructor({ apiKey = null }: FirecrawlAppConfig) {
|
constructor({ apiKey = null }: FirecrawlAppConfig) {
|
||||||
this.apiKey = apiKey || process.env.FIRECRAWL_API_KEY || '';
|
this.apiKey = apiKey || '';
|
||||||
if (!this.apiKey) {
|
if (!this.apiKey) {
|
||||||
throw new Error('No API key provided');
|
throw new Error('No API key provided');
|
||||||
}
|
}
|
||||||
@ -267,4 +265,4 @@ export default class FirecrawlApp {
|
|||||||
throw new Error(`Unexpected error occurred while trying to ${action}. Status code: ${response.status}`);
|
throw new Error(`Unexpected error occurred while trying to ${action}. Status code: ${response.status}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user