javascript - 如何使用 express 并且仅使用 JEST 来测试 rest api?
问题描述
我可以只使用JEST来测试我的其余 API 端点express
吗?我搜索了几篇文章,还看到了一些stackoverflow的问题,看看我该怎么做。但问题在于express
大多数人都在使用mocha/chai或supertest进行测试。但我想在我的测试中只使用 JEST。这可能吗?
到目前为止,这是我要实现此测试的代码:
index.js
const express = require('express');
const app = express();
app.post('/insert', function (req, res, next) {
const values = req.body; //name, roll
pool.query(`INSERT INTO student SET ?`, [values], (err, result) => {
if (err){
let err = new Error('Not Connected');
next(err);
} else {
res.status(201).json({ msg: `added ${result.insertId}`});
console.log(result);
}
});
});
到目前为止我尝试的是: index.test.js:
const express = require('express');
const app = express();
app.use('../routes');
test('Test POST, Success Scenario', async () => {
const response = await app.post('/insert')({
const values //dummy values will be insert here
});
expect(response.statusCode).toBe(200);
});
我知道我的测试代码不正确,它只是一个伪代码我实际上很困惑我将如何在这里达到终点
解决方案
这是仅使用 JESTexpress
测试 Nodejs Web 框架REST API的单元测试解决方案:
index.js
:
const express = require('express');
const { Pool } = require('pg');
const app = express();
const pool = new Pool();
app.post('/insert', (req, res, next) => {
const values = req.body;
pool.query(`INSERT INTO student SET ?`, [values], (err, result) => {
if (err) {
err = new Error('Not Connected');
next(err);
} else {
res.status(201).json({ msg: `added ${result.insertId}` });
console.log(result);
}
});
});
index.spec.js
:
const routes = {};
jest.mock('express', () => {
const mExpress = {
post: jest.fn((path, controller) => {
routes[path] = controller;
})
};
return jest.fn(() => mExpress);
});
let queryCallback;
jest.mock('pg', () => {
const mpool = {
query: jest.fn((query, values, callback) => {
queryCallback = callback;
})
};
const mPool = jest.fn(() => mpool);
return { Pool: mPool };
});
require('./index');
const express = require('express');
const { Pool } = require('pg');
const app = express();
const pool = new Pool();
describe('insert', () => {
afterEach(() => {
jest.restoreAllMocks();
});
test('should insert data correctly', done => {
const logSpy = jest.spyOn(console, 'log');
expect(app.post).toBeCalledWith('/insert', expect.any(Function));
const mReq = { body: 1 };
const mRes = { status: jest.fn().mockReturnThis(), json: jest.fn().mockReturnThis() };
routes['/insert'](mReq, mRes);
expect(pool.query).toBeCalledWith('INSERT INTO student SET ?', [1], expect.any(Function));
const mResult = { insertId: 1 };
queryCallback(null, mResult);
expect(mRes.status).toBeCalledWith(201);
expect(mRes.status().json).toBeCalledWith({ msg: 'added 1' });
expect(logSpy).toBeCalledWith(mResult);
done();
});
test('should call error handler middleware', () => {
expect(app.post).toBeCalledWith('/insert', expect.any(Function));
const mReq = { body: 1 };
const mRes = { status: jest.fn().mockReturnThis(), json: jest.fn().mockReturnThis() };
const mNext = jest.fn();
routes['/insert'](mReq, mRes, mNext);
expect(pool.query).toBeCalledWith('INSERT INTO student SET ?', [1], expect.any(Function));
const mError = new Error('network error');
queryCallback(mError, null);
expect(mNext).toBeCalledWith(new Error('Not Connected'));
});
});
覆盖率 100% 的单元测试结果:
PASS src/stackoverflow/56635460/index.spec.js (7.391s)
insert
✓ should insert data correctly (15ms)
✓ should call error handler middleware (1ms)
console.log node_modules/jest-mock/build/index.js:860
{ insertId: 1 }
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 8.571s
源代码:https ://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/56635460
推荐阅读
- java - 在java中检查属性是否为空或为空的最简单方法
- python-3.x - 如何使用切片并选择 3 种不同场景的中间字符
- javascript - 为什么这个 document.getElementById() 函数不起作用?
- mysql - Springboot hibarnate 错误:通过 JDBC 语句执行 DDL 时出错,数据库名称有条形符号
- python - 重复一个问题,直到一个好的答案出现
- ruby-on-rails - ActiveModelSerializer 不序列化对象嵌套子级
- elasticsearch - 使用 nGram 进行休眠搜索 | 如何指示 nGram 在搜索期间不生成克
- c++ - 链接列表抛出异常并且不打印第一个节点
- c++ - 需要在c ++中以十六进制格式将字符串写入文件
- r - 转换为data.table中的字符时的因素处理