Skip to content

Commit

Permalink
fix: move all before / beforeEach function inside describe to avoid b…
Browse files Browse the repository at this point in the history
…eing calleed multiple times
  • Loading branch information
limpbrains committed Nov 27, 2024
1 parent 27e64dc commit c56ea7a
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 206 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A self-custodial, JS Bitcoin wallet management library.",
"main": "dist/index.js",
"scripts": {
"test": "yarn build && env mocha --exit -r ts-node/register 'tests/**/*.ts'",
"test": "yarn build && env mocha --exit -r ts-node/register 'tests/**/*.test.ts'",
"test:boost": "yarn build && env mocha --exit -r ts-node/register 'tests/boost.test.ts'",
"test:wallet": "yarn build && env mocha --exit -r ts-node/register 'tests/wallet.test.ts'",
"test:receive": "yarn build && env mocha --exit -r ts-node/register 'tests/receive.test.ts'",
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import * as bitcoin from 'bitcoinjs-lib';
import { Network, networks } from 'bitcoinjs-lib';
import BIP32Factory, { BIP32Interface } from 'bip32';
import * as ecc from '@bitcoinerlab/secp256k1';
import cloneDeep from 'lodash.clonedeep';

import {
EAddressType,
EAvailableNetworks,
Expand Down Expand Up @@ -108,7 +110,6 @@ import {
import { Electrum } from '../electrum';
import { Transaction } from '../transaction';
import { GAP_LIMIT, GAP_LIMIT_CHANGE, TRANSACTION_DEFAULTS } from './constants';
import cloneDeep from 'lodash.clonedeep';
import { btcToSats } from '../utils/conversion';

const bip32 = BIP32Factory(ecc);
Expand Down
102 changes: 50 additions & 52 deletions tests/boost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,68 +25,66 @@ let waitForElectrum: TWaitForElectrum;
const rpc = new BitcoinJsonRpc(bitcoinURL);
const failure = { canBoost: false, rbf: false, cpfp: false };

beforeEach(async function () {
describe('Boost', async function () {
this.timeout(testTimeout);

let balance = await rpc.getBalance();
const address = await rpc.getNewAddress();
await rpc.generateToAddress(1, address);
beforeEach(async function () {
this.timeout(testTimeout);

let balance = await rpc.getBalance();
const address = await rpc.getNewAddress();
await rpc.generateToAddress(1, address);

while (balance < 10) {
await rpc.generateToAddress(10, address);
balance = await rpc.getBalance();
}
while (balance < 10) {
await rpc.generateToAddress(10, address);
balance = await rpc.getBalance();
}

waitForElectrum = await initWaitForElectrumToSync(
{ host: electrumHost, port: electrumPort },
bitcoinURL
);
waitForElectrum = await initWaitForElectrumToSync(
{ host: electrumHost, port: electrumPort },
bitcoinURL
);

await waitForElectrum();
await waitForElectrum();

const mnemonic = generateMnemonic();
const mnemonic = generateMnemonic();

const res = await Wallet.create({
rbf: true,
mnemonic,
// mnemonic: 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
network: EAvailableNetworks.regtest,
addressType: EAddressType.p2wpkh,
electrumOptions: {
servers: [
{
host: '127.0.0.1',
ssl: 60002,
tcp: 60001,
protocol: EProtocol.tcp
}
],
net,
tls
},
// reduce gap limit to speed up tests
gapLimitOptions: {
lookAhead: 2,
lookBehind: 2,
lookAheadChange: 2,
lookBehindChange: 2
const res = await Wallet.create({
rbf: true,
mnemonic,
// mnemonic: 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
network: EAvailableNetworks.regtest,
addressType: EAddressType.p2wpkh,
electrumOptions: {
servers: [
{
host: '127.0.0.1',
ssl: 60002,
tcp: 60001,
protocol: EProtocol.tcp
}
],
net,
tls
},
// reduce gap limit to speed up tests
gapLimitOptions: {
lookAhead: 2,
lookBehind: 2,
lookAheadChange: 2,
lookBehindChange: 2
}
});
if (res.isErr()) {
throw res.error;
}
wallet = res.value;
await wallet.refreshWallet({});
});
if (res.isErr()) {
throw res.error;
}
wallet = res.value;
await wallet.refreshWallet({});
});

afterEach(async function () {
if (wallet?.electrum) {
await wallet.electrum.disconnect();
}
});

describe('Boost', async function () {
this.timeout(testTimeout);
afterEach(async function () {
await wallet?.electrum?.disconnect();
});

it('Should fail in some cases.', async () => {
// tx not found
Expand Down
4 changes: 1 addition & 3 deletions tests/derivation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as chai from 'chai';
import { expect } from 'chai';
import {
EAddressType,
EAvailableNetworks,
Expand All @@ -8,8 +8,6 @@ import {
getKeyDerivationPathString
} from '../src';

const expect = chai.expect;

describe('Derivation Methods', () => {
it('Should return default derivation path object for p2wpkh', () => {
const pathRes = getKeyDerivationPath({
Expand Down
51 changes: 29 additions & 22 deletions tests/electrum.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
import * as chai from 'chai';
import { expect } from 'chai';
import net from 'net';
import tls from 'tls';

import { Wallet } from '../';
import { EAvailableNetworks, EAddressType, IGetUtxosResponse } from '../src';
import { TEST_MNEMONIC } from './constants';
import { Result } from '../src';
import { servers } from '../example/helpers';
import {
EAddressType,
EAvailableNetworks,
IGetUtxosResponse,
Result
} from '../src';
import { TEST_MNEMONIC } from './constants';
import { EXPECTED_SHARED_RESULTS } from './expected-results';

const expect = chai.expect;

const testTimeout = 60000;

let wallet: Wallet;

before(async function () {
describe('Electrum Methods', async function (): Promise<void> {
this.timeout(testTimeout);
const res = await Wallet.create({
mnemonic: TEST_MNEMONIC,
network: EAvailableNetworks.testnet,
addressType: EAddressType.p2wpkh,
electrumOptions: {
servers: servers[EAvailableNetworks.testnet],
net,
tls
}

before(async function () {
this.timeout(testTimeout);
const res = await Wallet.create({
mnemonic: TEST_MNEMONIC,
network: EAvailableNetworks.testnet,
addressType: EAddressType.p2wpkh,
electrumOptions: {
servers: servers[EAvailableNetworks.testnet],
net,
tls
}
});
if (res.isErr()) throw res.error;
wallet = res.value;
await wallet.refreshWallet({});
});

after(async function () {
await wallet?.electrum?.disconnect();
});
if (res.isErr()) throw res.error;
wallet = res.value;
await wallet.refreshWallet({});
});

describe('Electrum Methods', async function (): Promise<void> {
this.timeout(testTimeout);
it('connectToElectrum: Should connect to a random Electrum server', async () => {
const connectResponse = await wallet.connectToElectrum();
if (connectResponse.isErr()) throw connectResponse.error;
Expand Down
116 changes: 57 additions & 59 deletions tests/receive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,68 +26,66 @@ let waitForElectrum: TWaitForElectrum;
const rpc = new BitcoinJsonRpc(bitcoinURL);
const ml = new MessageListener();

beforeEach(async function () {
describe('Receive', async function () {
this.timeout(testTimeout);
ml.clear();

// Ensure sufficient balance in regtest
let balance = await rpc.getBalance();
const address = await rpc.getNewAddress();
await rpc.generateToAddress(1, address);

while (balance < 10) {
await rpc.generateToAddress(10, address);
balance = await rpc.getBalance();
}

waitForElectrum = await initWaitForElectrumToSync(
{ host: electrumHost, port: electrumPort },
bitcoinURL
);

await waitForElectrum();

const mnemonic = generateMnemonic();

const res = await Wallet.create({
mnemonic,
network: EAvailableNetworks.regtest,
addressType: EAddressType.p2wpkh,
electrumOptions: {
servers: [
{
host: '127.0.0.1',
ssl: 60002,
tcp: 60001,
protocol: EProtocol.tcp
}
],
net,
tls
},
// reduce gap limit to speed up tests
gapLimitOptions: {
lookAhead: 2,
lookBehind: 2,
lookAheadChange: 2,
lookBehindChange: 2
},
addressTypesToMonitor: [EAddressType.p2wpkh],
onMessage: ml.onMessage
});
if (res.isErr()) throw res.error;
wallet = res.value;
await wallet.refreshWallet({});
});

afterEach(async function () {
if (wallet?.electrum) {
await wallet.electrum.disconnect();
}
});
beforeEach(async function () {
this.timeout(testTimeout);
ml.clear();

describe('Receive', async function () {
this.timeout(testTimeout);
// Ensure sufficient balance in regtest
let balance = await rpc.getBalance();
const address = await rpc.getNewAddress();
await rpc.generateToAddress(1, address);

while (balance < 10) {
await rpc.generateToAddress(10, address);
balance = await rpc.getBalance();
}

waitForElectrum = await initWaitForElectrumToSync(
{ host: electrumHost, port: electrumPort },
bitcoinURL
);

await waitForElectrum();

const mnemonic = generateMnemonic();

const res = await Wallet.create({
mnemonic,
network: EAvailableNetworks.regtest,
addressType: EAddressType.p2wpkh,
electrumOptions: {
servers: [
{
host: '127.0.0.1',
ssl: 60002,
tcp: 60001,
protocol: EProtocol.tcp
}
],
net,
tls
},
// reduce gap limit to speed up tests
gapLimitOptions: {
lookAhead: 2,
lookBehind: 2,
lookAheadChange: 2,
lookBehindChange: 2
},
addressTypesToMonitor: [EAddressType.p2wpkh],
onMessage: ml.onMessage
});
if (res.isErr()) throw res.error;
wallet = res.value;
await wallet.refreshWallet({});
});

afterEach(async function () {
await wallet?.electrum?.disconnect();
});

it('Should generate new receiving address', async () => {
const r = await wallet.getNextAvailableAddress();
Expand Down
Loading

0 comments on commit c56ea7a

Please sign in to comment.