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,7 +17,6 @@ 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.
21
20
22
21
## Making Plugins
23
22
@@ -27,76 +26,5 @@ than what is included, limited only by what you want to achieve. The Plugin API
27
26
is also intended as a way to simplify testing by providing users a way to
28
27
encapsulate common assertions for repeat use.
29
28
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
-
101
29
-[Core Plugin Concepts]({{site.github.url}}/guide/plugins/) covers the basics of using the Chai Plugin API.
102
30
-[Building a Helper]({{site.github.url}}/guide/helpers/) is a walkthrough for writing your first plugin.
0 commit comments