From 0c9d8b157f5582804cb0a3030290b224f136d7ff Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Thu, 10 Mar 2022 11:57:37 +0530 Subject: [PATCH 1/5] e2e for mocha testing added --- apps/remix-ide-e2e/src/tests/terminal.test.ts | 62 ++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/apps/remix-ide-e2e/src/tests/terminal.test.ts b/apps/remix-ide-e2e/src/tests/terminal.test.ts index f56dccf174..129ec1bc1c 100644 --- a/apps/remix-ide-e2e/src/tests/terminal.test.ts +++ b/apps/remix-ide-e2e/src/tests/terminal.test.ts @@ -104,7 +104,7 @@ module.exports = { .click('[data-id="treeViewDivtreeViewItemcontracts"]') .openFile('contracts/2_Owner.sol') .clickLaunchIcon('solidity') - .click('*[data-id="compilerContainerCompileBtn"]').pause(5000) // compile Owner + .click('*[data-id="compilerContainerCompileBtn"]').pause(20000) // compile Owner .executeScript('remix.execute(\'deployWithEthersJs.js\')') .waitForElementContainsText('*[data-id="terminalJournal"]', 'Contract Address:', 60000) .waitForElementContainsText('*[data-id="terminalJournal"]', '0xd9145CCE52D386f254917e481eB44e9943F39138', 60000) @@ -119,6 +119,28 @@ module.exports = { .waitForElementContainsText('*[data-id="terminalJournal"]', 'newOwner', 60000) .waitForElementContainsText('*[data-id="terminalJournal"]', '0xd9145CCE52D386f254917e481eB44e9943F39138', 60000) }, + 'Run tests using Mocha script and check result is logged in the terminal #group4': function (browser: NightwatchBrowser) { + browser + .click('*[data-id="treeViewDivtreeViewItem"]') + .addFile('scripts/storage.test.js', { content: storageMochaTests }) + .pause(1000) + .click('[data-id="treeViewDivtreeViewItemcontracts"]') + .openFile('contracts/1_Storage.sol') + .clickLaunchIcon('solidity') + .click('*[data-id="compilerContainerCompileBtn"]') + .pause(10000) // compile Storage + .executeScript('remix.execute(\'scripts/storage.test.js\')') + .pause(5000) + .waitForElementContainsText('*[data-id="terminalJournal"]', 'Running tests....') + .waitForElementContainsText('*[data-id="terminalJournal"]', 'storage contract Address:') + .waitForElementContainsText('*[data-id="terminalJournal"]', '✓ test initial value') + .waitForElementContainsText('*[data-id="terminalJournal"]', '✓ test updating and retrieving updated value') + .waitForElementContainsText('*[data-id="terminalJournal"]', '✘ fail test updating and retrieving updated value') + .waitForElementContainsText('*[data-id="terminalJournal"]', 'Expected: 55') + .waitForElementContainsText('*[data-id="terminalJournal"]', 'Actual: 56') + .waitForElementContainsText('*[data-id="terminalJournal"]', 'Message: incorrect number: expected 56 to equal 55') + .waitForElementContainsText('*[data-id="terminalJournal"]', '2 passing, 1 failing') + }, 'Should print hardhat logs #group4': function (browser: NightwatchBrowser) { browser @@ -261,6 +283,44 @@ const deployWithEthersJs = ` } })()` +const storageMochaTests = ` +const { expect } = require("chai"); + +describe("Storage", function () { + it("test initial value", async function () { + // Make sure contract is compiled and artifacts are generated + const metadata = JSON.parse(await remix.call('fileManager', 'getFile', 'contracts/artifacts/Storage.json')) + const signer = (new ethers.providers.Web3Provider(web3Provider)).getSigner() + let Storage = new ethers.ContractFactory(metadata.abi, metadata.data.bytecode.object, signer); + let storage = await Storage.deploy(); + console.log('storage contract Address: ' + storage.address); + await storage.deployed() + expect((await storage.retrieve()).toNumber()).to.equal(0); + }); + + it("test updating and retrieving updated value", async function () { + const metadata = JSON.parse(await remix.call('fileManager', 'getFile', 'contracts/artifacts/Storage.json')) + const signer = (new ethers.providers.Web3Provider(web3Provider)).getSigner() + let Storage = new ethers.ContractFactory(metadata.abi, metadata.data.bytecode.object, signer); + let storage = await Storage.deploy(); + await storage.deployed() + const setValue = await storage.store(56); + await setValue.wait(); + expect((await storage.retrieve()).toNumber()).to.equal(56); + }); + + it("fail test updating and retrieving updated value", async function () { + const metadata = JSON.parse(await remix.call('fileManager', 'getFile', 'contracts/artifacts/Storage.json')) + const signer = (new ethers.providers.Web3Provider(web3Provider)).getSigner() + let Storage = new ethers.ContractFactory(metadata.abi, metadata.data.bytecode.object, signer); + let storage = await Storage.deploy(); + await storage.deployed() + const setValue = await storage.store(56); + await setValue.wait(); + expect((await storage.retrieve()).toNumber(), 'incorrect number').to.equal(55); + }); +});` + const hardhatLog = ` // SPDX-License-Identifier: GPL-3.0 From 39df37ad140942570a4c17c5c1d5668b910f094a Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Thu, 10 Mar 2022 12:37:56 +0530 Subject: [PATCH 2/5] fix e2e --- apps/remix-ide-e2e/src/tests/terminal.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/remix-ide-e2e/src/tests/terminal.test.ts b/apps/remix-ide-e2e/src/tests/terminal.test.ts index 129ec1bc1c..c574eef214 100644 --- a/apps/remix-ide-e2e/src/tests/terminal.test.ts +++ b/apps/remix-ide-e2e/src/tests/terminal.test.ts @@ -119,12 +119,11 @@ module.exports = { .waitForElementContainsText('*[data-id="terminalJournal"]', 'newOwner', 60000) .waitForElementContainsText('*[data-id="terminalJournal"]', '0xd9145CCE52D386f254917e481eB44e9943F39138', 60000) }, - 'Run tests using Mocha script and check result is logged in the terminal #group4': function (browser: NightwatchBrowser) { + 'Run tests using Mocha script and check result logging in the terminal #group4': function (browser: NightwatchBrowser) { browser .click('*[data-id="treeViewDivtreeViewItem"]') .addFile('scripts/storage.test.js', { content: storageMochaTests }) .pause(1000) - .click('[data-id="treeViewDivtreeViewItemcontracts"]') .openFile('contracts/1_Storage.sol') .clickLaunchIcon('solidity') .click('*[data-id="compilerContainerCompileBtn"]') From 0073bf881d459463a13e2ea24c601b549a623830 Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Thu, 10 Mar 2022 13:31:20 +0530 Subject: [PATCH 3/5] pause --- apps/remix-ide-e2e/src/tests/terminal.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/remix-ide-e2e/src/tests/terminal.test.ts b/apps/remix-ide-e2e/src/tests/terminal.test.ts index c574eef214..a71cffd13b 100644 --- a/apps/remix-ide-e2e/src/tests/terminal.test.ts +++ b/apps/remix-ide-e2e/src/tests/terminal.test.ts @@ -104,7 +104,7 @@ module.exports = { .click('[data-id="treeViewDivtreeViewItemcontracts"]') .openFile('contracts/2_Owner.sol') .clickLaunchIcon('solidity') - .click('*[data-id="compilerContainerCompileBtn"]').pause(20000) // compile Owner + .click('*[data-id="compilerContainerCompileBtn"]').pause(5000) // compile Owner .executeScript('remix.execute(\'deployWithEthersJs.js\')') .waitForElementContainsText('*[data-id="terminalJournal"]', 'Contract Address:', 60000) .waitForElementContainsText('*[data-id="terminalJournal"]', '0xd9145CCE52D386f254917e481eB44e9943F39138', 60000) @@ -127,9 +127,9 @@ module.exports = { .openFile('contracts/1_Storage.sol') .clickLaunchIcon('solidity') .click('*[data-id="compilerContainerCompileBtn"]') - .pause(10000) // compile Storage + .pause(2000) // compile Storage .executeScript('remix.execute(\'scripts/storage.test.js\')') - .pause(5000) + .pause(1000) .waitForElementContainsText('*[data-id="terminalJournal"]', 'Running tests....') .waitForElementContainsText('*[data-id="terminalJournal"]', 'storage contract Address:') .waitForElementContainsText('*[data-id="terminalJournal"]', '✓ test initial value') From ad19a5bd0145579f57d4bfc2c0f5dc5f92bd1835 Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Thu, 10 Mar 2022 17:57:54 +0530 Subject: [PATCH 4/5] removed click --- apps/remix-ide-e2e/src/tests/terminal.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/remix-ide-e2e/src/tests/terminal.test.ts b/apps/remix-ide-e2e/src/tests/terminal.test.ts index a71cffd13b..5913e4bf63 100644 --- a/apps/remix-ide-e2e/src/tests/terminal.test.ts +++ b/apps/remix-ide-e2e/src/tests/terminal.test.ts @@ -121,13 +121,12 @@ module.exports = { }, 'Run tests using Mocha script and check result logging in the terminal #group4': function (browser: NightwatchBrowser) { browser - .click('*[data-id="treeViewDivtreeViewItem"]') .addFile('scripts/storage.test.js', { content: storageMochaTests }) .pause(1000) .openFile('contracts/1_Storage.sol') .clickLaunchIcon('solidity') .click('*[data-id="compilerContainerCompileBtn"]') - .pause(2000) // compile Storage + .pause(1000) // compile Storage .executeScript('remix.execute(\'scripts/storage.test.js\')') .pause(1000) .waitForElementContainsText('*[data-id="terminalJournal"]', 'Running tests....') From 500307c78516ccdfb5dbbc3953872f0a1c95d56f Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Thu, 10 Mar 2022 19:22:51 +0530 Subject: [PATCH 5/5] e2e for contract wiht lib --- apps/remix-ide-e2e/src/tests/terminal.test.ts | 112 +++++++++++++++++- 1 file changed, 110 insertions(+), 2 deletions(-) diff --git a/apps/remix-ide-e2e/src/tests/terminal.test.ts b/apps/remix-ide-e2e/src/tests/terminal.test.ts index 5913e4bf63..0f90f84199 100644 --- a/apps/remix-ide-e2e/src/tests/terminal.test.ts +++ b/apps/remix-ide-e2e/src/tests/terminal.test.ts @@ -139,7 +139,27 @@ module.exports = { .waitForElementContainsText('*[data-id="terminalJournal"]', 'Message: incorrect number: expected 56 to equal 55') .waitForElementContainsText('*[data-id="terminalJournal"]', '2 passing, 1 failing') }, - + 'Run tests using Mocha for a contract with library deployment and check result logging in the terminal #group4': function (browser: NightwatchBrowser) { + browser + .addFile('scripts/storageWithLib.test.js', { content: storageWithLibMochaTests }) + .pause(1000) + .click('[data-id="treeViewDivtreeViewItemcontracts"]') + .addFile('contracts/StorageWithLib.sol', { content: storageWithLibContract }) + .openFile('contracts/StorageWithLib.sol') + .clickLaunchIcon('solidity') + .click('*[data-id="compilerContainerCompileBtn"]') + .pause(1000) // compile StorageWithLib + .executeScript('remix.execute(\'scripts/storageWithLib.test.js\')') + .pause(1000) + .waitForElementContainsText('*[data-id="terminalJournal"]', 'Running tests....') + .waitForElementContainsText('*[data-id="terminalJournal"]', 'Storage with lib') + .waitForElementContainsText('*[data-id="terminalJournal"]', 'deploying lib:') + .waitForElementContainsText('*[data-id="terminalJournal"]', '✘ test library integration by calling a lib method') + .waitForElementContainsText('*[data-id="terminalJournal"]', 'Expected: 34') + .waitForElementContainsText('*[data-id="terminalJournal"]', 'Actual: 14') + .waitForElementContainsText('*[data-id="terminalJournal"]', 'Message: expected \'14\' to equal \'34\'') + .waitForElementContainsText('*[data-id="terminalJournal"]', '0 passing, 1 failing') + }, 'Should print hardhat logs #group4': function (browser: NightwatchBrowser) { browser .click('*[data-id="terminalClearConsole"]') // clear the terminal @@ -284,7 +304,7 @@ const deployWithEthersJs = ` const storageMochaTests = ` const { expect } = require("chai"); -describe("Storage", function () { +describe("Storage with lib", function () { it("test initial value", async function () { // Make sure contract is compiled and artifacts are generated const metadata = JSON.parse(await remix.call('fileManager', 'getFile', 'contracts/artifacts/Storage.json')) @@ -319,6 +339,94 @@ describe("Storage", function () { }); });` +const storageWithLibContract = ` +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity >=0.7.0 <0.9.0; + +library Lib { + function test () public view returns (uint) { + return 14; + } +} +/** + * @title Storage + * @dev Store & retrieve value inr a variable + */ +contract StorageWithLib { + + uint256 number; + + /** + * @dev Store valrue in variable + * @param num value to store + */ + function store(uint256 num) public { + number = num; + } + + /** + * @dev Return value + * @return value of 'number' + */ + function retrieve() public view returns (uint256){ + return number; + } + + function getFromLib() public view returns (uint) { + return Lib.test(); + } +} +` + +const storageWithLibMochaTests = ` +const { expect } = require("chai"); +const { ethers } = require("hardhat"); + +describe("Storage", function () { + it("test library integration by calling a lib method", async function () { + const metadataLib = JSON.parse(await remix.call('fileManager', 'readFile', 'contracts/artifacts/Lib.json')) + console.log('deploying lib:') + const artifactLib = { + contractName: 'Lib', + sourceName: 'contracts/StorageWithLib.sol', + abi: metadataLib.abi, + bytecode: '0x' + metadataLib.data.bytecode.object, + deployedBytecode: '0x' + metadataLib.data.deployedBytecode.object, + linkReferences: metadataLib.data.bytecode.linkReferences, + deployedLinkReferences: metadataLib.data.deployedBytecode.linkReferences, + } + + const optionsLib = {} + const factoryLib = await ethers.getContractFactoryFromArtifact(artifactLib, optionsLib) + const lib = await factoryLib.deploy(); + await lib.deployed() + + const metadata = JSON.parse(await remix.call('fileManager', 'readFile', 'contracts/artifacts/StorageWithLib.json')) + const artifact = { + contractName: 'StorageWithLib', + sourceName: 'contracts/StorageWithLib.sol', + abi: metadata.abi, + bytecode: '0x' + metadata.data.bytecode.object, + deployedBytecode: '0x' + metadata.data.deployedBytecode.object, + linkReferences: metadata.data.bytecode.linkReferences, + deployedLinkReferences: metadata.data.deployedBytecode.linkReferences, + } + const options = { + libraries: { + 'Lib': lib.address + } + } + + const factory = await ethers.getContractFactoryFromArtifact(artifact, options) + const storage = await factory.deploy(); + await storage.deployed() + const storeValue = await storage.store(333); + await storeValue.wait(); + expect((await storage.getFromLib()).toString()).to.equal('34'); + }); +});` + const hardhatLog = ` // SPDX-License-Identifier: GPL-3.0