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
+30Lines changed: 30 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,6 +67,36 @@ export default function myPlugin(chai, utils) {
67
67
}
68
68
```
69
69
70
+
### Guard against multiple calls to `use(..)`
71
+
72
+
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.
73
+
74
+
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.
75
+
76
+
```js
77
+
import*aschaifrom'chai';
78
+
79
+
let overwritten =false;
80
+
81
+
functionsomePlugin(base) {
82
+
if (!overwritten) {
83
+
base.util.overwriteMethod(base.Assertion.prototype, "equal", function (_super) {
84
+
returnfunction(...args) {
85
+
console.log("Called!"); // log something out
86
+
87
+
return_super.call(this, ...args);
88
+
};
89
+
});
90
+
overwritten =true;
91
+
}
92
+
}
93
+
94
+
chai.use(somePlugin);
95
+
chai.use(somePlugin);
96
+
97
+
chai.expect(123).to.equal(123); // Logs `Called!` only once
98
+
```
99
+
70
100
By following these guidelines, you can create Chai plugins that are easy to use and maintain.
71
101
72
102
-[Core Plugin Concepts]({{site.github.url}}/guide/plugins/) covers the basics of using the Chai Plugin API.
0 commit comments