Skip to content

Commit 01eb070

Browse files
author
Kristján Oddsson
authored
Merge pull request #206 from chaijs/koddsson/add-esm-guide
Add guide on using Chai with ESM and plugins
2 parents 9fffbc6 + 6115cc2 commit 01eb070

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

_guides/index.md

+72
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ assertion styles.
1717

1818
- [Install Chai]({{site.github.url}}/guide/installation/) in node, the browser, and other environments.
1919
- [Learn about styles]({{site.github.url}}/guide/styles/) that you can use to define assertions.
20+
- [Importing Chai, and using plugins]({{site.github.url}}/guide/using-chai-with-esm-and-plugins/) to learn how to use Chai with ESM and plugins.
2021

2122
## Making Plugins
2223

@@ -26,5 +27,76 @@ than what is included, limited only by what you want to achieve. The Plugin API
2627
is also intended as a way to simplify testing by providing users a way to
2728
encapsulate common assertions for repeat use.
2829

30+
### Exposing Globals in Plugins
31+
32+
When creating a Chai plugin, it's possible to expose globals that can be used across multiple files. Here's how to do it sustainably:
33+
34+
#### Good Practice
35+
36+
Prefer exporting any global in the module record so it can be imported directly instead of adding it as a property in the chai object:
37+
38+
```javascript
39+
// An example of a good plugin:
40+
41+
export const myGlobal = {...};
42+
43+
export default function myPlugin(chai, utils) {
44+
}
45+
```
46+
47+
#### Potential Issues
48+
49+
Avoid exposing globals only through `chai.use()` without making them available for import, as this can lead to issues when trying to use the global across multiple files:
50+
51+
```javascript
52+
// An example of a plugin which may have issues:
53+
54+
const myGlobal = {...};
55+
56+
export default function myPlugin(chai, utils) {
57+
chai.myGlobal = myGlobal;
58+
}
59+
```
60+
61+
```javascript
62+
// Another example of a plugin which may have issues:
63+
64+
export default function myPlugin(chai, utils) {
65+
chai.myGlobal = {...};
66+
}
67+
```
68+
69+
### Guard against multiple calls to `use(..)`
70+
71+
In certain situations the `use(..)` function could be called multiple times with your plugin. For a lot of plugins this won't be a issue but it's considered best practise to check if the plugin has been applied already.
72+
73+
Here's a contrived example of how you might implement a check in your plugin but the actual implementation is left up to the plugin author.
74+
75+
```js
76+
import * as chai from 'chai';
77+
78+
let overwritten = false;
79+
80+
function somePlugin(base) {
81+
if (!overwritten) {
82+
base.util.overwriteMethod(base.Assertion.prototype, "equal", function (_super) {
83+
return function(...args) {
84+
console.log("Called!"); // log something out
85+
86+
return _super.call(this, ...args);
87+
};
88+
});
89+
overwritten = true;
90+
}
91+
}
92+
93+
chai.use(somePlugin);
94+
chai.use(somePlugin);
95+
96+
chai.expect(123).to.equal(123); // Logs `Called!` only once
97+
```
98+
99+
By following these guidelines, you can create Chai plugins that are easy to use and maintain.
100+
29101
- [Core Plugin Concepts]({{site.github.url}}/guide/plugins/) covers the basics of using the Chai Plugin API.
30102
- [Building a Helper]({{site.github.url}}/guide/helpers/) is a walkthrough for writing your first plugin.
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: Using Chai with ESM and Plugins
3+
layout: guide
4+
bodyClass: guide
5+
weight: 0
6+
order: 20
7+
headings:
8+
- Importing Chai
9+
- Using Plugins
10+
- Exposing Globals in Plugins
11+
---
12+
13+
# Using Chai with ESM and Plugins
14+
15+
This guide provides an overview of how to use Chai with ECMAScript modules (ESM) and plugins, including examples using the `chai-http` plugin.
16+
17+
## Importing Chai
18+
19+
To use Chai with ESM, you can import Chai in your test files using the `import` statement. Here's how you can import the `expect` interface:
20+
21+
```javascript
22+
import { expect } from 'chai';
23+
```
24+
25+
## Using Plugins
26+
27+
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:
28+
29+
```javascript
30+
import chai from 'chai';
31+
import { request }, chaiHttp from 'chai-http';
32+
33+
chai.use(chaiHttp);
34+
35+
// Now you can use `chai-http` using the `request` function.
36+
```
37+
38+
### chai-http Example
39+
40+
Here's an example of using `chai-http` to test an HTTP GET request:
41+
42+
```javascript
43+
import chai, { expect } from 'chai';
44+
import { request }, chaiHttp from 'chai-http';
45+
46+
chai.use(chaiHttp);
47+
48+
describe('GET /user', () => {
49+
it('should return the user', done => {
50+
request('http://example.com')
51+
.get('/user')
52+
.end((err, res) => {
53+
expect(res).to.have.status(200);
54+
expect(res.body).to.be.an('object');
55+
done();
56+
});
57+
});
58+
});
59+
```

0 commit comments

Comments
 (0)