diff --git a/README.md b/README.md index 8b742e43..194e636d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package-lock.json b/package-lock.json index a5ace851..7549761d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20363,9 +20363,9 @@ } }, "node_modules/react-silence/node_modules/@types/react": { - "version": "16.14.60", - "resolved": "https://registry.npmjs.org/@types/react/-/react-16.14.60.tgz", - "integrity": "sha512-wIFmnczGsTcgwCBeIYOuy2mdXEiKZ5znU/jNOnMZPQyCcIxauMGWlX0TNG4lZ7NxRKj7YUIZRneJQSSdB2jKgg==", + "version": "16.14.61", + "resolved": "https://registry.npmjs.org/@types/react/-/react-16.14.61.tgz", + "integrity": "sha512-CK3zd17pDWAEMnN5TdzwQJQlto2dK/lb0WZsI74owWgQ8PR60WRk0sCeBxLWuSuuqqDZKqeUcxod/8yECqrP/g==", "dev": true, "dependencies": { "@types/prop-types": "*", @@ -26112,6 +26112,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" }, @@ -27970,16 +27971,10 @@ "@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", "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" @@ -28044,16 +28039,6 @@ "dev": true, "peer": true }, - "packages/caravan-psbt/node_modules/@types/jest": { - "version": "29.5.12", - "resolved": "https://registry.npmjs.org/@types/jest/-/jest-29.5.12.tgz", - "integrity": "sha512-eDC8bTvT/QhYdxJAulQikueigY5AsdBRH2yDKW3yveW7svY3+DzN84/2NUgkw10RTiJbWqZrTtoGVdYlvFJdLw==", - "dev": true, - "dependencies": { - "expect": "^29.0.0", - "pretty-format": "^29.0.0" - } - }, "packages/caravan-psbt/node_modules/acorn": { "version": "8.11.3", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", @@ -28496,32 +28481,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "packages/caravan-psbt/node_modules/pretty-format": { - "version": "29.7.0", - "resolved": "https://registry.npmjs.org/pretty-format/-/pretty-format-29.7.0.tgz", - "integrity": "sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==", - "dev": true, - "dependencies": { - "@jest/schemas": "^29.6.3", - "ansi-styles": "^5.0.0", - "react-is": "^18.0.0" - }, - "engines": { - "node": "^14.15.0 || ^16.10.0 || >=18.0.0" - } - }, - "packages/caravan-psbt/node_modules/pretty-format/node_modules/ansi-styles": { - "version": "5.2.0", - "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-5.2.0.tgz", - "integrity": "sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA==", - "dev": true, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/chalk/ansi-styles?sponsor=1" - } - }, "packages/caravan-psbt/node_modules/supports-color": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", diff --git a/packages/caravan-bitcoin/package.json b/packages/caravan-bitcoin/package.json index 9cb771d2..d2d1906e 100644 --- a/packages/caravan-bitcoin/package.json +++ b/packages/caravan-bitcoin/package.json @@ -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" }, diff --git a/packages/caravan-clients/src/__tests__/client.test.ts b/packages/caravan-clients/src/__tests__/client.test.ts index aa1bf1d4..0464e271 100644 --- a/packages/caravan-clients/src/__tests__/client.test.ts +++ b/packages/caravan-clients/src/__tests__/client.test.ts @@ -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"); diff --git a/packages/caravan-clients/src/__tests__/wallet.test.ts b/packages/caravan-clients/src/__tests__/wallet.test.ts index dde04fb3..bdf34614 100644 --- a/packages/caravan-clients/src/__tests__/wallet.test.ts +++ b/packages/caravan-clients/src/__tests__/wallet.test.ts @@ -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"; diff --git a/packages/caravan-psbt/jest.config.ts b/packages/caravan-psbt/jest.config.ts deleted file mode 100644 index d2c20cba..00000000 --- a/packages/caravan-psbt/jest.config.ts +++ /dev/null @@ -1,14 +0,0 @@ -import type { JestConfigWithTsJest } from "ts-jest"; - -const config: JestConfigWithTsJest = { - extensionsToTreatAsEsm: [".ts"], - verbose: true, - preset: "ts-jest/presets/default-esm", - testEnvironment: "node", - testPathIgnorePatterns: ["./lib"], - transform: {}, - setupFiles: ["/jest.setup.ts"], - moduleDirectories: ["node_modules", ""], -}; - -export default config; diff --git a/packages/caravan-psbt/jest.setup.ts b/packages/caravan-psbt/jest.setup.ts deleted file mode 100644 index b0bd26f6..00000000 --- a/packages/caravan-psbt/jest.setup.ts +++ /dev/null @@ -1 +0,0 @@ -import "@inrupt/jest-jsdom-polyfills"; diff --git a/packages/caravan-psbt/package.json b/packages/caravan-psbt/package.json index 4b713eab..f0a2a8d8 100644 --- a/packages/caravan-psbt/package.json +++ b/packages/caravan-psbt/package.json @@ -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", @@ -54,16 +54,10 @@ "@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", "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" diff --git a/packages/caravan-psbt/src/functions.test.ts b/packages/caravan-psbt/src/__tests__/functions.test.ts similarity index 95% rename from packages/caravan-psbt/src/functions.test.ts rename to packages/caravan-psbt/src/__tests__/functions.test.ts index fabb23b5..d0618420 100644 --- a/packages/caravan-psbt/src/functions.test.ts +++ b/packages/caravan-psbt/src/__tests__/functions.test.ts @@ -1,4 +1,4 @@ -import { bufferize } from "./functions"; +import { bufferize } from "../functions"; describe("bufferize", () => { it("should return a buffer when given a hex string", () => { diff --git a/packages/caravan-psbt/src/psbtv0/psbt.test.ts b/packages/caravan-psbt/src/__tests__/psbtv0/psbt.test.ts similarity index 98% rename from packages/caravan-psbt/src/psbtv0/psbt.test.ts rename to packages/caravan-psbt/src/__tests__/psbtv0/psbt.test.ts index 4204d912..186eafd8 100644 --- a/packages/caravan-psbt/src/psbtv0/psbt.test.ts +++ b/packages/caravan-psbt/src/__tests__/psbtv0/psbt.test.ts @@ -1,7 +1,3 @@ -/** - * @jest-environment jsdom - */ - import { generateMultisigFromHex, P2WSH, @@ -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 diff --git a/packages/caravan-psbt/src/psbtv0/utils.test.ts b/packages/caravan-psbt/src/__tests__/psbtv0/utils.test.ts similarity index 82% rename from packages/caravan-psbt/src/psbtv0/utils.test.ts rename to packages/caravan-psbt/src/__tests__/psbtv0/utils.test.ts index ae3072ac..909872a1 100644 --- a/packages/caravan-psbt/src/psbtv0/utils.test.ts +++ b/packages/caravan-psbt/src/__tests__/psbtv0/utils.test.ts @@ -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", () => { diff --git a/packages/caravan-psbt/src/psbtv2/psbtv2.test.ts b/packages/caravan-psbt/src/__tests__/psbtv2/psbtv2.test.ts similarity index 99% rename from packages/caravan-psbt/src/psbtv2/psbtv2.test.ts rename to packages/caravan-psbt/src/__tests__/psbtv2/psbtv2.test.ts index 05e97595..e5de13a8 100644 --- a/packages/caravan-psbt/src/psbtv2/psbtv2.test.ts +++ b/packages/caravan-psbt/src/__tests__/psbtv2/psbtv2.test.ts @@ -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. @@ -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", @@ -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); }); @@ -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", @@ -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"]; @@ -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"]; diff --git a/packages/caravan-psbt/src/psbtv0/psbt.ts b/packages/caravan-psbt/src/psbtv0/psbt.ts index 9959ba60..dc86efd8 100644 --- a/packages/caravan-psbt/src/psbtv0/psbt.ts +++ b/packages/caravan-psbt/src/psbtv0/psbt.ts @@ -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"; diff --git a/packages/caravan-psbt/tsconfig.json b/packages/caravan-psbt/tsconfig.json index 9a6082a3..addbeeda 100644 --- a/packages/caravan-psbt/tsconfig.json +++ b/packages/caravan-psbt/tsconfig.json @@ -2,14 +2,9 @@ "extends": "@caravan/typescript-config/base.json", "compilerOptions": { "baseUrl": ".", - "types": [ - "node", - "jest" - ], + "types": ["node", "vitest/globals"], "paths": { - "src/*": [ - "./src/*" - ] + "src/*": ["./src/*"] } } } diff --git a/vitest.config.ts b/vitest.config.ts index a72a2292..f6b75af9 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -6,6 +6,6 @@ export default defineConfig({ test: { include: ["./**/*.test.ts"], globals: true, - environment: "jsdom", + environment: "node", }, });