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!');
});
});
```
zip
supertest-master.zip 预估大小:16个文件
folder
supertest-master 文件夹
file
.gitignore 46B
file
.eslintrc 656B
file
package.json 1KB
file
package-lock.json 155KB
file
.travis.yml 164B
file
LICENSE 1KB
file
index.js 785B
file
README.md 8KB
folder
lib 文件夹
file
test.js 8KB
file
agent.js 2KB
file
.npmignore 76B
folder
test 文件夹
file
.eslintrc 287B
file
supertest.js 31KB
folder
fixtures 文件夹
file
test_key.pem 2KB
file
test_cert.pem 1KB
file
.editorconfig 199B
zip 文件大小:60.02KB