什么是测试express.js API的最佳方法 [英] What's the best way to test express.js API

查看:81
本文介绍了什么是测试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

然后在Backend文件夹中,我可以使用它提供端点并读取/返回文件:

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并不能很好地进行测试,因为它仅当在各处用作exports.getData()而不是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.isolateModulesjest.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆