Skip to content

Commit

Permalink
feat: jest tests + release script
Browse files Browse the repository at this point in the history
  • Loading branch information
jenslys committed Nov 24, 2024
1 parent c484e59 commit 13efffb
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 53 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Build, Test and Publish

on:
push:
branches:
- main

jobs:
build-and-publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "20.x"
registry-url: "https://registry.npmjs.org"

- name: Install dependencies
run: npm ci

- name: Build
run: npm run build

- name: Run tests
run: npm test

- name: Check version change
id: check
run: |
git diff HEAD^ HEAD -G"\"version\": \"[^\"]*\"" package.json | grep "\"version\":" || echo "No version change"
if git diff HEAD^ HEAD -G"\"version\": \"[^\"]*\"" package.json | grep -q "\"version\":"; then
echo "version_changed=true" >> $GITHUB_OUTPUT
else
echo "version_changed=false" >> $GITHUB_OUTPUT
fi
- name: Publish to npm
if: steps.check.outputs.version_changed == 'true'
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testMatch: ['**/__tests__/**/*.ts?(x)', '**/?(*.)+(spec|test).ts?(x)'],
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"build": "tsc",
"test": "jest",
"prepare": "npm run build",
"dev:test": "ts-node -r dotenv/config src/test.ts"
"dev:test": "ts-node -r dotenv/config src/test.ts",
"dev": "ts-node -r dotenv/config src/dev.ts"
},
"author": "",
"license": "MIT",
Expand Down
28 changes: 28 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Skrape, SkrapeError } from '../index';
import { z } from 'zod';

describe('Skrape', () => {
let skrape: Skrape;

beforeEach(() => {
skrape = new Skrape({
apiKey: 'test-key',
});
});

it('should create a Skrape instance', () => {
expect(skrape).toBeInstanceOf(Skrape);
});

it('should throw error if apiKey is missing', () => {
expect(() => new Skrape({} as any)).toThrow();
});

it('should have correct default baseUrl', () => {
expect((skrape as any).baseUrl).toBe('https://skrape.ai/api');
});

it('should store API key', () => {
expect((skrape as any).apiKey).toBe('test-key');
});
});
51 changes: 51 additions & 0 deletions src/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Skrape, SkrapeError } from "./index";
import { z } from "zod";
import dotenv from "dotenv";

dotenv.config();

const apiKey = process.env.SKRAPE_API_KEY;
if (!apiKey) {
throw new Error("SKRAPE_API_KEY is required");
}

const skrape = new Skrape({
apiKey,
baseUrl: "http://localhost:3000/api",
});

const newsSchema = z.object({
topStories: z
.array(
z.object({
title: z.string(),
url: z.string(),
score: z.number(),
author: z.string(),
commentCount: z.number(),
})
)
.max(3),
});

async function test() {
try {
const result = await skrape.extract(
"https://news.ycombinator.com",
newsSchema
);
console.log(JSON.stringify(result, null, 2));
} catch (error) {
if (error instanceof SkrapeError) {
console.error("SkrapeError:", error.message);
console.error("Status:", error.status);
if (error.retryAfter) {
console.error("Retry after:", error.retryAfter, "seconds");
}
} else {
console.error("Error:", error);
}
}
}

test();
52 changes: 0 additions & 52 deletions src/test.ts

This file was deleted.

0 comments on commit 13efffb

Please sign in to comment.