How to run near-js-api tests against a localnet with multiple contracts

367 Views Asked by At

I have set up node:

nearup run localnet --binary-path ~/code/nearcore/target/release

I am trying to run a jest test case:

beforeAll(async function () {
  // NOTE: nearlib and nearConfig are made available by near-cli/test_environment
  const near = await nearlib.connect(nearConfig)
})

However there is obvious step missing how to create test accounts on your local node. This is the error:

 ● Test suite failed to run

    Can not sign transactions for account test.near, no matching key pair found in Signer.

      at node_modules/near-api-js/lib/account.js:83:23
      at Object.exponentialBackoff [as default] (node_modules/near-api-js/lib/utils/exponential-backoff.js:7:24)
      at Account.signAndSendTransaction (node_modules/near-api-js/lib/account.js:80:24)
      at Account.createAndDeployContract (node_modules/near-api-js/lib/account.js:176:9)
      at LocalTestEnvironment.setup (node_modules/near-cli/test_environment.js:39:9)

Also looks like near-js-api is hardcoded to deploy only one contract. I need to test multiple contracts. How can I deploy multiple contracts? From near-js-api test_environment.js

class LocalTestEnvironment extends NodeEnvironment {
    constructor(config) {
        super(config);
    }

    async setup() {
        this.global.nearlib = require('near-api-js');
        this.global.nearAPI = require('near-api-js');
        this.global.window = {};
        let config = require('./get-config')();
        this.global.testSettings = this.global.nearConfig = config;
        const now = Date.now();
        // create random number with at least 7 digits
        const randomNumber = Math.floor(Math.random() * (9999999 - 1000000) + 1000000);
        config = Object.assign(config, {
            contractName: 'test-account-' + now + '-' + randomNumber,
            accountId: 'test-account-' + now + '-' + randomNumber
        });
        const keyStore = new nearAPI.keyStores.UnencryptedFileSystemKeyStore(PROJECT_KEY_DIR);
        config.deps = Object.assign(config.deps || {}, {
            storage:  this.createFakeStorage(),
            keyStore,
        });
        const near = await nearAPI.connect(config);

        const masterAccount = await near.account(testAccountName);
        const randomKey = await nearAPI.KeyPair.fromRandom('ed25519');
        const data = [...fs.readFileSync('./out/main.wasm')];
        await config.deps.keyStore.setKey(config.networkId, config.contractName, randomKey);
        await masterAccount.createAndDeployContract(config.contractName, randomKey.getPublicKey(), data, INITIAL_BALANCE);

        await super.setup();
    }

near-js-sdk itself is deploying against mysterious shared-test

    case 'ci':
        return {
            networkId: 'shared-test',
            nodeUrl: 'https://rpc.ci-testnet.near.org',
            masterAccount: 'test.near',
        };
1

There are 1 best solutions below

0
On

How can I deploy multiple contracts?

You can use masterAccount.createAndDeployContract same as test_environment.js. There is nothing special there except some common init - you can instead create whatever accounts / contracts are needed for your tests directly.

near-js-sdk itself is deploying against mysterious shared-test

This is shared network which can be used to run integration tests. Unless you want to keep your dev work private – it is recommended way to run tests (as hits most realistic environment currently available for testing).

However there is obvious step missing how to create test accounts on your local node.

If you created project using create-near-app you likely have corresponding test.near keys in neardev/ project folder already. That's why above mysterious environment generally works out of box.

For your local environment you need to create test.near yourself:

NODE_ENV=local near create-account test.near --masterAccount some-existing-account

After that you can copy keys to local format (or just reconfigure UnencryptedFileSystemKeyStore to use ~/.near-credentials path):

cp ~/.near-credentials/local/test.near.json project/dir/neardev/local/test.near.json