Skip to content

Commit 8d428d4

Browse files
author
Kristján Oddsson
authored
Merge pull request #208 from chaijs/revert-change
Revert "Merge pull request #206 from chaijs/koddsson/add-esm-guide"
2 parents 01eb070 + f2e8ea7 commit 8d428d4

File tree

2 files changed

+0
-131
lines changed

2 files changed

+0
-131
lines changed

_guides/index.md

-72
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ 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.
2120

2221
## Making Plugins
2322

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

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-
10129
- [Core Plugin Concepts]({{site.github.url}}/guide/plugins/) covers the basics of using the Chai Plugin API.
10230
- [Building a Helper]({{site.github.url}}/guide/helpers/) is a walkthrough for writing your first plugin.

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

-59
This file was deleted.

0 commit comments

Comments
 (0)