Skip to content

Commit

Permalink
clutch-assert/loader (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
gurpreetatwal authored Dec 7, 2016
1 parent 399cffe commit db5ed45
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 10 deletions.
46 changes: 46 additions & 0 deletions loader/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const fs = require('fs');
const path = require('path');

const DEFAULT_DIRECTORY = 'test';

function getDirectory(rc) {

if (!(typeof rc === 'object' && rc.directory)) {
return DEFAULT_DIRECTORY;
}

const dir = rc.directory;
const last = dir.substring(dir.length - 1);

// strip trailing slashes
if (last === '/' || last === '\\') {
return dir.substring(0, dir.length - 1);
}

return dir;
}

function checkDirectory(dir) {

dir = path.resolve(dir);
try {
fs.accessSync(dir, fs.F_OK);
} catch (e) { // eslint-disable-next-line max-len
e.message = `(clutch-assert/loader) Tried to instrument ${dir} but it does not exist.
Please specify the correct directory in .clutchrc as follows:
{
"directory": "test-unit"
}
`;
throw e;
}

}

function createPattern(dir) {
return dir + path.sep + '**' + path.sep + '*.js';
}

module.exports = {getDirectory, checkDirectory, createPattern};
34 changes: 34 additions & 0 deletions loader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const fs = require('fs');
const path = require('path');
const helpers = require('./helpers');
const espower = require('espower-loader');
const patterns = require('../lib/patterns');

const parent = path.dirname(module.parent.paths[0]);

var rc;
try {
rc = JSON.parse(fs.readFileSync(parent + path.sep + '.clutchrc'));
} catch (e) {

if (e.code !== 'ENOENT') {
throw e;
}

// no clutchrc found

}

const dir = helpers.getDirectory(rc);
helpers.checkDirectory(dir);
const pattern = helpers.createPattern(dir);

espower({
pattern,
cwd: process.cwd(),
espowerOptions: {
patterns: patterns.ENHANCED,
},
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "clutch-assert",
"version": "0.0.5",
"version": "0.1.0",
"description": "Combine Ava's assertion API with Power Assert",
"main": "index.js",
"license": "MIT",
Expand Down Expand Up @@ -39,6 +39,7 @@
},
"dependencies": {
"empower": "^1.2.1",
"espower-loader": "^1.2.0",
"is-observable": "^0.2.0",
"is-promise": "^2.1.0",
"not-so-shallow": "^0.1.4",
Expand Down
54 changes: 45 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

This project is essentially a combination of ideas from two other projects
[ava](https://github.com/avajs/ava) and
[power-assert](github.com/power-assert-js/power-assert).
[power-assert](http://github.com/power-assert-js/power-assert).

It combines the api of ava's assertions with the power of power-assert and also
provides a handy [`.keys`] assertion which can be useful if switching from chai.
provides a handy `.keys` assertion which can be useful if switching from chai.

## Why/When should I use this?

Expand All @@ -19,22 +19,26 @@ testing. Using clutch assert will allow you to incrementally convert all of your
assertions to ava style assertions and then once you're ready to switch just
replace the word assert with ava's assertion mixin variable.

## Quick Start
**Note:** ava does not have the `.keys` assertion

Install clutch-assert and intelli-espower-loader
## Quick Start

Install clutch-assert
```
$ npm install --save clutch-assert intelli-espower-loader
$ npm install --save clutch-assert
```

Instruct your test runner to require intelli-espower-loader before running tests
Instruct your test runner to require the clutch-assert loader before running tests.
**You will not get enhanced assertion messages if you neglect to require the loader**

Example using [mocha](https://visionmedia.github.io/mocha/)
```
$(npm bin)/mocha --require intelli-espower-loader path/to/test/mocha_node.js
$(npm bin)/mocha --require clutch-assert/loader path/to/test/mocha_node.js
```
For more detailed instructions and use cases please refer to the
[power-assert documentation](https://github.com/power-assert-js/power-assert)

By default the loader will instrument all files in a directory named `test`, if
your tests are located elsewhere you must provide that path to the loader as
detailed under [loader configuration](https://github.com/smartcar/clutch-assert/#loader-configuration).

## Usage

Expand All @@ -48,6 +52,38 @@ assert.is(1 + 1, 2);
dependencies work power-assert won't work correctly if the variable is named
something else.

### Loader Configuration
By default the loader instruments all files under the `test` directory, this can be changed
by placing a `.clutchrc` in the root of your project which should be a json file with a `directory`
top-level key.

*Example*
```json
{
"directory": "test/unit"
}
```

### Using Other Loaders
The [power-assert loaders](https://github.com/power-assert-js/power-assert#be-sure-to-transform-test-code)
do not have support for the `.keys` assertion by default. If you wish to use
that assertion you must import the patterns from clutch assert and configure
the loader to use those patterns.

Here is an example using `espower-loader`
```js
const espower = require('espower-loader');
const patterns = require('clutch-assert/lib/patterns');

espower({
cwd: process.cwd(),
pattern: 'test/**/*.js',
espowerOptions: {
patterns: pattern.ENHANCED,
},
});
```

## API

### `.pass([message])`
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/to_be_instrumented.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

// espower will replace the following with
// const assert = require('power-assert');

const assert = require('assert');
const one = 1;

assert.not(one, 1);
1 change: 1 addition & 0 deletions test/loader-error/.clutchrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
garbage
20 changes: 20 additions & 0 deletions test/loader-error/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

/* eslint-disable global-require */

const test = require('ava');

test.afterEach(function() {

delete require.cache[require.resolve('../../loader')];
});

test('does not swallow errors', function(t) {

const err = t.throws(function() {
require('../../loader');
}, SyntaxError);


t.true(err.message.includes('Unexpected token g'));
});
3 changes: 3 additions & 0 deletions test/loader/.clutchrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"directory":"test/fixtures"
}
37 changes: 37 additions & 0 deletions test/loader/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const test = require('ava');
const {sep} = require('path');
const helpers = require('../../loader/helpers');

test('getDirectory', t => {

t.is(helpers.getDirectory(), 'test');
t.is(helpers.getDirectory(), 'test');
t.is(helpers.getDirectory('other'), 'test');
t.is(helpers.getDirectory({directory: 'other'}), 'other');
t.is(helpers.getDirectory({directory: 'other\\'}), 'other');
t.is(helpers.getDirectory({directory: 'other/'}), 'other');

});

test('checkDirectory', function(t) {

t.notThrows(function() {
helpers.checkDirectory('lib');
});

const err = t.throws(function() {
helpers.checkDirectory('what');
});

t.true(err.message.includes('what'));

});


test('createPattern', function(t) {

t.is(helpers.createPattern('mytestdir'), `mytestdir${sep}**${sep}*.js`);

});
21 changes: 21 additions & 0 deletions test/loader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

/* eslint-disable global-require */

const test = require('ava');

test.afterEach(function() {
delete require.cache[require.resolve('../../loader')];
});

test('overall', function(t) {

require('../../loader');

const err = t.throws(function() {
require('../fixtures/to_be_instrumented');
});

t.is(err.message, "Cannot find module 'power-assert'");

});

0 comments on commit db5ed45

Please sign in to comment.