Movatterモバイル変換


[0]ホーム

URL:


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 thechai-http plugin.

Importing Chai

To use Chai with ESM, you can import Chai in your test files using theimport statement. Here’s how you can import theexpect interface:

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 theuse method to load it. Here’s how to use thechai-http plugin as an example:

import*aschaifrom'chai';import{request,defaultaschaiHttp}from'chai-http';chai.use(chaiHttp);// Now you can use `chai-http` using the `request` function.

chai-http Example

Here’s an example of usingchai-http to test an HTTP GET request:

import*aschaifrom'chai';import{request,defaultaschaiHttp}from'chai-http';const{expect}=chai;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();});});});

[8]ページ先頭

©2009-2025 Movatter.jp