测试 express.js API 的最佳方法是什么 [英] What's the best way to test express.js API
问题描述
我是使用 JavaScript 进行 API 测试的新手.我找到了许多测试 REST API 的解决方案,但不确定什么是最好的.我在后端和测试中使用 express.js 开玩笑.
I'm new in API testing with JavaScript. I've found many solution for testing REST APIs, but not sure what's the best. Im using express.js in the backend and for the tests jest.
我已经看到我可以通过模拟函数来测试,或者我也可以直接模拟 API.
I've seen that I can test with jest, by mocking the function or that I can also mock the API directly.
我有一个存储对象的 data.js:
I've a data.js where the object is stored:
let data = [
{
id: 0,
name: "empty shopppinglist",
location: "",
targetDate: "",
priority: "",
isFinished: false,
items: ["vodka"
]
}
]
module.exports = data
然后在后端文件夹中我有这个来提供端点并读取/返回文件:
Then in the Backend Folder I have this to provide the endpoint and read / return the file:
function getData(){
let shoppingLists = data
return shoppingLists
}
module.exports.getData = getData
let shoppingLists = data
app.get('/api/shoppingLists', (req, res) => {
const listsWithoutItems = getData().map(({ items, ...rest }) => rest)
res.status(200).send(listsWithoutItems)
})
我也不确定,是否可以将 app.get 调用移动到函数.
I'm here also not sure, if I can move the app.get call to a function.
在我的测试中,我想测试 API 的行为,因此如果数据无效,我将收到错误 500 等.为此,我尝试过此测试:
In my test, I'd like to test the behaviour of the API, so if invalid data, that I will get an error 500 etc.. For this I've tried with this test:
describe('Get Endpoint for all Lists', () => {
it('should return 2 lists', async () => {
myapp.getData = jest.fn(() => [
{
"id": 0,
"name": "filled shopping list",
"location": "lidl",
"targetDate": "22.03.1986",
"priority": "1",
"isFinished": false,
"items": ["vanille"
]
}
])
const res = await request(myapp)
.get('/api/shoppingLists')
expect(res.statusCode).toEqual(200)
expect(res.body).toHaveProperty('2')
})
})
不幸的是,我总是从 data.js 获取原始条目,而不是模拟结果.通过搜索文档,我还看到,我可以模拟定义我的 API 的整个 app.js.但现在我不确定什么是更好的方法.
Unfortunately, I always get the original entry from data.js and not the mocked result. And by searching for documentation, I've also seen, that I can mock the whole app.js where my API is defined. But now I'm not sure what is the better way to do that.
推荐答案
将 getData
模拟为 myapp.getData
是不可能的.module.exports.getData = getData
不能很好地用于测试,因为它允许模拟 getData
仅当它在任何地方用作 exports.getData() 时
而不是 getData()
,这是不切实际的.
It's impossible to mock getData
as myapp.getData
. module.exports.getData = getData
doesn't serve a good purpose for testing because it allows to mock getData
only if it's used everywhere as exports.getData()
instead of getData()
, which is impractical.
data
需要在它定义的模块中模拟.然后,每个测试都应该重新导入依赖于它的所有模块,以便受到模拟的影响.jest.isolateModules
或 jest.resetModules
可以与 require
结合使用以提供特定于测试的模块模拟.
data
needs to be mocked in a module it's defined. Then all modules that depend on it should be re-imported per test in order to be affected by a mock. jest.isolateModules
or jest.resetModules
can be used in conjunction with require
to provide test-specific module mocks.
beforeEach(() => {
jest.resetModules();
});
it('should return 2 lists', async () => {
jest.mock('.../data', () => [...]);
const myapp = require('.../app');
const res = await request(myapp)
...
});
这篇关于测试 express.js API 的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!