mirror of https://github.com/embarklabs/embark.git
feat(@embark/test-runner): introduce artifacts.require API
This commit adds a convenience API `artifacts.require(name)` that aims to make requiring artifacts a little bit more straight forward. Usage: ``` const SimpleStorage = artifacts.require('SimpleStorage'); const EmbarkJS = artifacts.require('EmbarkJS'); ```
This commit is contained in:
parent
70313352a5
commit
b021689387
|
@ -1,6 +1,6 @@
|
|||
/*global contract, it*/
|
||||
/*global artifacts, contract, it*/
|
||||
/*
|
||||
const SimpleStorage = require('Embark/contracts/SimpleStorage');
|
||||
const SimpleStorage = artifacts.require('SimpleStorage');
|
||||
|
||||
let accounts;
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*global contract, config, it, assert, web3*/
|
||||
const SimpleStorage = require('Embark/contracts/SimpleStorage');
|
||||
/*global artifacts, contract, config, it, assert, web3*/
|
||||
const SimpleStorage = artifacts.require('SimpleStorage');
|
||||
|
||||
let accounts;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*global contract, it*/
|
||||
/*global artifacts, contract, it*/
|
||||
/*
|
||||
const SimpleStorage = require('Embark/contracts/SimpleStorage');
|
||||
const SimpleStorage = artifacts.require('SimpleStorage');
|
||||
|
||||
let accounts;
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*global contract, config, it, web3*/
|
||||
/*global artifacts, contract, config, it, web3*/
|
||||
const assert = require('assert');
|
||||
const AnotherStorage = require('Embark/contracts/AnotherStorage');
|
||||
const SimpleStorage = require('Embark/contracts/SimpleStorage');
|
||||
const AnotherStorage = artifacts.require('AnotherStorage');
|
||||
const SimpleStorage = artifacts.require('SimpleStorage');
|
||||
|
||||
let accounts, defaultAccount;
|
||||
const numAddresses = 10;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*global contract, config, it, web3*/
|
||||
/*global artifacts, contract, config, it, web3*/
|
||||
const assert = require('assert');
|
||||
const SomeContract = require('Embark/contracts/SomeContract');
|
||||
const MyToken2 = require('Embark/contracts/MyToken2');
|
||||
const SomeContract = artifacts.require('SomeContract');
|
||||
const MyToken2 = artifacts.require('MyToken2');
|
||||
|
||||
config({
|
||||
contracts: {
|
||||
|
@ -38,4 +38,3 @@ contract("SomeContract", function() {
|
|||
});
|
||||
|
||||
});
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*global describe, it, web3, config*/
|
||||
/*global artifacts, describe, it, web3, config*/
|
||||
const assert = require('assert');
|
||||
const SimpleStorage = require('Embark/contracts/SimpleStorage');
|
||||
const EmbarkJS = require('Embark/EmbarkJS');
|
||||
const SimpleStorage = artifacts.require('SimpleStorage');
|
||||
const EmbarkJS = artifacts.require('EmbarkJS');
|
||||
|
||||
config({
|
||||
namesystem: {
|
||||
|
@ -58,4 +58,3 @@ describe("EmbarkJS functions", function() {
|
|||
assert.strictEqual(parseInt(result, 10), 100);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*global contract, config, it, assert, mineAtTimestamp*/
|
||||
const Expiration = require('Embark/contracts/Expiration');
|
||||
/*global artifacts, contract, config, it, assert, mineAtTimestamp*/
|
||||
const Expiration = artifacts.require('Expiration');
|
||||
const now = Math.floor(new Date().getTime()/1000.0); // Get unix epoch. The getTime method returns the time in milliseconds.
|
||||
|
||||
config({
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*global contract, config, it*/
|
||||
/*global artifacts, contract, config, it*/
|
||||
const assert = require('assert');
|
||||
const AnotherStorage = require('Embark/contracts/AnotherStorage');
|
||||
const AnotherStorage = artifacts.require('AnotherStorage');
|
||||
|
||||
// FIXME this doesn't work and no idea how it ever worked because ERC20 is not defined anywhere
|
||||
// config({
|
||||
|
@ -22,4 +22,3 @@ contract("AnotherStorageWithInterface", function() {
|
|||
assert.strictEqual(result.toString(), '0x0000000000000000000000000000000000000000');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*global contract, config, it*/
|
||||
/*global artifacts, contract, config, it*/
|
||||
const assert = require('assert');
|
||||
const Test2 = require('Embark/contracts/Test2');
|
||||
const Test2 = artifacts.require('Test2');
|
||||
|
||||
config({
|
||||
contracts: {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
/*global describe, it, config*/
|
||||
/*global artifacts, describe, it, config*/
|
||||
const assert = require('assert');
|
||||
const MyToken = require('Embark/contracts/MyToken');
|
||||
const MyToken2 = require('Embark/contracts/MyToken2');
|
||||
const EmbarkJS = require('Embark/EmbarkJS');
|
||||
const MyToken = artifacts.require('MyToken');
|
||||
const MyToken2 = artifacts.require('MyToken2');
|
||||
const EmbarkJS = artifacts.require('EmbarkJS');
|
||||
|
||||
let accounts;
|
||||
|
||||
|
@ -55,4 +55,3 @@ describe("ENS functions", function() {
|
|||
assert.strictEqual(myToken2Name, 'MyToken2.embark.eth');
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*global contract, config, it*/
|
||||
/*global artifacts, contract, config, it*/
|
||||
const assert = require('assert');
|
||||
const PluginStorage = require('Embark/contracts/PluginStorage');
|
||||
const SimpleStorage = require('Embark/contracts/SimpleStorage');
|
||||
const PluginStorage = artifacts.require('PluginStorage');
|
||||
const SimpleStorage = artifacts.require('SimpleStorage');
|
||||
|
||||
config({
|
||||
contracts: {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*global contract, it, assert, before, web3*/
|
||||
const SimpleStorage = require('Embark/contracts/SimpleStorage');
|
||||
const {Utils} = require('Embark/EmbarkJS');
|
||||
/*global artifacts, contract, it, assert, before, web3*/
|
||||
const SimpleStorage = artifacts.require('SimpleStorage');
|
||||
const {Utils} = artifacts.require('EmbarkJS');
|
||||
|
||||
contract("SimpleStorage Deploy", function () {
|
||||
let simpleStorageInstance;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*global contract, config, it, assert, web3*/
|
||||
const SimpleStorage = require('Embark/contracts/SimpleStorage');
|
||||
const SimpleStorage = artifacts.require('SimpleStorage');
|
||||
let accounts;
|
||||
const {Utils} = require('Embark/EmbarkJS');
|
||||
const {Utils} = artifacts.require('EmbarkJS');
|
||||
|
||||
config({
|
||||
contracts: {
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
/*global describe, config, it, web3*/
|
||||
/*global artifacts, describe, config, it, web3*/
|
||||
const assert = require('assert');
|
||||
const Token = require('Embark/contracts/Token');
|
||||
const MyToken = require('Embark/contracts/MyToken');
|
||||
const MyToken2 = require('Embark/contracts/MyToken2');
|
||||
const AlreadyDeployedToken = require('Embark/contracts/AlreadyDeployedToken');
|
||||
const Test = require('Embark/contracts/Test');
|
||||
const SomeContract = require('Embark/contracts/SomeContract');
|
||||
const Token = artifacts.require('Token');
|
||||
const MyToken = artifacts.require('MyToken');
|
||||
const MyToken2 = artifacts.require('MyToken2');
|
||||
const AlreadyDeployedToken = artifacts.require('AlreadyDeployedToken');
|
||||
const Test = artifacts.require('Test');
|
||||
const SomeContract = artifacts.require('SomeContract');
|
||||
|
||||
config({
|
||||
namesystem: {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
/*global contract, config, it*/
|
||||
/*global artifacts, contract, config, it*/
|
||||
const assert = require('assert');
|
||||
const AnotherStorage = require('Embark/contracts/AnotherStorage');
|
||||
const SimpleStorage = require('Embark/contracts/SimpleStorage');
|
||||
const AnotherStorage = artifacts.require('AnotherStorage');
|
||||
const SimpleStorage = artifacts.require('SimpleStorage');
|
||||
let accounts;
|
||||
|
||||
config({
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
/*global contract, config, it*/
|
||||
/*global artifacts, contract, config, it*/
|
||||
const assert = require('assert');
|
||||
const SomeContract = require('Embark/contracts/SomeContract');
|
||||
const SimpleStorage = require('Embark/contracts/SimpleStorage');
|
||||
const MyToken2 = require('Embark/contracts/MyToken2');
|
||||
const SomeContract = artifacts.require('SomeContract');
|
||||
const SimpleStorage = artifacts.require('SimpleStorage');
|
||||
const MyToken2 = artifacts.require('MyToken2');
|
||||
|
||||
config({
|
||||
contracts: {
|
||||
|
@ -42,4 +42,3 @@ contract("SomeContract", function() {
|
|||
});
|
||||
|
||||
});
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*global contract, config, it*/
|
||||
/*global artifacts, contract, config, it*/
|
||||
const assert = require('assert');
|
||||
const Test2 = require('Embark/contracts/Test2');
|
||||
const Test2 = artifacts.require('Test2');
|
||||
|
||||
config({
|
||||
contracts: {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*global contract, config, it*/
|
||||
/*global artifacts, contract, config, it*/
|
||||
const assert = require('assert');
|
||||
const SimpleStorage = require('Embark/contracts/SimpleStorage');
|
||||
const SimpleStorage = artifacts.require('SimpleStorage');
|
||||
|
||||
config({
|
||||
contracts: {
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
/*global describe, config, it*/
|
||||
/*global artifacts, describe, config, it*/
|
||||
const assert = require('assert');
|
||||
const Token = require('Embark/contracts/Token');
|
||||
const MyToken = require('Embark/contracts/MyToken');
|
||||
const MyToken2 = require('Embark/contracts/MyToken2');
|
||||
const AlreadyDeployedToken = require('Embark/contracts/AlreadyDeployedToken');
|
||||
const Test = require('Embark/contracts/Test');
|
||||
const Token = artifacts.require('Token');
|
||||
const MyToken = artifacts.require('MyToken');
|
||||
const MyToken2 = artifacts.require('MyToken2');
|
||||
const AlreadyDeployedToken = artifacts.require('AlreadyDeployedToken');
|
||||
const Test = artifacts.require('Test');
|
||||
|
||||
config({
|
||||
contracts: {
|
||||
|
|
|
@ -50,6 +50,7 @@
|
|||
"@babel/runtime-corejs3": "7.7.4",
|
||||
"@types/async": "3.0.3",
|
||||
"async": "3.1.0",
|
||||
"colors": "1.4.0",
|
||||
"core-js": "3.4.3",
|
||||
"embark-i18n": "^5.1.1",
|
||||
"embark-utils": "^5.2.0-nightly.1",
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import 'colors';
|
||||
import {__} from 'embark-i18n';
|
||||
|
||||
const async = require('async');
|
||||
|
@ -31,6 +32,31 @@ class MochaTestRunner {
|
|||
);
|
||||
}
|
||||
|
||||
static originalRequire = require('module').prototype.require;
|
||||
|
||||
static requireArtifact(artifactName, compiledContracts) {
|
||||
if (artifactName === 'EmbarkJS') return EmbarkJS;
|
||||
|
||||
const instance = compiledContracts[artifactName];
|
||||
|
||||
if (!instance) {
|
||||
compiledContracts[artifactName] = {};
|
||||
}
|
||||
|
||||
if (!compiledContracts[artifactName].abiDefinition) {
|
||||
return compiledContracts[artifactName];
|
||||
}
|
||||
|
||||
try {
|
||||
return Object.setPrototypeOf(
|
||||
instance,
|
||||
EmbarkJS.Blockchain.Contract(instance)
|
||||
);
|
||||
} catch (e) {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
addFile(path) {
|
||||
if (!this.match(path)) {
|
||||
throw new Error(`invalid JavaScript test path: ${path}`);
|
||||
|
@ -49,7 +75,6 @@ class MochaTestRunner {
|
|||
this.options = options;
|
||||
|
||||
const Module = require("module");
|
||||
const originalRequire = require("module").prototype.require;
|
||||
|
||||
let accounts = [];
|
||||
let compiledContracts = {};
|
||||
|
@ -186,29 +211,29 @@ class MochaTestRunner {
|
|||
return seriesCb(null, 0);
|
||||
}
|
||||
|
||||
let testRunner = this;
|
||||
|
||||
Module.prototype.require = function(req) {
|
||||
if (["Embark/EmbarkJS", "EmbarkJS"].includes(req)) {
|
||||
testRunner.logger.warn(
|
||||
`${__('WARNING!')} ${__('Use')} ${`artifacts.require('EmbarkJS')`.cyan}, ${__('the syntax').yellow} ${`require('${req}')`.cyan} ${__('has been deprecated and will be removed in future versions').yellow}`
|
||||
);
|
||||
return MochaTestRunner.requireArtifact("EmbarkJS");
|
||||
}
|
||||
|
||||
const prefix = "Embark/contracts/";
|
||||
if (req.startsWith(prefix)) {
|
||||
const contractClass = req.replace(prefix, "");
|
||||
const instance = compiledContracts[contractClass];
|
||||
|
||||
if (!instance) {
|
||||
compiledContracts[contractClass] = {};
|
||||
}
|
||||
if (!compiledContracts[contractClass].abiDefinition) {
|
||||
return compiledContracts[contractClass];
|
||||
}
|
||||
try {
|
||||
return Object.setPrototypeOf(instance, EmbarkJS.Blockchain.Contract(instance));
|
||||
} catch (e) {
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
if (req === "Embark/EmbarkJS") {
|
||||
return EmbarkJS;
|
||||
const artifactName = req.replace(prefix, "");
|
||||
testRunner.logger.warn(
|
||||
`${__('WARNING!')} ${__('Use')} ${`artifacts.require('${artifactName}')`.cyan}, ${__('the syntax').yellow} ${`require('${req}')`.cyan} ${__('has been deprecated and will be removed in future versions').yellow}`
|
||||
);
|
||||
return MochaTestRunner.requireArtifact(
|
||||
artifactName,
|
||||
compiledContracts
|
||||
);
|
||||
}
|
||||
|
||||
return originalRequire.apply(this, arguments);
|
||||
return MochaTestRunner.originalRequire.call(this, req);
|
||||
};
|
||||
|
||||
const mocha = new Mocha();
|
||||
|
@ -223,11 +248,18 @@ class MochaTestRunner {
|
|||
global.config = config;
|
||||
});
|
||||
|
||||
global.artifacts = {
|
||||
require: (artifactName) => MochaTestRunner.requireArtifact(
|
||||
artifactName,
|
||||
compiledContracts
|
||||
)
|
||||
};
|
||||
|
||||
mocha.suite.timeout(TEST_TIMEOUT);
|
||||
mocha.addFile(file);
|
||||
|
||||
mocha.run((failures) => {
|
||||
Module.prototype.require = originalRequire;
|
||||
Module.prototype.require = MochaTestRunner.originalRequire;
|
||||
seriesCb(null, failures);
|
||||
});
|
||||
});
|
||||
|
@ -238,7 +270,7 @@ class MochaTestRunner {
|
|||
}
|
||||
], (err) => {
|
||||
this.embark.config.plugins.runActionsForEvent('tests:finished', () => {
|
||||
Module.prototype.require = originalRequire;
|
||||
Module.prototype.require = MochaTestRunner.originalRequire;
|
||||
cb(err);
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue