generated from defi-wonderland/solidity-foundry-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration-tests-with-nodes.js
98 lines (83 loc) · 3.2 KB
/
integration-tests-with-nodes.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const { spawn } = require('child_process');
require('dotenv').config(); // Initialize dotenv to load environment variables
(async() => {
// Starting Anvil nodes for 'mainnet' and 'optimism'
console.debug(`Starting Ganache node`);
const ganachePromise = runGanacheNode();
// Waiting for both nodes to be fully operational
console.debug(`Waiting for node to be up and running...`);
const ganache = await Promise.resolve(ganachePromise);
// Running end-to-end tests
console.debug(`Running tests`);
const testProcess = spawn('yarn', [`test:integration`]);
// Handle test errors
testProcess.stderr.on('data', (data) => console.error(`Test error: ${data}`));
// Track the test result
let testPassed = true;
testProcess.stdout.on('data', (data) => {
console.info(String(data));
if (String(data).includes('Failing tests:')) testPassed = false;
});
// When tests are complete, kill the Ganache nodes
testProcess.on('close', (code) => {
console.debug(`Tests finished running, killing ganache node...`);
try {
ganache.kill();
console.debug('Ganache nodes terminated successfully.');
} catch (error) {
console.error(`Error terminating Ganache nodes: ${error}`);
}
// Exit with an error code if tests failed
if (!testPassed) {
console.error('Tests failed. Setting exit code to 1.');
process.exit(1);
}
// Exit with a success code if tests passed
process.exit(0);
});
})();
/**
* Start an Anvil node for a given network
*
* @param network: name of the network to run (string)
* @returns promise which resolves when the node is running
*/
// function runAnvilNode(network) {
// return new Promise((resolve, reject) => {
// const node = spawn('yarn', [`anvil:${network}`]);
// // Handle errors without exposing sensitive information
// node.stderr.on('data', () => {
// console.error(`Anvil ${network} node errored! Not showing the error log since it could reveal the RPC url.`);
// reject();
// });
// // Resolve the promise when the node is up
// node.stdout.on('data', (data) => {
// if (String(data).includes('Listening on')) {
// console.debug(`Anvil ${network} node up and running`);
// resolve(node);
// }
// });
// });
// }
/**
* Start a Ganache node
*
* @returns promise which resolves when the node is running
*/
function runGanacheNode() {
return new Promise((resolve, reject) => {
const node = spawn('yarn', ['ganache']);
// Handle errors without exposing sensitive information
node.stderr.on('data', () => {
console.error(`Ganache node errored! Not showing the error log since it could reveal the RPC url.`);
reject();
});
// Resolve the promise when the node is up
node.stdout.on('data', (data) => {
if (String(data).includes('Listening on')) {
console.debug(`Ganache node up and running`);
resolve(node);
}
});
});
}