Skip to content

Commit

Permalink
feat: setup jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Th0rgal committed Oct 21, 2024
1 parent bc04e9c commit 6780a16
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
1 change: 0 additions & 1 deletion index.ts

This file was deleted.

21 changes: 21 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default {
preset: "ts-jest",
testEnvironment: "node",
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},
testMatch: ["**/__tests__/**/*.ts", "**/?(*.)+(spec|test).ts"],
collectCoverage: true,
coverageDirectory: "coverage",
coverageReporters: ["text", "lcov"],
extensionsToTreatAsEsm: [".ts"],
moduleFileExtensions: ["ts", "js", "json", "node"],
transform: {
"^.+\\.ts$": [
"ts-jest",
{
useESM: true,
},
],
},
};
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"type": "module",
"scripts": {
"build": "rollup -c",
"test": "npx jest",
"prepublishOnly": "npm run build"
},
"keywords": [
Expand All @@ -20,7 +21,10 @@
"author": "Your Name",
"license": "MIT",
"dependencies": {
"cross-fetch": "^4.0.0"
"@types/jest": "^29.5.13",
"cross-fetch": "^4.0.0",
"jest": "^29.7.0",
"ts-jest": "^29.2.5"
},
"devDependencies": {
"@rollup/plugin-alias": "^4.0.2",
Expand Down
58 changes: 58 additions & 0 deletions src/tests/BitcoinRpcProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { BitcoinRpcProvider } from "@/BitcoinRpcProvider";
import fetch from "cross-fetch";

jest.mock("cross-fetch");

describe("BitcoinRpcProvider", () => {
let provider: BitcoinRpcProvider;

beforeEach(() => {
provider = new BitcoinRpcProvider({
url: "http://localhost:8332",
username: "testuser",
password: "testpass",
});
});

afterEach(() => {
jest.resetAllMocks();
});

it("should get block header", async () => {
const mockResponse = {
json: jest.fn().mockResolvedValue({ result: "mock_block_header" }),
};
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue(
mockResponse as any
);

const result = await provider.getBlockHeader("mock_block_hash");
expect(result).toBe("mock_block_header");
expect(fetch).toHaveBeenCalledWith(
"http://localhost:8332",
expect.objectContaining({
method: "POST",
body: expect.stringContaining('"method":"getblockheader"'),
})
);
});

it("should get block hash", async () => {
const mockResponse = {
json: jest.fn().mockResolvedValue({ result: "mock_block_hash" }),
};
(fetch as jest.MockedFunction<typeof fetch>).mockResolvedValue(
mockResponse as any
);

const result = await provider.getBlockHash(12345);
expect(result).toBe("mock_block_hash");
expect(fetch).toHaveBeenCalledWith(
"http://localhost:8332",
expect.objectContaining({
method: "POST",
body: expect.stringContaining('"method":"getblockhash"'),
})
);
});
});

0 comments on commit 6780a16

Please sign in to comment.