Skip to content

Commit d5104ed

Browse files
committed
better tests
1 parent 8196012 commit d5104ed

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

src/test/blockchain.test.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,51 +7,35 @@ import { describe, test, expect, beforeAll, afterAll } from "vitest";
77

88
const TEST_DB_PATH = `${import.meta.dirname}/test-db`;
99

10-
// Utility function to clean the test database directory
11-
function cleanTestDB ()
12-
{
13-
if ( fs.existsSync( TEST_DB_PATH ) )
14-
{
15-
fs.rmSync( TEST_DB_PATH, { recursive: true });
16-
}
17-
}
18-
19-
const minerKeys = Wallet.generateKeyPair();
20-
function initializeBlockchain ()
21-
{
22-
const consensus = new POWConsensus();
23-
return new Blockchain({
24-
dbPath: TEST_DB_PATH,
25-
nodes: {
26-
list: [ "http://127.0.0.1:3001" ],
27-
hostUrl: "http://127.0.0.1:3000"
28-
},
29-
chainName: "test-chain",
30-
minerPublicKey: minerKeys.publicKey,
31-
consensus
32-
});
33-
}
34-
3510
describe( "Blockchain Test Suite", () =>
3611
{
12+
let blockchain: Blockchain;
13+
let senderKeys: KeyPair;
14+
let receiverKeys: KeyPair;
15+
let minerKeys: KeyPair;
16+
3717
beforeAll( () =>
3818
{
3919
cleanTestDB();
40-
}, 1000 );
20+
minerKeys = Wallet.generateKeyPair();
21+
blockchain = initializeBlockchain( minerKeys.publicKey ); // miner: 100
22+
senderKeys = Wallet.generateKeyPair();
23+
receiverKeys = Wallet.generateKeyPair();
24+
});
25+
4126
afterAll( () =>
4227
{
4328
cleanTestDB();
44-
}, 1000 );
29+
});
4530

4631
test( "serial test", () =>
4732
{
48-
const blockchain = initializeBlockchain(); // miner: 100
49-
50-
const senderKeys = Wallet.generateKeyPair();
51-
const receiverKeys = Wallet.generateKeyPair();
5233
const newBlock = blockchain.mineNewBlock(); // miner: 200
5334
expect( newBlock.index ).toBe( 1 );
35+
});
5436

37+
test( "parallel test", () =>
38+
{
5539
const transaction1 = new Transaction({
5640
from: minerKeys.publicKey,
5741
to: senderKeys.publicKey,
@@ -169,3 +153,26 @@ describe( "Blockchain Test Suite", () =>
169153
}
170154
});
171155
});
156+
157+
function cleanTestDB ()
158+
{
159+
if ( fs.existsSync( TEST_DB_PATH ) )
160+
{
161+
fs.rmSync( TEST_DB_PATH, { recursive: true });
162+
}
163+
}
164+
165+
function initializeBlockchain ( minerKeysPublicKey: string )
166+
{
167+
const consensus = new POWConsensus();
168+
return new Blockchain({
169+
dbPath: TEST_DB_PATH,
170+
nodes: {
171+
list: [ "http://127.0.0.1:3001" ],
172+
hostUrl: "http://127.0.0.1:3000"
173+
},
174+
chainName: "test-chain",
175+
minerPublicKey: minerKeysPublicKey,
176+
consensus
177+
});
178+
}

0 commit comments

Comments
 (0)