You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _guides/index.md
+72
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@ assertion styles.
17
17
18
18
-[Install Chai]({{site.github.url}}/guide/installation/) in node, the browser, and other environments.
19
19
-[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.
20
21
21
22
## Making Plugins
22
23
@@ -26,5 +27,76 @@ than what is included, limited only by what you want to achieve. The Plugin API
26
27
is also intended as a way to simplify testing by providing users a way to
27
28
encapsulate common assertions for repeat use.
28
29
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
+
exportconstmyGlobal= {...};
42
+
43
+
exportdefaultfunctionmyPlugin(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
+
constmyGlobal= {...};
55
+
56
+
exportdefaultfunctionmyPlugin(chai, utils) {
57
+
chai.myGlobal= myGlobal;
58
+
}
59
+
```
60
+
61
+
```javascript
62
+
// Another example of a plugin which may have issues:
63
+
64
+
exportdefaultfunctionmyPlugin(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*aschaifrom'chai';
77
+
78
+
let overwritten =false;
79
+
80
+
functionsomePlugin(base) {
81
+
if (!overwritten) {
82
+
base.util.overwriteMethod(base.Assertion.prototype, "equal", function (_super) {
83
+
returnfunction(...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
+
29
101
-[Core Plugin Concepts]({{site.github.url}}/guide/plugins/) covers the basics of using the Chai Plugin API.
30
102
-[Building a Helper]({{site.github.url}}/guide/helpers/) is a walkthrough for writing your first plugin.
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
+
importchaifrom'chai';
31
+
import { request }, chaiHttpfrom'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:
0 commit comments