diff --git a/libs/remix-ui/solidity-unit-testing/src/lib/solidity-unit-testing.tsx b/libs/remix-ui/solidity-unit-testing/src/lib/solidity-unit-testing.tsx index c3f9adc852..5536f3d63e 100644 --- a/libs/remix-ui/solidity-unit-testing/src/lib/solidity-unit-testing.tsx +++ b/libs/remix-ui/solidity-unit-testing/src/lib/solidity-unit-testing.tsx @@ -1,7 +1,7 @@ import React, { useState, useRef, useEffect } from 'react' // eslint-disable-line // import { TestTabLogic } from './logic/testTabLogic' var async = require('async') -var ReactDOM = require('react-dom'); +import { canUseWorker, urlFromVersion } from '@remix-project/remix-solidity' import './css/style.css' @@ -42,6 +42,7 @@ export const SolidityUnitTesting = (props: any) => { let [runningTestsNumber, setRunningTestsNumber] = useState(0) let [hasBeenStopped, setHasBeenStopped] = useState(false) let [areTestsRunning, setAreTestsRunning] = useState(false) + let [isDebugging, setIsDebugging] = useState(false) @@ -176,6 +177,234 @@ export const SolidityUnitTesting = (props: any) => { setPathOptions(pathOptions) } + const testCallback = (result, runningTests) => { + console.log('result---in testCallback->', result) + // this.testsOutput.hidden = false + // let debugBtn = yo`` + // if ((result.type === 'testPass' || result.type === 'testFailure') && result.debugTxHash) { + // const { web3, debugTxHash } = result + // debugBtn = yo`
this.startDebug(debugTxHash, web3)}> + // + //
` + // debugBtn.style.cursor = 'pointer' + // } + // if (result.type === 'contract') { + // this.testSuite = result.value + // if (this.testSuites) { + // this.testSuites.push(this.testSuite) + // } else { + // this.testSuites = [this.testSuite] + // } + // this.rawFileName = result.filename + // this.runningTestFileName = this.cleanFileName(this.rawFileName, this.testSuite) + // this.outputHeader = yo` + //
+ // ${this.testSuite} (${this.rawFileName}) + //
+ // ` + // this.testsOutput.appendChild(this.outputHeader) + // } else if (result.type === 'testPass') { + // if (result.hhLogs && result.hhLogs.length) this.printHHLogs(result.hhLogs, result.value) + // this.testsOutput.appendChild(yo` + //
this.discardHighlight()} + // > + //
+ // ✓ ${result.value} + // ${debugBtn} + //
+ //
+ // `) + // } else if (result.type === 'testFailure') { + // if (result.hhLogs && result.hhLogs.length) this.printHHLogs(result.hhLogs, result.value) + // if (!result.assertMethod) { + // this.testsOutput.appendChild(yo` + //
this.highlightLocation(result.location, runningTests, result.filename)} + // > + //
+ // ✘ ${result.value} + // ${debugBtn} + //
+ // Error Message: + // "${result.errMsg}" + //
+ // `) + // } else { + // const preposition = result.assertMethod === 'equal' || result.assertMethod === 'notEqual' ? 'to' : '' + // const method = result.assertMethod === 'ok' ? '' : result.assertMethod + // const expected = result.assertMethod === 'ok' ? '\'true\'' : result.expected + // this.testsOutput.appendChild(yo` + //
this.highlightLocation(result.location, runningTests, result.filename)} + // > + //
+ // ✘ ${result.value} + // ${debugBtn} + //
+ // Error Message: + // "${result.errMsg}" + // Assertion: + //
+ // Expected value should be + //
${method}
+ //
${preposition} ${expected}
+ //
+ // Received value: + // ${result.returned} + // Skipping the remaining tests of the function. + //
+ // `) + // } + // } else if (result.type === 'logOnly') { + // if (result.hhLogs && result.hhLogs.length) this.printHHLogs(result.hhLogs, result.value) + // } + } + + const resultsCallback = (_err, result, cb) => { + // total stats for the test + // result.passingNum + // result.failureNum + // result.timePassed + cb() + } + + const updateFinalResult = (_errors, result, filename) => { + // ++this.readyTestsNumber + // this.testsOutput.hidden = false + // if (!result && (_errors && (_errors.errors || (Array.isArray(_errors) && (_errors[0].message || _errors[0].formattedMessage))))) { + // this.testCallback({ type: 'contract', filename }) + // this.currentErrors = _errors.errors + // this.setHeader(false) + // } + // if (_errors && _errors.errors) { + // _errors.errors.forEach((err) => this.renderer.error(err.formattedMessage || err.message, this.testsOutput, { type: err.severity, errorType: err.type })) + // } else if (_errors && Array.isArray(_errors) && (_errors[0].message || _errors[0].formattedMessage)) { + // _errors.forEach((err) => this.renderer.error(err.formattedMessage || err.message, this.testsOutput, { type: err.severity, errorType: err.type })) + // } else if (_errors && !_errors.errors && !Array.isArray(_errors)) { + // // To track error like this: https://github.com/ethereum/remix/pull/1438 + // this.renderer.error(_errors.formattedMessage || _errors.message, this.testsOutput, { type: 'error' }) + // } + // yo.update(this.resultStatistics, this.createResultLabel()) + // if (result) { + // const totalTime = parseFloat(result.totalTime).toFixed(2) + + // if (result.totalPassing > 0 && result.totalFailing > 0) { + // this.testsOutput.appendChild(yo` + //
+ // Result for ${filename} + // Passing: ${result.totalPassing} + // Failing: ${result.totalFailing} + // Total time: ${totalTime}s + //
+ // `) + // } else if (result.totalPassing > 0 && result.totalFailing <= 0) { + // this.testsOutput.appendChild(yo` + //
+ // Result for ${filename} + // Passing: ${result.totalPassing} + // Total time: ${totalTime}s + //
+ // `) + // } else if (result.totalPassing <= 0 && result.totalFailing > 0) { + // this.testsOutput.appendChild(yo` + //
+ // Result for ${filename} + // Failing: ${result.totalFailing} + // Total time: ${totalTime}s + //
+ // `) + // } + // // fix for displaying right label for multiple tests (testsuites) in a single file + // this.testSuites.forEach(testSuite => { + // this.testSuite = testSuite + // this.runningTestFileName = this.cleanFileName(filename, this.testSuite) + // this.outputHeader = document.querySelector(`#${this.runningTestFileName}`) + // this.setHeader(true) + // }) + + // result.errors.forEach((error, index) => { + // this.testSuite = error.context + // this.runningTestFileName = this.cleanFileName(filename, error.context) + // this.outputHeader = document.querySelector(`#${this.runningTestFileName}`) + // const isFailingLabel = document.querySelector(`.failed_${this.runningTestFileName}`) + // if (!isFailingLabel) this.setHeader(false) + // }) + // this.testsOutput.appendChild(yo` + //
+ //

+ //
+ // `) + // } + // if (this.hasBeenStopped && (this.readyTestsNumber !== this.runningTestsNumber)) { + // // if all tests has been through before stopping no need to print this. + // this.testsExecutionStopped.hidden = false + // } + // if (_errors) this.testsExecutionStoppedError.hidden = false + // if (_errors || this.hasBeenStopped || this.readyTestsNumber === this.runningTestsNumber) { + // // All tests are ready or the operation has been canceled or there was a compilation error in one of the test files. + // const stopBtn = document.getElementById('runTestsTabStopAction') + // stopBtn.setAttribute('disabled', 'disabled') + // const stopBtnLabel = document.getElementById('runTestsTabStopActionLabel') + // stopBtnLabel.innerText = 'Stop' + // if (this.data.selectedTests.length !== 0) { + // const runBtn = document.getElementById('runTestsTabRunAction') + // runBtn.removeAttribute('disabled') + // } + // this.areTestsRunning = false + // } + } + + const runTest = (testFilePath, callback) => { + console.log('runTest----->', testFilePath, hasBeenStopped) + isDebugging = false + if (hasBeenStopped) { + // this.updateFinalResult() + return + } + // this.resultStatistics.hidden = false + testTab.fileManager.readFile(testFilePath).then((content) => { + const runningTests = {} + runningTests[testFilePath] = { content } + const { currentVersion, evmVersion, optimize, runs, isUrl } = testTab.compileTab.getCurrentCompilerConfig() + const currentCompilerUrl = isUrl ? currentVersion : urlFromVersion(currentVersion) + const compilerConfig = { + currentCompilerUrl, + evmVersion, + optimize, + usingWorker: canUseWorker(currentVersion), + runs + } + const deployCb = async (file, contractAddress) => { + const compilerData = await testTab.call('compilerArtefacts', 'getCompilerAbstract', file) + await testTab.call('compilerArtefacts', 'addResolvedContract', contractAddress, compilerData) + } + testTab.testRunner.runTestSources( + runningTests, + compilerConfig, + (result) => testCallback(result, runningTests), + (_err, result, cb) => resultsCallback(_err, result, cb), + deployCb, + (error, result) => { + updateFinalResult(error, result, testFilePath) + callback(error) + }, (url, cb) => { + return testTab.contentImport.resolveAndSave(url).then((result) => cb(null, result)).catch((error) => cb(error.message)) + }, { testFilePath } + ) + }).catch((error) => { + console.log('Error in runTest---->', error) + if (error) return // eslint-disable-line + }) + } + const runTests = () => { console.log('runtests--->') areTestsRunning = true @@ -192,7 +421,7 @@ export const SolidityUnitTesting = (props: any) => { // _paq.push(['trackEvent', 'solidityUnitTesting', 'runTests']) async.eachOfSeries(tests, (value: any, key: any, callback: any) => { if (hasBeenStopped) return - // runTest(value, callback) + runTest(value, callback) }) } @@ -230,8 +459,8 @@ export const SolidityUnitTesting = (props: any) => { setSelectedTests(selectedTests) if (eChecked) { setCheckSelectAll(true) + setDisableRunButton(false) if ((readyTestsNumber === runningTestsNumber || hasBeenStopped) && stopButtonLabel.trim() === 'Stop') { - setDisableRunButton(false) setRunButtonTitle('Run tests') } } else if (!selectedTests.length) {