Using the testBridge
utility
Write the bridge.integration.test.ts
Any test that requires HTTP to work should be moved in an integration test using the file name convention .integration.test.ts
. In the case of testing a coin integration, we usually name that file bridge.integration.test.ts
and we recommend to use the testBridge
utility.
The testBridge
utility takes a “DatasetTest” as a parameter which will conduct many kind of integration tests.
First, create a bridge.integration.test.ts
file and fill it with this empty template:
import "../../__tests__/test-helpers/setup";
import { testBridge } from "../../__tests__/test-helpers/bridge";
import type { DatasetTest } from "../../types";
import type { Transaction } from "./types";
const dataset: DatasetTest<Transaction> = {
implementations: ["js"],
currencies: {
mycoin: {},
},
};
You can also generate it with a Ledger device, with a seed that you want to freeze (meaning you don’t want to do anymore transaction with that seed, or you will need to regenerate the snapshot everytime) and execute in the CLI the command:
pnpm run:cli generateTestScanAccounts -c mycoin
The expected output is:
import "../../__tests__/test-helpers/setup";
import { testBridge } from "../../__tests__/test-helpers/bridge";
import type { CurrenciesData } from "../../../types";
import type { Transaction } from "../types";
const dataset: CurrenciesData<Transaction> = {
scanAccounts: [
{
name: "mycoin seed 1",
apdus: `
=> 100112344221c00002200000000000000000000000
<= 213321ac21234122100000000000000000
=> 100112344221c00002200000800000008000000080
<= 213321ac21234122100000000000000000
=> 100112344221c00002210000800000008000000080
<= 213321ac21234122100000000000000000
=> 100112344221c00002220000800000008000000080
<= 213321ac21234122100000000000000000
=> 100112344221c00002230000800000008000000080
`,
},
],
};
testBridge(dataset);
Just keep the part with the scanAccounts and put it the mycoin
part :
import "../../__tests__/test-helpers/setup";
import { testBridge } from "../../__tests__/test-helpers/bridge";
import type { DatasetTest } from "../../types";
import type { Transaction } from "./types";
const dataset: DatasetTest<Transaction> = {
implementations: ["js"],
currencies: {
mycoin: {
scanAccounts: [
{
name: "mycoin seed 1",
apdus: `
=> 100112344221c00002200000000000000000000000
<= 213321ac21234122100000000000000000
=> 100112344221c00002200000800000008000000080
<= 213321ac21234122100000000000000000
=> 100112344221c00002210000800000008000000080
<= 213321ac21234122100000000000000000
=> 100112344221c00002220000800000008000000080
<= 213321ac21234122100000000000000000
=> 100112344221c00002230000800000008000000080
`,
},
],
},
},
};
testBridge(dataset);
Then, get info on the accounts that you want to freeze, they will be used as references for our tests. It should look something like this:
import "../../__tests__/test-helpers/setup";
import { testBridge } from "../../__tests__/test-helpers/bridge";
import type { DatasetTest } from "../../types";
import type { Transaction } from "./types";
const dataset: DatasetTest<Transaction> = {
implementations: ["js"],
currencies: {
mycoin: {
scanAccounts: [
{
name: "mycoin seed 1",
apdus: `
=> 100112344221c00002200000000000000000000000
<= 213321ac21234122100000000000000000
=> 100112344221c00002200000800000008000000080
<= 213321ac21234122100000000000000000
=> 100112344221c00002210000800000008000000080
<= 213321ac21234122100000000000000000
=> 100112344221c00002220000800000008000000080
<= 213321ac21234122100000000000000000
=> 100112344221c00002230000800000008000000080
`,
},
],
accounts: [
{
raw: {
id: `js:2:mycoin:ADDR:`,
seedIdentifier: ADDR,
name: "MyCoin 1",
derivationMode: "",
index: 0,
freshAddress: ADDR,
freshAddressPath: "44'/354'/0'/0/0'",
freshAddresses: [],
blockHeight: 0,
operations: [],
pendingOperations: [],
currencyId: "mycoin",
unitMagnitude: 10,
lastSyncDate: "",
balance: "2111000",
},
transactions: [
// HERE WE WILL INSERT OUR test
],
},
],
},
},
};
testBridge(dataset);
The accounts
part is manual. This allow you to chose the best parameters for the situation you want to test.
Use regular Jest tests if you need more flexibility
We tried to cover as many cases as possible that are in getTransactionStatus
.
You are free to define your own extra tests in bridge.integration.test.ts
(or any other integration test file) for more advanced tests that would not be covered by the bridge generic tests.
Launch the test
To launch the test do:
pnpm common jest --runTestsByPath src/families/<mycoin>/bridge.integration.test.ts
Replace <mycoin>
with the name of your coin.
How does a test work?
The transaction tests simulate an object Transaction
as input, and a TransactionStatus
as an output that we compare with an expected status.
There’s some generic tests that are already made in src/__tests__/test-helpers/bridge.ts
that are mandatory to pass.
To implement your own test in test-dataset.ts
, add an Object typed like this in the array of transactions
:
import Transaction from "./types";
type TestTransaction = {
name: string;
transaction: Transaction;
expectedStatus: {
amount: BigNumber;
errors: {};
warnings: {};
};
};
This TestTransaction
uses as mainAccount the account that we have set before and then execute the command getTransactionStatus
by using the transaction
object as input.