supertest: 超级代理驱动的HTTP服务器API测试库
SuperTest通过简化HTTP断言来提升开发体验。它提供一个高级的HTTP测试抽象,同时保留了使用superagent的能力。安装SuperTest作为npm模块,并将其添加到package.json中的开发依赖项中:
```
npm install supertest --save-dev
```
安装完成后,可以通过简单的require('supertest')引入SuperTest。以下是一个基本的示例:
```
const request = require('supertest');
```
使用SuperTest时,可以将http.Server或Function传递给request()方法——如果服务器尚未侦听连接,它将绑定到临时端口,无需手动追踪端口号。SuperTest可以与任何测试框架一起使用。以下是一个不依赖于任何特定测试框架的示例:
```
const request = require('supertest');
const express = require('express');
// 创建Express应用
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
// 使用SuperTest测试应用
describe('GET /', () => {
it('should return "Hello World!"', async () => {
const response = await request(app).get('/');
expect(response.text).toBe('Hello World!');
});
});
```
```
npm install supertest --save-dev
```
安装完成后,可以通过简单的require('supertest')引入SuperTest。以下是一个基本的示例:
```
const request = require('supertest');
```
使用SuperTest时,可以将http.Server或Function传递给request()方法——如果服务器尚未侦听连接,它将绑定到临时端口,无需手动追踪端口号。SuperTest可以与任何测试框架一起使用。以下是一个不依赖于任何特定测试框架的示例:
```
const request = require('supertest');
const express = require('express');
// 创建Express应用
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
// 使用SuperTest测试应用
describe('GET /', () => {
it('should return "Hello World!"', async () => {
const response = await request(app).get('/');
expect(response.text).toBe('Hello World!');
});
});
```
supertest-master.zip
预估大小:16个文件
supertest-master
文件夹
.gitignore
46B
.eslintrc
656B
package.json
1KB
package-lock.json
155KB
.travis.yml
164B
LICENSE
1KB
index.js
785B
README.md
8KB
lib
文件夹
60.02KB
文件大小:
评论区