I have a Truffle migration file, much like the one from the example project:
// Load zos scripts and truffle wrapper function
const { scripts, ConfigManager } = require('@openzeppelin/cli');
const { add, push, create } = scripts;
async function deploy(options) {
// Register v0 of MyContract in the zos project
add({ contractsData: [{ name: 'MyContract_v0', alias: 'MyContract' }] });
// Push implementation contracts to the network
await push(options);
// Create an instance of MyContract, setting initial value to 42
await create(Object.assign({ contractAlias: 'MyContract', methodName: 'initialize', methodArgs: [42] }, options));
}
module.exports = function(deployer, networkName, accounts) {
deployer.then(async () => {
const { network, txParams } = await ConfigManager.initNetworkConfiguration({ network: networkName, from: accounts[1] })
await deploy({ network, txParams })
})
}
I can run ganache-cli -f https://kovan.infura.io/v3/[key]@latest
followed by truffle migrate --network local
to run the migration.
But I’m using a different means of deployment in my test file, using TestHelper
:
const { TestHelper } = require("@openzeppelin/cli");
const { defaultSender } = require("@openzeppelin/test-environment");
const MyContract = Contracts.getFromLocal("MyContract_v0");
describe("MyContract", () => {
// An instance of ProxyAdminProject.
let project;
// An instance of MyContract_v0.
let myContract;
before(async function () {
project = await TestHelper({ from: defaultSender });
myContract = await project.createProxy(MyContract, {
initMethod: "initialize",
initArgs: [42],
});
});
// tests...
});
There are two reasons why I prefer migrations over the CLI:
- My contracts have multiple
initializer
parameters. - I’m forking Kovan to ganache, and because I don’t have access to archive state after a while, I need to restart ganache every so often.
Entering all of those parameters every so often is tedious, time-consuming, and prone to fat fingers. Using a migration would speed up deployment time, ensure all contracts and variables are properly initialized, and guarantee that public network deployments match those of my local tests.
I’m just unsure how to reference the contracts deployed via migration in my tests, and whether I need to modify my test script "test": "mocha --exit --recursive --no-timeout"
if I’m using Truffle.
Is it safe to deploy and test in the manner above (migration file + a separate test deployment), or am I correct that both processes should be linked? If the latter, then how would I be able to use the instance of MyContract
that was created and deployed in my migration, in my test?
After running the migration, the following in the beforeAll
block produces TypeError: MyContract.deployed is not a function
:
myContract = await MyContract.deployed();
and MyContract.address
produces null
.