Skip to content

Commit

Permalink
Revert "Merge pull request #206 from chaijs/koddsson/add-esm-guide"
Browse files Browse the repository at this point in the history
This reverts commit 01eb070, reversing
changes made to 9fffbc6.
  • Loading branch information
koddsson committed May 15, 2024
1 parent 01eb070 commit f2e8ea7
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 131 deletions.
72 changes: 0 additions & 72 deletions _guides/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ assertion styles.

- [Install Chai]({{site.github.url}}/guide/installation/) in node, the browser, and other environments.
- [Learn about styles]({{site.github.url}}/guide/styles/) that you can use to define assertions.
- [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.

## Making Plugins

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

### Exposing Globals in Plugins

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:

#### Good Practice

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:

```javascript
// An example of a good plugin:

export const myGlobal = {...};

export default function myPlugin(chai, utils) {
}
```

#### Potential Issues

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:

```javascript
// An example of a plugin which may have issues:

const myGlobal = {...};

export default function myPlugin(chai, utils) {
chai.myGlobal = myGlobal;
}
```

```javascript
// Another example of a plugin which may have issues:

export default function myPlugin(chai, utils) {
chai.myGlobal = {...};
}
```

### Guard against multiple calls to `use(..)`

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.

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.

```js
import * as chai from 'chai';

let overwritten = false;

function somePlugin(base) {
if (!overwritten) {
base.util.overwriteMethod(base.Assertion.prototype, "equal", function (_super) {
return function(...args) {
console.log("Called!"); // log something out

return _super.call(this, ...args);
};
});
overwritten = true;
}
}

chai.use(somePlugin);
chai.use(somePlugin);

chai.expect(123).to.equal(123); // Logs `Called!` only once
```

By following these guidelines, you can create Chai plugins that are easy to use and maintain.

- [Core Plugin Concepts]({{site.github.url}}/guide/plugins/) covers the basics of using the Chai Plugin API.
- [Building a Helper]({{site.github.url}}/guide/helpers/) is a walkthrough for writing your first plugin.
59 changes: 0 additions & 59 deletions _guides/using-chai-with-esm-and-plugins.md

This file was deleted.

0 comments on commit f2e8ea7

Please sign in to comment.