-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathenv-setup.ts
57 lines (49 loc) · 1.71 KB
/
env-setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { StacksTestnet } from '@stacks/network';
import { ChainID } from '@stacks/transactions';
import { RPCClient } from 'rpc-bitcoin';
import { startApiServer } from '../../src/api/init';
import { StacksCoreRpcClient } from '../../src/core-rpc/client';
import { PgWriteStore } from '../../src/datastore/pg-write-store';
import { loadDotEnv } from '../../src/helpers';
import { TestEnvContext } from '../utils/test-helpers';
let testEnv: TestEnvContext;
beforeAll(async () => {
console.log('Jest - setup..');
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'test';
}
loadDotEnv();
process.env.PG_DATABASE = 'postgres';
process.env.STACKS_CHAIN_ID = '0x80000000';
const db = await PgWriteStore.connect({ usageName: 'tests' });
const api = await startApiServer({ datastore: db, writeDatastore: db, chainId: ChainID.Testnet });
const client = new StacksCoreRpcClient();
const stacksNetwork = new StacksTestnet({ url: `http://${client.endpoint}` });
const { BTC_RPC_PORT, BTC_RPC_HOST, BTC_RPC_PW, BTC_RPC_USER } = process.env;
if (!BTC_RPC_PORT || !BTC_RPC_HOST || !BTC_RPC_PW || !BTC_RPC_USER) {
throw new Error('Bitcoin JSON-RPC env vars not fully configured.');
}
const bitcoinRpcClient = new RPCClient({
url: BTC_RPC_HOST,
port: Number(BTC_RPC_PORT),
user: BTC_RPC_USER,
pass: BTC_RPC_PW,
timeout: 120000,
wallet: 'main',
});
testEnv = {
db,
api,
client,
stacksNetwork,
bitcoinRpcClient,
};
Object.assign(global, { testEnv });
console.log('Jest - setup done');
});
afterAll(async () => {
console.log('Jest - teardown..');
await testEnv.api.forceKill();
await testEnv.db?.close({ timeout: 0 });
console.log('Jest - teardown done');
});