Skip to content

Commit

Permalink
some es5 to es6, coverage improvement and fix script commands (#38)
Browse files Browse the repository at this point in the history
* some es5 to es6, coverage improvement and fix script commands

* remove legacy code and fix readme info

* revert handlers export

* don't run ghost test

* set spacejam as devdep

* fix script commands with new spacejam syntax
  • Loading branch information
cgalvarez authored and serut committed Sep 21, 2016
1 parent 9bfcda0 commit 02e02af
Show file tree
Hide file tree
Showing 22 changed files with 257 additions and 122 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ before_install:
- if [ ! -e $HOME/.meteor/meteor ]; then curl https://install.meteor.com | sh; fi

# Install dependencies
- npm install -g https://github.com/serut/spacejam/tarball/windows-suppport-rc4
- npm install -g eslint coveralls codecov.io codacy-coverage
- meteor npm install

Expand Down
74 changes: 34 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ First, create your configuration file `.coverage.json` to specify what you want



Then setup how you want to run your app. Actually, to let meteor-coverage start processing, you need to enable it, because it's a probe disabled by default and you need to provide the absolute path to your source folder.
Then setup how you want to run your app. Actually, to let meteor-coverage start processing, you need to enable it, because it's a probe disabled by default and you need to provide the absolute path to your source folder.
- [use spacejam](#setup-spacejam) as a test runner, perfect for CI or prepublish hooks. Spacejam setup everything for you throw the `--coverage` option.
OR
- [use a meteor setting file](#meteor---settings-file) to store the coverage configuration, then run your app with `--settings settings.coverage.json`
Expand All @@ -53,28 +53,28 @@ Then setup how you want to run your app. Actually, to let meteor-coverage start
meteor-coverage ensures the output folder exist on boot, which is by default `./.coverage` inside your app.
For Typescript or any simular language users, you need to remap your code (released soon).

## Usage
## Usage

Run the following command :
Run the following command :

meteor [... see supported options below] --settings settings.coverage.json

Then open [localhost:3000/coverage](localhost:3000/coverage) in your browser. A missing feature would be to save your client coverage with a widget. Instead, you need to enter this javascript in your browser console :

Meteor.sendCoverage(function(stats,err) {console.log(stats,err);});
# Reopen localhost:3000/coverage to see that client coverage have been saved on server

# Creates an html export inside coverage_app_folder/output_folder/index.html
Meteor.exportCoverage("html", function(err) {console.log(err)})

or you can use spacejam-mocha

spacejam-mocha [... see supported options below] --coverage [out_html|out_lcovonly|out_text_summary|out_json_report|out_json_summary|in_coverage|out_coverage|out_remap]
[--settings settings.coverage.json]

That's it !

### Run options
### Run options

While spacejam requires mocha, meteor-coverage is agnostic on this point. If the `COVERAGE` flag is true, it instruments your app and provides an HTTP API to generate reports.

Expand All @@ -96,8 +96,8 @@ If you have any trouble, refer to this example of Meteor application [meteor-cov

Add the following dependencies in your `package.json`:

meteor npm init # If the package.json file does not exist
meteor npm i --save-dev https://github.com/serut/spacejam/tarball/windows-suppport-rc4
meteor npm init # If the package.json file does not exist
meteor npm i --save-dev https://github.com/serut/spacejam/tarball/windows-suppport-rc4

Add what you need to run your app inside your `package.json`:

Expand All @@ -107,28 +107,28 @@ Add what you need to run your app inside your `package.json`:
"test:app-unit-coverage": "spacejam-mocha test --coverage out_coverage",
"test:app-full-coverage": "spacejam-mocha test --full-app --coverage 'in_coverage|out_lcovonly'",
"test:packages-coverage": "spacejam-mocha test-packages --coverage out_lcovonly"

[PCKGS]
"test": "spacejam-mocha test-packages ./ --coverage out_lcovonly ",
"test:watch": "meteor npm run lint:fix & meteor npm run test:packages-coverage-watch",
"test:packages-coverage-watch": "meteor-mocha test-packages --settings settings.coverage.json",

"lint:fix": "eslint --fix ."
}



If you want to, you can use this syntax, the following two commands are equivalent, but the second one is shorter and thus less typing error-prone

spacejam [..] --driver-package practicalmeteor:mocha-console-runner
spacejam [..] --driver-package practicalmeteor:mocha-console-runner
spacejam-mocha [..]

Same for meteor:


meteor --driver-package practicalmeteor:mocha [...]
meteor-mocha


You may notice that you can't execute `spacejam-mocha` on your terminal, that's because you installed it with the flag `--save-dev`.
The executable is in fact located in the folder `./node_modules/.bin/` and that's not mentioned inside the `package.json` because the `.bin` folder [is added to the PATH before node runs smth](https://docs.npmjs.com/misc/scripts#path)
Expand All @@ -145,22 +145,22 @@ Now, you can run your test (here is an extract of a [circle.yml](https://github.
- meteor npm run test-coverage-app-full
# Package test using mocha
- meteor npm run test-coverage-packages-mocha

### Coveralls

Install

meteor npm i --save-dev coveralls
Add this after tests execution:
meteor npm i --save-dev coveralls

Add this after tests execution:

# Send coverage report
cat .coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js || true # ignore coveralls error
### Codecov

### Codecov

meteor npm i --save-dev codecov.io

- cat .coverage/lcov.info | ./node_modules/codecov.io/bin/codecov.io.js || true # ignore codecov error

## spacejam --coverage possibilities
Expand Down Expand Up @@ -254,7 +254,7 @@ Exemple:
"output": "./.coverage"
}
```
Details :
Details :

- Allows / Disallow is coded with the following order `include`, `exclude.general`, `exclude.(client|server)`, it is used before both instrumentation and before coverage report creation.
- The glob syntax can be found [here](http://www.linuxjournal.com/content/bash-extended-globbing).
Expand All @@ -264,9 +264,9 @@ Details :
### Flow router issue

If you are using flow-router, there are [an issue](https://github.com/kadirahq/flow-router/pull/615) that prevents tests to succeed.
This workaround replaces flow-router with a patched version which ignores that error:
This workaround replaces flow-router with a patched version which ignores that error:
```bash
cd packages
cd packages
git submodule add https://github.com/serut/flow-router
```

Expand Down Expand Up @@ -305,12 +305,7 @@ You can find [here](https://github.com/practicalmeteor/spacejam/compare/windows-

If you are using a language that compiles to JavaScript (there are [lots of them](https://github.com/jashkenas/coffeescript/wiki/list-of-languages-that-compile-to-js)), you may want to see your coverage reports referred to the original source files (prior to compilation).

To remap your source files, you have to provide the report type `out_remap` explicitly when using `spacejam`:

```shell
spacejam test-packages --driver-package practicalmeteor:mocha --coverage out_remap
spacejam-mocha --coverage out_remap
```
To remap your source files, you have to provide the report type `out_remap` explicitly when using `spacejam`: `spacejam-mocha --coverage out_remap`

You'll get your remapped coverage reports at `./.coverage/.remap` (or `custom_output/.remap` if you're customized the output folder through the file `.coverage.json`).

Expand Down Expand Up @@ -361,8 +356,8 @@ Why? When a browser opens the client side of your application, this package inte
- type: the type of report you want to create inside your `COVERAGE_APP_FOLDER`

- Default: `coverage`, used to dump the coverage object in a file because when there are several types of test, we want to merge results, and the server reloads between each one.
- Allowed values: `cobertura`, `html`, `json`, `json-summary`, `lcov`, `teamcity`, `text`, `text-lcov`, `text-summary`, `lcovonly`, `coverage`, `remap`
- **Not working values:** `clover`, `cobertura`, `lcov`, `text`, `text-lcov`, PR welcome
- Allowed values: `coverage`, `html`, `json`, `json-summary`, `lcovonly`, `remap`, `text-summary`
- **Not working values:** `clover`, `cobertura`, `lcov`, `teamcity`, `text`, `text-lcov`, PR welcome
- Except for `coverage`, the file generation is handled by [istanbuljs/istanbul-reports](https://github.com/istanbuljs/istanbul-reports)

```js
Expand All @@ -383,9 +378,9 @@ Anyone is welcome to contribute.

# You should fork this repo first
git clone https://github.com/serut/meteor-coverage
# I have a preference to always execute npm action
# throw the integrated meteor npm (npm --v !== meteor npm --v)
meteor npm install
# I have a preference to always execute npm action
# throw the integrated meteor npm (npm --v !== meteor npm --v)
meteor npm install
# Edit the app_folder key to match your app folder, don't forget the ending slash
nano settings.coverage.json
# Then run mocha watch tests
Expand All @@ -401,4 +396,3 @@ This package would not exist without the amazing work of:
- All contributors of [istanbul-api](https://github.com/istanbuljs/istanbul-api) and [istanbul-middleware](https://github.com/gotwarlost/istanbul-middleware) projects.

All of them were very helpful in the development of this package. Merci !

1 change: 0 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ machine:
dependencies:
override:
- curl https://install.meteor.com | /bin/sh
- npm install -g https://github.com/serut/spacejam/tarball/windows-suppport-rc4
- meteor npm install
checkout:
post:
Expand Down
59 changes: 56 additions & 3 deletions client/methods.e2e.tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {assert} from 'meteor/practicalmeteor:chai';
import {chai, assert, expect} from 'meteor/practicalmeteor:chai';
import Meteor from 'meteor/lmieulet:meteor-coverage';
var _should = chai.should();

var testCoverage = function(done, operation, reportType) {
this.timeout(0);
Expand Down Expand Up @@ -34,18 +35,70 @@ describe('meteor-coverage', function () {
done(e);
}
});
/*it('handle errors on sending client coverage', function (done) {
// Manual stub
const _jQueryAjaxBackup = $.ajax;
$.ajax = function(options) { options.error(); };
try {
Meteor.sendCoverage(
function (stats, err) {
assert.isTrue(stats.TOTAL > 0, 'no client coverage');
assert.isTrue(stats.SUCCESS > 0, 'none of the client coverage have been saved');
assert.isTrue(stats.FAILED > 0, 'an export failed');
done();
}
);
} catch (e) {
console.error(e, e.stack);
done(e);
}
// Undo stub
$.ajax = _jQueryAjaxBackup;
});*/

// test implemented report types
let coverage = {
export: ['coverage', 'lcovonly', 'json', 'json-summary', 'text-summary', 'html', 'remap'],
export: ['coverage', 'html', 'json', 'json-summary', 'lcovonly', 'remap', 'text-summary'],
import: ['coverage']
};
for (let operation in coverage) {
if (coverage.hasOwnProperty(operation)) {
coverage[operation].forEach(function (reportType) {
it(`${operation} ${reportType}`, function (done) {
it(`${operation} [${reportType}]`, function (done) {
testCoverage.call(this, done, operation, reportType); // pass mocha context
});
});
}
}

// test non-implemented report types
let reportTypes = {
disallowed: ['', 'none', 'random-invented'],
pending: ['clover', 'cobertura', 'lcov', 'text', 'text-lcov']
};
for (let group in reportTypes) {
if (reportTypes.hasOwnProperty(group)) {
reportTypes[group].forEach(function (reportType) {
it(`export [${reportType}] should fail`, function (done) {
this.timeout(0);
try {
Meteor.exportCoverage(reportType, function (err) {
if (group === 'disallowed') {
expect(err).to.be.undefined;
} else {
err.should.be.a('string');
assert.isTrue(err.startsWith('Error: '));
}
done();
});
} catch (e) {
console.error(e, e.stack);
done(e);
}
});
});
}
}
});
10 changes: 7 additions & 3 deletions client/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ Meteor.sendCoverage = function (callback) {
successCallback = function () {
Meteor.increaseSuccess();
var stats = Meteor.getStats();
/* istanbul ignore else */
if (stats.SUCCESS + stats.FAILED === stats.TOTAL) {
callback(stats);
}
},
errorCallback = function() {
Meteor.increaseFailures();
var stats = Meteor.getStats();
/* istanbul ignore else */
if (stats.SUCCESS + stats.FAILED === stats.TOTAL) {
callback(stats, arguments);
}
Expand All @@ -56,7 +58,7 @@ Meteor.sendCoverage = function (callback) {

// Send each property alone
for (var property in globalCoverage) {

/* istanbul ignore else */
if (globalCoverage.hasOwnProperty(property)) {
Meteor.increaseTotal();

Expand All @@ -76,14 +78,15 @@ Meteor.sendCoverage = function (callback) {
* Usage: Meteor.exportCoverage(null, function(err) {console.log(err)})
*/
Meteor.exportCoverage = function (type, callback) {

/* istanbul ignore next: ternary operator */
var url = type ? '/coverage/export/'+type : '/coverage/export';
$.ajax({
method: 'GET',
url: url,
success: function(data) {
try {
let result = JSON.parse(data);
/* istanbul ignore else */
if (result.type !== 'success') {
throw new Error('Error: '+JSON.stringify(arguments)+'. An unexpected error occurred while trying to export coverage data');
}
Expand All @@ -94,7 +97,7 @@ Meteor.exportCoverage = function (type, callback) {
}
},
error: function() {
callback('Error: '+JSON.stringify(arguments)+'. An server error occurred while trying to export coverage data');
callback('Error: '+JSON.stringify(arguments)+'. A server error occurred while trying to export coverage data');
}
});
};
Expand All @@ -109,6 +112,7 @@ Meteor.importCoverage = function (callback) {
success: function(data) {
try {
let result = JSON.parse(data);
/* istanbul ignore else */
if (result.type !== 'success') {
throw new Error('Error: '+JSON.stringify(arguments)+'. An unexpected error occurred while trying to import coverage data');
}
Expand Down
2 changes: 1 addition & 1 deletion client/methods.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('meteor-coverage', function (done) {
assert.isDefined(Meteor.sendCoverage);
});

it('calls the callback function if there are no coverage', function () {
it('calls the callback function if there is no coverage', function () {
const callback = sandbox.spy();
sandbox.stub(Meteor, 'getCoverageObject');

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
"eslint-plugin-import": "^1.13.0",
"eslint-plugin-mocha": "^4.3.0",
"eslint-plugin-promise": "^2.0.1",
"eslint-plugin-standard": "^2.0.0"
"eslint-plugin-standard": "^2.0.0",
"spacejam": "https://github.com/serut/spacejam/tarball/windows-suppport-rc4"
},
"scripts": {
"test": "spacejam test-packages ./ --coverage out_lcovonly --driver-package practicalmeteor:mocha-console-runner",
"test:watch": "meteor-mocha test-packages --settings settings.coverage.json",
"test": "spacejam-mocha test-packages ./ --coverage out_lcovonly",
"test:watch": "meteor-mocha test-packages ./ --settings settings.coverage.json",
"start": "meteor npm run lint:fix & meteor npm run test:watch",
"lint": "eslint .",
"lint:fix": "eslint --fix ."
Expand Down
1 change: 1 addition & 0 deletions server/boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default Boot = {
// Create reports output folder if not exists
let outputFolder = path.join(Conf.COVERAGE_APP_FOLDER, Conf.COVERAGE_EXPORT_FOLDER);
fs.access(outputFolder, fs.F_OK | fs.R_OK | fs.W_OK, (err) => {
/* istanbul ignore else */
if (err) {
try {
mkdirp(outputFolder);
Expand Down
4 changes: 4 additions & 0 deletions server/context/conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export const COVERAGE_EXPORT_FOLDER = configuration.output;
export const exclude = configuration.exclude;
export const include = configuration.include;
export const remapFormat = configuration.remapFormat;
export const reportTypes = {
allowed: ['clover', 'cobertura', 'coverage', 'html', 'json', 'json-summary', 'lcov', 'lcovonly', 'remap', 'teamcity', 'text', 'text-lcov', 'text-summary'],
pending: ['clover', 'cobertura', 'lcov', 'teamcity', 'text', 'text-lcov']
};

Log.info('Coverage configuration:');
Log.info('- IS_COVERAGE_ACTIVE=', IS_COVERAGE_ACTIVE);
Expand Down
Loading

0 comments on commit 02e02af

Please sign in to comment.