Skip to content
This repository has been archived by the owner on Apr 1, 2019. It is now read-only.

activate dirty-chai #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 27 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
# unit-test-toolbox
A convenient aggregation of quality npm modules to be used for writing unit tests. Just install this module and you get everything needed at once!

This modules includes `mocha`, `chai`, `sinon`, `sinon-chai`, `chai-as-promised`, `chai-subset`, `chai-moment`.
A convenient aggregation of quality npm modules to be used for writing unit tests.
Just install this module and you get everything needed at once!

**v4 important note, breaking change:**
* As we strive to write solid unit tests, v4 now includes and activates [`dirty-chai`](https://github.com/prodatakey/dirty-chai)
* dropped legacy node compatibility (<2) may still work but didn't try and won't maintain.


This modules bundles:
* `mocha`
* `chai`
* `dirty-chai`
* `sinon`
* `sinon-chai`
* `chai-as-promised`
* `chai-subset`
* `chai-moment`
It also exposes:
- a pre-made plumbing activating the `chai.expect` interface, chai `sinon-chai` and `chai-as-promised` plugins
- (for legacy nodes with npm <= 2) the `mocha` executable as a redirection to its integrated `mocha` executable
- a pre-made plumbing for:
- activating the `chai.expect` interface
- activating correctly the chai alteration `dirty-chai` **[READ THIS](https://github.com/prodatakey/dirty-chai#use-with-chai-as-promised)**
- activating correctly the chai plugins: `sinon-chai`, `chai-as-promised`, `chai-subset`, `chai-moment`
- a pre-made mocha options file

**In progress**:
- working on integrating sinon properly
Expand All @@ -16,7 +33,7 @@ Writing unit tests in JavaScript with mocha/chai requires assembling a bunch of

**:-1: WITHOUT @offirmo/unit-test-toolbox :-1: :hurtrealbad:**:
1. remembering all the libs needed
1. installing them: `npm install mocha chai sinon sinon-chai chai-as-promised`
1. installing them: `npm install mocha chai sinon sinon-chai chai-as-promised chai-subset chai-moment`
1. if using typescript: `npm install @types/mocha @types/chai @types/sinon @types/sinon-chai @types/chai-as-promised`
1. write an init file for activating `chai.expect` (what else ?), `sinon-chai` and `chai-as-promised`
1. write the npm task `"test": "mocha --opts mocha.opts path/to/init.js '<glob_to_my_tests/**/*spec.js>'"`
Expand All @@ -26,7 +43,7 @@ The proposed solution:

**:+1: WITH @offirmo/unit-test-toolbox :+1: :sunglasses:**:
1. install only one module `npm i -D @offirmo/unit-test-toolbox`
1. write the npm task `"test": "mocha --opts mocha.opts node_modules/@offirmo/unit-test-toolbox/mocha-chai-init-node.js '<glob_to_my_tests/**/*spec.js>'"`
1. copy this npm task `"test": "mocha --opts mocha.opts node_modules/@offirmo/unit-test-toolbox/mocha-chai-init-node.js '<glob_to_my_tests/**/*spec.js>'"`
1. keep @offirmo/unit-test-toolbox up-to-date


Expand All @@ -46,27 +63,6 @@ If you want to use the pre-written init file, reference it in your `test` task:
},
```

Suggested `mocha.opts`:
```
--reporter spec
--check-leaks
--es_staging
--full-trace
--harmony
```

### Legacy
For node 0.10 with npm 2, reference a special branch in `package.json`
```
"@offirmo/unit-test-toolbox": "git://github.com/offirmo/unit-test-toolbox.git#legacy-node_v0.10",
```
You can then use `mocha` as usual (transparently forwarded).

`sinon` should be imported through unit-test-toolbox:
```js
var sinon = require('@offirmo/unit-test-toolbox/node_modules/sinon')
```


## See also
* mocha
Expand All @@ -80,3 +76,6 @@ var sinon = require('@offirmo/unit-test-toolbox/node_modules/sinon')

## Contributing
Suggestions welcome.
TODO
* https://github.com/prodatakey/dirty-chai
* https://github.com/prodatakey/dirty-chai/issues/12
19 changes: 11 additions & 8 deletions mocha-chai-init-node.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
"use strict";

var Mocha = require('mocha')
var sinon = require('sinon')
var chai = require('chai')
var sinon_chai = require('sinon-chai')
var chai_as_promised = require('chai-as-promised')
var chai_subset = require('chai-subset')
var chai_moment = require('chai-moment')
const Mocha = require('mocha')
const sinon = require('sinon')
const chai = require('chai')
const dirty_chai = require("dirty-chai")
const chai_as_promised = require('chai-as-promised')
const sinon_chai = require('sinon-chai')
const chai_subset = require('chai-subset')
const chai_moment = require('chai-moment')


// expose some global variables as convenience
global.expect = chai.expect
global.sinon = sinon

// activate chai extensions
chai.use(sinon_chai)
// order is important: https://github.com/prodatakey/dirty-chai#use-with-chai-as-promised
chai.use(chai_as_promised)
chai.use(dirty_chai)
chai.use(sinon_chai)
chai.use(chai_subset)
chai.use(chai_moment)

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"chai-as-promised": "^7",
"chai-moment": "^0.1",
"chai-subset": "^1",
"dirty-chai": "^2.0.1",
"mocha": "^4",
"sinon": "^4",
"sinon-chai": "^2"
Expand Down