To be merged, pull requests MUST include tests for any new features or bug fixes.
For testing all features, you need to meet some criteria in the config file located at test/config.json.
If you are running API tests (in the test/api/ directory), you need to enable the following features in the config:
{
"cacheEnabled": true,
// ...
"api": {
"enabled": true,
"access": {
"public": true,
// ...
},
// ...
},
"peers": {
"enabled": true,
// ...
}
}You need to specify at least some peers in peers.list, e.g.:
{
// ...
"peers": {
"enabled": true,
"list": [
{
"ip": "162.55.32.80",
"port": 36667
},
{
"ip": "81.0.247.181",
"port": 36667
},
{
"ip": "95.217.19.144",
"port": 36667
}
],
// ...
}
}The tests are located in the test/ directory. The structure of the directory is as follows:
api/- End-to-end API tests that require running a local test nodeunit/- Unit tests that DO NOT require running a test nodecommon/- Contains stub objects and utilities for the testsnode.js- Package for making requests to the local test nodeconfig.json- Configuration file to run a local test node; copyconfig.default.jsongenesisBlock.json- Genesis block datagenesisDelegates.json- Genesis delegate accountsgenesisPasses.json- Passphrases for the genesis accounts
All tests inside api/ and unit/ should mirror (as much as possible) the structure of the project. For example, unit tests for the modules/blocks.js module should be located in the test/unit/modules/blocks.js file.
[!IMPORTANT] > API tests require the
testnetlocal node to be running in parallel during their execution:
npm run start:testnetSee Test Environment for reference.
[!CAUTION] > Unit tests should NOT be run in parallel to prevent disruption of the node's state, and the
testnetshould be run at least once before.
To run a single test file, use the following command:
npm run test:single test/path/to/the/test.jsIf you have changed any common files (e.g., files inside test/common/, test/node.js package, etc.), consider running all tests:
# run all unit tests; remember to stop testnet node before
npm run test:unit
# run only fast unit tests (excluding time-consuming ones)
npm run test:unit:fast
# run all API tests; remember to run testnet node before
npm run test:apiSince we use the Chai package for assertions, we have a few rules for consistency:
-
Use proper English grammar in assertions
// ❌ expect({}).to.be.a('object'); // ✅ expect(true).to.be.an('object');
-
Use
to.be.trueinstead ofto.be.okBoolean values should be strictly asserted:
// ❌ expect(true).to.be.ok; // ✅ expect(true).to.be.true;
-
Use
not.toinstead ofto.notPrefer
not.tofor convention:// ❌ expect(true).to.not.be.false; // ✅ expect(true).not.to.be.false;
-
Use
.equal()instead of.eql()for===Use
.eql()only for deep assertion.// ❌ expect(true).to.eql(true); // ✅ expect(true).to.be.true;
-
Use parentheses for functions and methods in
describenames// ❌ describe(`functionName`, () => { /* ... */ }) // ✅ describe(`functionName()`, () => { /* ... */ })
Happy testing!