If your tests are hardhat configuration specific, as specified in the CLI command, you could parse the CLI input directly in the test file. Then you could use Mocha's .skip()
to ignore specific config based conditions.
I've applied this like the below example based off of network selections:
package.json
{
...,
"scripts": {
"test": "hardhat test --network localhost",
"test-hardhat": "hardhat test --network hardhat",
"test-net": "hardhat test --network rinkeby"
},
sample-test.js
const { assert } = require('chai');
const PARMS = process.argv;
const NETWORK = PARMS[PARMS.indexOf('--network') + 1];
describe('ModifyVariable', function () {
let contract;
const inputStr = 'Applesauce';
before(async () => {
const ModifyVariable = await ethers.getContractFactory('ModifyVariable');
contract = await ModifyVariable.deploy(10, inputStr);
await contract.deployed();
});
// Maybe I'll run this. Maybe I won't...
it('should change x to 1337', async function () {
if (NETWORK === 'localhost') this.skip();
await contract.modifyToLeet();
const newX = await contract.x();
assert.equal(newX.toNumber(), 1337);
});
// Always test rename to 'Bob'.
it('should change publicString to "Bob"', async () => {
await contract.modifyToBob();
const newPublicString = await contract.publicString();
assert.equal(newPublicString, 'Bob');
});
});
describe('ModifyVariable', function () {
let contract;
const inputStr = 'Applesauce';
// Maybe I'll run this. Maybe I won't...
before(async function () {
if (NETWORK === 'localhost') this.skip();
const ModifyVariable = await ethers.getContractFactory('ModifyVariable');
contract = await ModifyVariable.deploy(10, inputStr);
await contract.deployed();
});
it(`should change publicString to "${inputStr}"`, async () => {
await contract.modifyToInput(inputStr);
const newPublicString = await contract.publicString();
assert.equal(newPublicString, inputStr);
})
});
output
$ npm run test
> hardhat-project1@1.0.0 test
> hardhat test --network localhost
ModifyVariable
- should change x to 1337
√ should change publicString to "Bob" (2157ms)
ModifyVariable
- should change publicString to "Applesauce"
1 passing (3s)
2 pending