Skip to content

Commit 3e13c7f

Browse files
authored
Merge pull request #215 from chaijs/guides-esm
docs: update guides to use esm
2 parents 5764f2a + 352b37c commit 3e13c7f

File tree

4 files changed

+56
-45
lines changed

4 files changed

+56
-45
lines changed

_guides/helpers.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Hopefully our `Model` class is self explanatory, but as an example,
7474
here we construct a person object.
7575

7676
```javascript
77-
var arthur = new Model('person');
77+
const arthur = new Model('person');
7878
arthur.set('name', 'Arthur Dent');
7979
arthur.set('occupation', 'traveller');
8080
console.log(arthur.get('name')); // Arthur Dent
@@ -97,7 +97,7 @@ implementation throughout.
9797
For this example, we want the following test case to pass:
9898

9999
```javascript
100-
var arthur = new Model('person');
100+
const arthur = new Model('person');
101101
expect(arthur).to.be.a.model;
102102
```
103103

@@ -141,7 +141,7 @@ expect(arthur).to.be.a.model('person');
141141

142142
// language chain method
143143
Assertion.addMethod('model', function (type) {
144-
var obj = this._obj;
144+
const obj = this._obj;
145145

146146
// first, our instanceof check, shortcut
147147
new Assertion(this._obj).to.be.instanceof(Model);
@@ -176,8 +176,8 @@ To understand when to best use chainable methods we will examine a chainable met
176176
core.
177177
178178
```javascript
179-
var arr = [ 1, 2, 3 ]
180-
, obj = { a: 1, b: 2 };
179+
const arr = [ 1, 2, 3 ];
180+
const obj = { a: 1, b: 2 };
181181

182182
expect(arr).to.contain(2);
183183
expect(obj).to.contain.key('a');
@@ -241,7 +241,7 @@ function assertModelAge (n) {
241241
new Assertion(this._obj).to.be.instanceof(Model);
242242

243243
// make sure we have an age and its a number
244-
var age = this._obj.get('age');
244+
const age = this._obj.get('age');
245245
new Assertion(age).to.be.a('number');
246246

247247
// do our comparison
@@ -301,7 +301,7 @@ Let's start out with the basic overwrite utility and a basic assertion.
301301
```javascript
302302
chai.overwriteProperty('ok', function (_super) {
303303
return function checkModel () {
304-
var obj = this._obj;
304+
const obj = this._obj;
305305
if (obj && obj instanceof Model) {
306306
new Assertion(obj).to.have.deep.property('_attrs.id').a('number');
307307
} else {
@@ -324,7 +324,7 @@ will serve as the actual assertion.
324324
With this in place, we can write positive assertions.
325325
326326
```javascript
327-
var arthur = new Model('person');
327+
const arthur = new Model('person');
328328
arthur.set('id', 42);
329329
expect(arthur).to.be.ok;
330330
expect(true).to.be.ok;
@@ -336,7 +336,7 @@ revert to the original behavior. We will, however, run into a bit of
336336
trouble if we try to negate an `ok` assertion on a model.
337337
338338
```javascript
339-
var arthur = new Model('person');
339+
const arthur = new Model('person');
340340
arthur.set('id', 'dont panic');
341341
expect(arthur).to.not.be.ok;
342342
```
@@ -355,10 +355,10 @@ property overwrite would look like this.
355355
```javascript
356356
chai.overwriteProperty('ok', function (_super) {
357357
return function checkModel () {
358-
var obj = this._obj;
358+
const obj = this._obj;
359359
if (obj && obj instanceof Model) {
360360
new Assertion(obj).to.have.deep.property('_attrs.id'); // we always want this
361-
var assertId = new Assertion(obj._attrs.id);
361+
const assertId = new Assertion(obj._attrs.id);
362362
utils.transferFlags(this, assertId, false); // false means don't transfer `object` flag
363363
assertId.is.a('number');
364364
} else {
@@ -380,7 +380,7 @@ for the wrong type of id attribute, we would get an error message that states
380380
test suite, so we will provide it with a bit more information.
381381
382382
```javascript
383-
var assertId = new Assertion(obj._attrs.id, 'model assert ok id type');
383+
const assertId = new Assertion(obj._attrs.id, 'model assert ok id type');
384384
```
385385
386386
This will change our error message to be a more informative `model assert ok id type:
@@ -393,7 +393,7 @@ For this example we will be returning to our example of asserting Arthur's
393393
age to be above a minimum threshold.
394394
395395
```javascript
396-
var arthur = new Model('person');
396+
const arthur = new Model('person');
397397
arthur.set('age', 27);
398398
expect(arthur).to.have.age.above(17);
399399
```
@@ -405,7 +405,7 @@ so all we have to do is check if that exists.
405405
Assertion.overwriteMethod('above', function (_super) {
406406
return function assertAge (n) {
407407
if (utils.flag(this, 'model.age')) {
408-
var obj = this._obj;
408+
const obj = this._obj;
409409

410410
// first we assert we are actually working with a model
411411
new Assertion(obj).instanceof(Model);
@@ -414,7 +414,7 @@ Assertion.overwriteMethod('above', function (_super) {
414414
new Assertion(obj).to.have.deep.property('_attrs.age').a('number');
415415

416416
// now we compare
417-
var age = obj.get('age');
417+
const age = obj.get('age');
418418
this.assert(
419419
age > n
420420
, "expected #{this} to have an age above #{exp} but got #{act}"

_guides/plugins.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ community. A more appropriate pattern for creating helpers is as follows...
4646
For our helper file: `test/helpers/model.js`
4747

4848
```javascript
49-
module.exports = function (chai, utils) {
49+
export function chaiModel(chai, utils) {
5050
var Assertion = chai.Assertion;
5151

5252
// your helpers here
@@ -56,9 +56,9 @@ module.exports = function (chai, utils) {
5656
And, for our actual test: `test/person.js`
5757

5858
```javascript
59-
var chai = require('chai')
60-
, chaiModel = require('./helpers/model')
61-
, expect = chai.expect;
59+
import * as chai from 'chai';
60+
import {chaiModel} from './helpers/model.js'
61+
import {expect} from 'chai';
6262

6363
chai.use(chaiModel);
6464
```
@@ -85,9 +85,9 @@ The flag utility is exposed as `utils.flag` from within our `use` function. It c
8585
as either a getter or a setter, depending on the number of arguments passed to it.
8686

8787
```javascript
88-
var myAssert = new Assertion(obj);
88+
const myAssert = new Assertion(obj);
8989
utils.flag(myAssert, 'owner', 'me'); // sets key `owner` to `me`
90-
var owner = utils.flag(myAssert, 'owner'); // get key `owner', returns value
90+
const owner = utils.flag(myAssert, 'owner'); // get key `owner', returns value
9191
```
9292

9393
### object flag
@@ -96,15 +96,15 @@ The most important of Chai's reserved flags is the `object` flag. This is the su
9696
of an assertion.
9797

9898
```javascript
99-
var myAssert = new Assertion('Arthur Dent');
100-
var obj = flag(myAssert, 'object'); // obj === 'Arthur Dent';
99+
const myAssert = new Assertion('Arthur Dent');
100+
const obj = flag(myAssert, 'object'); // obj === 'Arthur Dent';
101101
```
102102

103103
This flag is so often used that a shortcut was provided as the `_obj` property of a
104104
constructed assertion.
105105

106106
```javascript
107-
var obj = myAssert._obj; // obj === `Arthur Dent`
107+
const obj = myAssert._obj; // obj === `Arthur Dent`
108108
```
109109

110110
The following flags are used by Chai's core assertions. Side effects may occur should you
@@ -135,7 +135,7 @@ negated or not.
135135
To begin, we will construct Arthur again, then we can assert that he is who he says he is.
136136

137137
```javascript
138-
var arthur = new Assertion('Arthur Dent');
138+
const arthur = new Assertion('Arthur Dent');
139139

140140
arthur.assert(
141141
arthur._obj === 'Arthur Dent'

_guides/styles.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ node.js. This assert module, however, provides several additional
2828
tests and is browser compatible.
2929

3030
```js
31-
var assert = require('chai').assert
32-
, foo = 'bar'
33-
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
31+
import {assert} from 'chai';
32+
33+
const foo = 'bar';
34+
const beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
3435

3536
assert.typeOf(foo, 'string'); // without optional message
3637
assert.typeOf(foo, 'string', 'foo is a string'); // with optional message
@@ -59,9 +60,10 @@ The BDD style is exposed through `expect` or `should` interfaces. In both
5960
scenarios, you chain together natural language assertions.
6061

6162
```js
62-
var expect = require('chai').expect
63-
, foo = 'bar'
64-
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
63+
import {expect} from 'chai';
64+
65+
const foo = 'bar';
66+
const beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
6567

6668
expect(foo).to.be.a('string');
6769
expect(foo).to.equal('bar');
@@ -73,7 +75,7 @@ Expect also allows you to include arbitrary messages to prepend to any failed
7375
assertions that might occur.
7476

7577
```js
76-
var answer = 43;
78+
const answer = 43;
7779

7880
// AssertionError: expected 43 to equal 42.
7981
expect(answer).to.equal(42);
@@ -94,9 +96,12 @@ property to start your chain. This style has some issues when used with Internet
9496
Explorer, so be aware of browser compatibility.
9597

9698
```js
97-
var should = require('chai').should() //actually call the function
98-
, foo = 'bar'
99-
, beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
99+
import {should} from 'chai';
100+
101+
should(); //actually call the function
102+
103+
const foo = 'bar';
104+
const beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };
100105

101106
foo.should.be.a('string');
102107
foo.should.equal('bar');
@@ -111,9 +116,10 @@ First of all, notice that the `expect` require is just a reference to the
111116
being executed.
112117

113118
```js
114-
var chai = require('chai')
115-
, expect = chai.expect
116-
, should = chai.should();
119+
import * as chai from 'chai';
120+
121+
const {expect} = chai;
122+
const should = chai.should();
117123
```
118124

119125
The `expect` interface provides a function as a starting point for chaining
@@ -143,7 +149,10 @@ with a `should` chain starter. As such, the appropriate few assertions
143149
for this scenario are as follows:
144150

145151
```js
146-
var should = require('chai').should();
152+
import {should as loadShould} from 'chai';
153+
154+
const should = loadShould();
155+
147156
db.get(1234, function (err, doc) {
148157
should.not.exist(err);
149158
should.exist(doc);
@@ -168,7 +177,7 @@ statement – it has to go on its own line, which looks a little
168177
verbose:
169178

170179
```js
171-
import chai from 'chai';
180+
import * as chai from 'chai';
172181
chai.should();
173182
```
174183

_guides/using-chai-with-esm-and-plugins.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import { expect } from 'chai';
2727
Chai plugins can extend Chai's capabilities. To use a plugin, you first need to install it, then use the `use` method to load it. Here's how to use the `chai-http` plugin as an example:
2828

2929
```javascript
30-
import chai from 'chai';
31-
import { request }, chaiHttp from 'chai-http';
30+
import * as chai from 'chai';
31+
import { request, default as chaiHttp } from 'chai-http';
3232

3333
chai.use(chaiHttp);
3434

@@ -40,8 +40,10 @@ chai.use(chaiHttp);
4040
Here's an example of using `chai-http` to test an HTTP GET request:
4141

4242
```javascript
43-
import chai, { expect } from 'chai';
44-
import { request }, chaiHttp from 'chai-http';
43+
import * as chai from 'chai';
44+
import { request, default as chaiHttp } from 'chai-http';
45+
46+
const {expect} = chai;
4547

4648
chai.use(chaiHttp);
4749

@@ -56,4 +58,4 @@ describe('GET /user', () => {
5658
});
5759
});
5860
});
59-
```
61+
```

0 commit comments

Comments
 (0)