Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Test]: Jest to Vitest migration caravan-psbt package #133

Merged
merged 16 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,22 @@ import { BlockchainClient, ClientType } from "@caravan/clients";
```
- Note that now not only if you make a change to `caravan/coordinator` the changes will be reflected almost instantly in the app, but you can also make a change to the dependencies and everything will rebuild (technically turborepo only rebuilds what is necessary, caching the rest). Add a console.log to the `getFeeEstimate` in the `BlockchainClient` app and see for yourself!

## Testing

Caravan monorepo uses [vitest](https://vitest.dev) testing framework to test different pacakges within it.

- For running the test suite runners hit below command in terminal

```bash
turbo run test
```

**Editing test suite files:** Test suite files can be found in `__tests__` folder, every pacakge share a common vitest config file which manages test suiter runner environment.

In some files if environment is needed to be changed specifically, it can done by adding control comments at top of the file [docs reference](https://vitest.dev/guide/environment#environments-for-specific-files)



## Troubleshooting
- you might see an error "The request '...' failed to resolve only because it was resolved as fully specified"
Webpack has an [issue](https://github.com/webpack/webpack/issues/11467#issuecomment-691873586) by default
Expand Down
53 changes: 7 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/caravan-bitcoin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"lint-staged": "^13.1.2",
"mocha": "^10.2.0",
"prettier": "^3.2.5",
"react-silence": "^1.0.4",
"tsup": "^7.2.0",
"typescript": "^4.1.3"
},
Expand Down
11 changes: 1 addition & 10 deletions packages/caravan-clients/src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@ import * as bitcoind from "../bitcoind";
import * as wallet from "../wallet";
import BigNumber from "bignumber.js";

import {
vi,
beforeEach,
afterEach,
Mocked,
MockInstance,
describe,
it,
expect,
} from "vitest";
import { Mocked, MockInstance } from "vitest";

import axios from "axios";
vi.mock("axios");
Expand Down
2 changes: 0 additions & 2 deletions packages/caravan-clients/src/__tests__/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
bitcoindListUnspent,
} from "../wallet";

import { beforeEach, afterEach, vi, describe, it, expect } from "vitest";

import * as bitcoind from "../bitcoind";
import BigNumber from "bignumber.js";

Expand Down
14 changes: 0 additions & 14 deletions packages/caravan-psbt/jest.config.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/caravan-psbt/jest.setup.ts

This file was deleted.

16 changes: 6 additions & 10 deletions packages/caravan-psbt/package.json
bucko13 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
"scripts": {
"build": "tsup src/index.ts --format cjs,esm --dts",
"dev": "npm run build -- --watch",
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest src",
"lint": "eslint src",
"ci": "npm run lint && npm run test",
"test:watch": "npm run test -- --watch",
"test:debug": "node --inspect-brk ../../node_modules/.bin/jest --runInBand"
"test": "vitest --watch=false src",
"test:watch": "vitest --watch src",
"test:debug": "node --inspect-brk vitest --runInBand"
},
"keywords": [
"bitcoin",
Expand All @@ -54,18 +54,14 @@
"@caravan/eslint-config": "*",
"@caravan/multisig": "*",
"@caravan/typescript-config": "*",
"@inrupt/jest-jsdom-polyfills": "^3.2.1",
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.12",
"@vitejs/plugin-react": "^4.3.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had moved that dependency to the root package.json file as of which I've removed it from here

"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.4.1",
"jsdom": "24.0.0",
"jsdom-global": "3.0.2",
"lodash": "^4.17.21",
"react-silence": "^1.0.4",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"vitest": "^2.0.5"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bufferize } from "./functions";
import { bufferize } from "../functions";

describe("bufferize", () => {
it("should return a buffer when given a hex string", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @jest-environment jsdom
*/

import {
generateMultisigFromHex,
P2WSH,
Expand All @@ -12,9 +8,9 @@ import {
getUnsignedMultisigPsbtV0,
validateMultisigPsbtSignature,
translatePSBT,
} from "./psbt";
} from "../../psbtv0/psbt";
import _ from "lodash";
import { psbtArgsFromFixture } from "./utils";
import { psbtArgsFromFixture } from "../../psbtv0/utils";

describe("getUnsignedMultisigPsbtV0", () => {
TEST_FIXTURES.transactions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { autoLoadPSBT } from "./utils";
import { autoLoadPSBT } from "../../psbtv0/utils";

describe("autoLoadPSBT", () => {
it("should fail if you don't send a String", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { PsbtV2, getPsbtVersionNumber } from "./";
import { test, jest } from "@jest/globals";
import { silenceDescribe } from "react-silence";
import { PsbtV2, getPsbtVersionNumber } from "../../psbtv2";

import { KeyType, PsbtGlobalTxModifiableBits } from "./types";
import { KeyType, PsbtGlobalTxModifiableBits } from "../../psbtv2/types";

const BIP_370_VECTORS_INVALID_PSBT = [
// Case: PSBTv0 but with PSBT_GLOBAL_VERSION set to 2.
Expand Down Expand Up @@ -723,7 +721,14 @@ const BIP_174_VECTORS_VALID_PSBT = [
];

describe("PsbtV2", () => {
silenceDescribe("error", "warn");
beforeAll(() => {
vi.spyOn(console, "error").mockImplementation(() => {});
vi.spyOn(console, "warn").mockImplementation(() => {});
});

afterAll(() => {
vi.restoreAllMocks();
});

test.each(BIP_370_VECTORS_INVALID_PSBT)(
"Throws with BIP0370 test vectors. $case",
Expand Down Expand Up @@ -895,9 +900,9 @@ describe("PsbtV2.isReadyForSigner", () => {
});

it("Returns not ready for Signer when the psbt has already finalized inputs", () => {
jest
.spyOn(psbt, "isReadyForTransactionExtractor", "get")
.mockReturnValue(true);
vi.spyOn(psbt, "isReadyForTransactionExtractor", "get").mockReturnValue(
true,
);
expect(psbt.isReadyForSigner).toBe(false);
});

Expand Down Expand Up @@ -1066,7 +1071,15 @@ describe("PsbtV2.nLockTime", () => {
});

describe("PsbtV2.FromV0", () => {
silenceDescribe("error", "warn");
beforeAll(() => {
vi.spyOn(console, "error").mockImplementation(() => {});
vi.spyOn(console, "warn").mockImplementation(() => {});
});

// Restore console methods
afterAll(() => {
vi.restoreAllMocks();
});

test.each(BIP_174_VECTORS_INVALID_PSBT)(
"Throws with BIP0174 test vectors. $case",
Expand Down Expand Up @@ -1161,13 +1174,20 @@ describe("getPsbtVersionNumber", () => {
});

describe("PsbtV2.addPartialSig", () => {
silenceDescribe("error", "warn");
beforeAll(() => {
vi.spyOn(console, "error").mockImplementation(() => {});
vi.spyOn(console, "warn").mockImplementation(() => {});
});

// Restore console methods
afterAll(() => {
vi.restoreAllMocks();
});
let psbt;

beforeEach(() => {
psbt = new PsbtV2();
psbt.handleSighashType = jest.fn();
psbt.handleSighashType = vi.fn();
// This has to be added so inputs can be added else addSig will fail since
// the input at the index does not exist.
psbt.PSBT_GLOBAL_TX_MODIFIABLE = ["INPUTS"];
Expand Down Expand Up @@ -1229,7 +1249,7 @@ describe("PsbtV2.removePartialSig", () => {

beforeEach(() => {
psbt = new PsbtV2();
psbt.handleSighashType = jest.fn();
psbt.handleSighashType = vi.fn();
// This has to be added so inputs can be added else removeSig will fail
// since the input at the index does not exist.
psbt.PSBT_GLOBAL_TX_MODIFIABLE = ["INPUTS"];
Expand Down
2 changes: 1 addition & 1 deletion packages/caravan-psbt/src/psbtv0/psbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { GlobalXpub } from "bip174/src/lib/interfaces.js";
// be sorted out and simplified then we can use the primary module with wasm
import * as ecc from "../../vendor/tiny-secp256k1-asmjs/lib/index.js";
import * as bitcoin from "bitcoinjs-lib-v6";
import { bufferize } from "src/functions";
import { bufferize } from "../functions";
import BigNumber from "bignumber.js";
import { reverseBuffer } from "bitcoinjs-lib-v6/src/bufferutils.js";
import { autoLoadPSBT } from "./utils";
Expand Down
9 changes: 2 additions & 7 deletions packages/caravan-psbt/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@
"extends": "@caravan/typescript-config/base.json",
"compilerOptions": {
"baseUrl": ".",
"types": [
"node",
"jest"
],
"types": ["node", "vitest/globals"],
DhairyaMajmudar marked this conversation as resolved.
Show resolved Hide resolved
"paths": {
"src/*": [
"./src/*"
]
"src/*": ["./src/*"]
}
}
}
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export default defineConfig({
test: {
include: ["./**/*.test.ts"],
globals: true,
environment: "jsdom",
environment: "node",
},
DhairyaMajmudar marked this conversation as resolved.
Show resolved Hide resolved
});
Loading