Skip to content

Commit

Permalink
Merge pull request #21 from EvilOatmeal/fix-using-mocha-api
Browse files Browse the repository at this point in the history
Fix using the Mocha API
  • Loading branch information
iensu authored Mar 27, 2018
2 parents 8955c5c + 21d4a3b commit 8c921cf
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 11 deletions.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,43 @@ npm install --save-dev mocha-cakes-2

## Usage

You need to specify `mocha-cakes-2` as a mocha integration by adding the option `--ui mocha-cakes-2` to your `mocha` command:
### Enable the mocha-cakes-2 integration

To enable the Mocha integration you need to specify `mocha-cakes-2` in the `ui` option.

#### CLI

Either use the command line argument:

``` javascript
mocha --ui mocha-cakes-2 path/to/my/tests
```

You can also specify it in your [`mocha.opts`](https://mochajs.org/#mochaopts) file:
Or set it in your [`mocha.opts`](https://mochajs.org/#mochaopts) file:

``` javascript
--ui mocha-cakes-2
```

#### API

Either pass it in the options as you construct Mocha:

``` javascript
var mocha = new Mocha({
ui: 'mocha-cakes-2'
});
```

Or set it after you've constructed Mocha:

``` javascript
var mocha = new Mocha();
mocha.ui('mocha-cakes-2')
```

### Test structure

```javascript
require('chai').should();

Expand Down Expand Up @@ -226,6 +251,16 @@ Feature('Another feature', () => {
// ...
```

## Development

### Testing the CLI and API interfaces

If you use Mocha directly to run the tests you can set the `MOCHA_INTERFACE` environment variable to either `cli` or `api` to choose which Mocha interface to run the tests with: `MOCHA_INTERFACE=api mocha test/feature/tests.js`.

`MOCHA_INTERFACE` will default to `cli` if no value is set.

When you run `npm run test:cli` or `npm run test:api` (or `npm test` to run them both), `MOCHA_INTERFACE` is set automatically to the appropriate value.

## Acknowledgements

Mocha Cakes 2 is heavily influenced by **quangv**'s [mocha-cakes](https://github.com/quangv/mocha-cakes/).
1 change: 1 addition & 0 deletions mocha-cakes.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function mochaCakes(suite) {
context.And = testTypeCreator('And');
context.But = testTypeCreator('But');
context.it = testTypeCreator('');
context.xit = context.it.skip;

// lower-case aliases
context.scenario = context.Scenario;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"main": "mocha-cakes.js",
"typings": "./mocha-cakes.d.ts",
"scripts": {
"test": "mocha ./test/**/*tests.js",
"test": "npm run test:cli && npm run test:api",
"test:cli": "MOCHA_INTERFACE=cli mocha ./test/**/*tests.js",
"test:api": "MOCHA_INTERFACE=api node ./test/run-api.js",
"test:watch": "mocha ./test/**/*tests.js --watch -R dot"
},
"repository": {
Expand Down
87 changes: 79 additions & 8 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,91 @@
'use strict';

var Mocha = require('mocha');
var Promise = require('bluebird');
var execFile = Promise.promisify(require('child_process').execFile);
var path = require('path');

var testEnv = Object.assign({}, process.env, { NODE_PATH: __dirname });

var defaultOpts = {
reporter: 'spec'
};

var formatRegexp = /%[ds]/

var interfaces = {
cli: function(file, opts) {
opts = Object.assign({}, defaultOpts, opts);
var args = [
'--require', path.join(__dirname, '../mocha-cakes.js'),
'--ui', 'mocha-cakes',
'--reporter', opts.reporter,
path.join(__dirname, file)
];
return execFile('mocha', args, { env: testEnv });
},

api: function(file, opts) {
return new Promise(function(resolve) {
opts = Object.assign({}, defaultOpts, opts);

// The set up from requiring mocha-cakes and running chai.should() in `run-api.js`
// is inherited here so we don't need to set them up for each file we run.

var mocha = new Mocha({
ui: 'mocha-cakes-2',
reporter: opts.reporter,
// Since we're intercepting the console messages before they're formatted
// we would get the color strings in the output, but that doesn't happen
// in the CLI output since the colors are formatted and won't appear in the
// strings, so we disable colors here so we don't have to strip them from
// the output, and we can use the same test assertions with both interfaces.
useColors: false
});

mocha.addFile(path.join(__dirname, file));

// Override console.log() so we can get the output from the reporter.
var logs = [];
var originalLog = console.log
console.log = function() {
var args = Array.prototype.slice.call(arguments);
if (args.length < 2) {
logs.push(args[0] || '');
} else {
var str = args.reduce(function(str, arg) {
if (formatRegexp.test(str)) {
return str.replace(formatRegexp, arg);
}
return str + ' ' + arg;
});
logs.push(str);
}
}

// Mocha.run() will store the 'useColors' option internally, outside of the
// options we've set for this instance, so if the Mocha instance that's
// running this has a different 'useColors' value it won't be restored.
var previousUseColors = Mocha.reporters.Base.useColors;

mocha.run(function() {
console.log = originalLog;
Mocha.reporters.Base.useColors = previousUseColors;
resolve(logs.join('\n'));
});
});
}
}

function execTestFile(file, opts) {
opts = Object.assign({ reporter: 'spec' }, opts);
var args = [
'--require', path.join(__dirname, '../mocha-cakes.js'),
'--ui', 'mocha-cakes',
'--reporter', opts.reporter,
path.join(__dirname, file)
];
return execFile('mocha', args, { env: testEnv });
var interfaceName = process.env.MOCHA_INTERFACE || 'cli';
var func = interfaces[interfaceName];
if (!func) {
var actual = typeof interfaceName !== 'undefined' ? JSON.stringify(interfaceName) : 'undefined';
var valid = Object.keys(interfaces).join(', ');
throw new Error('The MOCHA_INTERFACE environment variable is set to ' + actual + ', valid values are: ' + valid);
}
return func(file, opts);
}

module.exports = {
Expand Down
21 changes: 21 additions & 0 deletions test/run-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var Mocha = require('mocha');
var chai = require('chai');

require('../mocha-cakes');

chai.should();

var mocha = new Mocha({
ui: 'mocha-cakes-2',
reporter: 'spec'
});

mocha.addFile('test/feature/tests.js');

mocha.run(function(failureCount) {
process.on("exit", function() {
process.exit(failureCount);
});
});

0 comments on commit 8c921cf

Please sign in to comment.