Michael Bradley, Jr ad01d1402c test: collect code coverage in the root of the monorepo
Implement scripts to collect coverage reports (JSON format) from all packages
in the monorepo that generate such reports. Reports are copied to
`<root>/.nyc_output/coverage-[pkg-dir-name].json`.

Implement scripts to generate a combined html report in `<root>/coverage`.

Adjust root `reset` and `clean` scripts to delete `<root>/.nyc_output` and
`<root>/coverage`.

Implement a script in `<root>/package.json` to generate a `text-lcov` report
and upload it to coveralls from CI builds. Remove coveralls from
`packages/embark`.

Supply `packages/embark` with an nyc configuration in its `package.json` and
have its `"test":` script generate both `json` and `html` reports.

Use nyc with `embarkjs`'s test suite: supply an nyc configuration in its
`package.json` and have its `"test":` script generate both `json` and `html`
reports.

Adjust `embarkjs`'s tests for more accurate coverage reporting.
2019-04-30 12:04:01 -04:00

112 lines
2.6 KiB
JavaScript

/* global Buffer global module require */
const async = require('async');
const http = require('http');
const net = require('net');
const Web3 = require('web3');
const startRPCMockServer = (options = {}, callback) => {
const opts = Object.assign({}, {
successful: true
}, options);
let port = 0;
let sock = net.createServer();
let state = { visited: false };
let server = http.createServer((req, res) => {
state.visited = true;
if(!opts.successful) {
res.statusCode = 500;
return res.end();
}
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
const request = JSON.parse(body);
const accountsResponse = JSON.stringify({
"jsonrpc": "2.0",
"id": request.id,
"result": [
"0x7c67d951b7338a96168f259a16b7ba25e7a30315"
]
});
res.writeHead(200, {
'Content-Length': Buffer.byteLength(accountsResponse),
'Content-Type': 'application/json'
});
res.end(accountsResponse);
});
});
async.waterfall([
cb => { sock.listen(0, cb); },
cb => { port = sock.address().port; cb(); },
cb => { sock.close(cb); },
cb => { server.listen(port, '127.0.0.1', () => cb()); }
], () => {
state.server = server;
state.connectionString = `http://localhost:${port}`;
callback(null, state);
});
};
const TestProvider = {};
TestProvider.init = function(_config) {
this.web3 = global.web3 || new Web3();
global.web3 = global.web3 || this.web3;
};
TestProvider.getInstance = function () {
return this.web3;
};
TestProvider.getAccounts = function () {
return this.web3.eth.getAccounts(...arguments);
};
TestProvider.getNewProvider = function (providerName, ...args) {
return new Web3.providers[providerName](...args);
};
TestProvider.setProvider = function (provider) {
return this.web3.setProvider(provider);
};
TestProvider.getCurrentProvider = function () {
return this.web3.currentProvider;
};
TestProvider.getDefaultAccount = function () {
return this.web3.eth.defaultAccount;
};
TestProvider.setDefaultAccount = function (account) {
this.web3.eth.defaultAccount = account;
};
TestProvider.newContract = function (options) {
return new this.web3.eth.Contract(options.abi, options.address);
};
TestProvider.send = function () {
return this.web3.eth.sendTransaction(...arguments);
};
TestProvider.toWei = function () {
return this.web3.toWei(...arguments);
};
TestProvider.getNetworkId = function () {
return this.web3.eth.net.getId();
};
module.exports = {
TestProvider,
startRPCMockServer
};