From 07712869387d284168a6b49f7ff101c9b36d9025 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Oddsson?= Date: Wed, 15 May 2024 17:36:46 +0200 Subject: [PATCH] Revert "Merge pull request #206 from chaijs/koddsson/add-esm-guide" This reverts commit 01eb0709b159717583c36f590361db54561d5e5d, reversing changes made to 9fffbc6f7eee2bec9cd5c1f0214615d6ebd2803e. --- _guides/index.md | 72 ---------------------- _guides/using-chai-with-esm-and-plugins.md | 59 ------------------ 2 files changed, 131 deletions(-) delete mode 100644 _guides/using-chai-with-esm-and-plugins.md diff --git a/_guides/index.md b/_guides/index.md index 6f96aa7a..c3578e70 100644 --- a/_guides/index.md +++ b/_guides/index.md @@ -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 @@ -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. diff --git a/_guides/using-chai-with-esm-and-plugins.md b/_guides/using-chai-with-esm-and-plugins.md deleted file mode 100644 index 615dcae0..00000000 --- a/_guides/using-chai-with-esm-and-plugins.md +++ /dev/null @@ -1,59 +0,0 @@ ---- - title: Using Chai with ESM and Plugins - layout: guide - bodyClass: guide - weight: 0 - order: 20 - headings: - - Importing Chai - - Using Plugins - - Exposing Globals in Plugins ---- - -# Using Chai with ESM and Plugins - -This guide provides an overview of how to use Chai with ECMAScript modules (ESM) and plugins, including examples using the `chai-http` plugin. - -## Importing Chai - -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: - -```javascript -import { expect } from 'chai'; -``` - -## Using Plugins - -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: - -```javascript -import chai from 'chai'; -import { request }, chaiHttp from 'chai-http'; - -chai.use(chaiHttp); - -// Now you can use `chai-http` using the `request` function. -``` - -### chai-http Example - -Here's an example of using `chai-http` to test an HTTP GET request: - -```javascript -import chai, { expect } from 'chai'; -import { request }, chaiHttp from 'chai-http'; - -chai.use(chaiHttp); - -describe('GET /user', () => { - it('should return the user', done => { - request('http://example.com') - .get('/user') - .end((err, res) => { - expect(res).to.have.status(200); - expect(res.body).to.be.an('object'); - done(); - }); - }); -}); -``` \ No newline at end of file