How to `setProvider` with new OZ test environment

I’m updating our test files to use the OZ test environment but am getting this error: Please call setProvider() first before calling new(). I’ve read through all the documentation and can’t manage to setProvider()

:computer: Environment

Truffle v5.1.11 (core: 5.1.11)
Solidity v0.5.16 (solc-js)
Node v13.7.0
Web3.js v1.2.1
"@openzeppelin/test-environment": "^0.1.4",
"@openzeppelin/test-helpers": "^0.5.4",

:memo:Details

I’m updating one of our projects to use the new test-environment repo. We are using Chainlink in the project so I’m using the Chainlink contracts for testing purposes. I’ve made all the updates necessary to use the OpenZeppelin test-environment, and I am running into this error:
Error: LinkToken error: Please call setProvider() first before calling new().

It’s specific to the LinkToken Chainlink contract. In the Chainlink documentation, they say:
" For ease of use with testing, if the environment variable NODE_ENV is set, the imported truffle contract abstraction will automatically set its provider to web3.currentProvider , avoiding the need to perform a setProvider call before using it during your truffle tests."
So all I had to do was run NODE_ENV=true truffle test and I wouldn’t get the setProvider error. Running NODE_ENV=true npm test with the OZ test environment doesn’t work though and I get the error.

I tried adding the setupProvider function to the test-environment.config file but I’m not exactly sure how to use it.

:1234: Code to reproduce

const { LinkToken } = require('@chainlink/contracts/truffle/v0.4/LinkToken');
const { Oracle } = require('@chainlink/contracts/truffle/v0.4/Oracle');

  // set up Chainlink
  link = await LinkToken.new({ from: creator });
  oc = await Oracle.new(link.address, { from: creator });
  await oc.setFulfillmentPermission(oracleNode, true, { from: creator });

I’ve tried calling setProvider() before the LinkToken.new() function bc that is where the error occurs, but nothing that i’ve tried has worked. I’m not exactly sure what the issue is…whether I simply can’t use the OZ test environment for testing Chainlink contracts?

Any advice?

1 Like

Hi @kseniya,

I’m sorry that you had issues setting this up.

I used setProvider on each contract using the provider from OpenZeppelin Test Environment. (I don’t know if there is a better way than this) and managed to run a simple test successfully:

test/LinkToken.test.js

// Load dependencies
const { accounts, contract, provider } = require('@openzeppelin/test-environment');
const { expect } = require('chai');

// Load compiled artifacts
const { LinkToken } = require('@chainlink/contracts/truffle/v0.4/LinkToken');
const { Oracle } = require('@chainlink/contracts/truffle/v0.4/Oracle');

LinkToken.setProvider(provider);
Oracle.setProvider(provider);

// Start test block
describe('LinkToken', function () {

    const [creator] = accounts;

    beforeEach(async function () {
        // set up Chainlink
        link = await LinkToken.new({ from: creator });
        oc = await Oracle.new(link.address, { from: creator });
    });

    // Test case
    it('retrieve name', async function () {
        expect(await link.name()).to.equal('ChainLink Token');
    });
});
1 Like

Oh, wow. I wasn’t calling setProvider on the contracts themselves :expressionless: Thanks for your help, that worked!

1 Like

Hi @kseniya,

I hadn’t come across setProvider before either, so it took some time to work out.
Glad to be of help.

Feel free to ask all the questions that you need.