admin管理员组文章数量:1377595
I just started learning jest today, and I've read that you shouldn't hit actual api endpoints because its slow, or it's not standard practice. Instead you create mocks that represent the data you would get returned?
if the purpose of testing the route was to see if it worked, wouldn't making a mock of the data defeat the purpose. I guess I'm just confused by all the beginners guides and the Jest documentation is a bit over my head.
My question is should I test my routes files for my node server, also how I would go about testing my routes file for my node server. if my route looks like this:
// routes.js
const express = require('express');
const router = express.Router();
const axios = require('axios')
// my backend is connected to another api in this project
router.get('/', (req,res) => {
axios.get('').then(data => res.json(data.data))
})
please I've read the docs it hasn't helped could you give me a specific example
I just started learning jest today, and I've read that you shouldn't hit actual api endpoints because its slow, or it's not standard practice. Instead you create mocks that represent the data you would get returned?
if the purpose of testing the route was to see if it worked, wouldn't making a mock of the data defeat the purpose. I guess I'm just confused by all the beginners guides and the Jest documentation is a bit over my head.
My question is should I test my routes files for my node server, also how I would go about testing my routes file for my node server. if my route looks like this:
// routes.js
const express = require('express');
const router = express.Router();
const axios = require('axios')
// my backend is connected to another api in this project
router.get('/', (req,res) => {
axios.get('https://jsonplaceholder.typicode./users/1').then(data => res.json(data.data))
})
please I've read the docs it hasn't helped could you give me a specific example
Share Improve this question edited Feb 25, 2019 at 13:39 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Feb 23, 2019 at 18:55 Wolf_TruWolf_Tru 5731 gold badge7 silver badges19 bronze badges1 Answer
Reset to default 3What people who say "you shouldn't hit actual api endpoints because its slow, or it's not standard practice..." are trying to discourage you from doing is testing the interface to an external service.
Jest is for unit testing, not end-to-end or integration testing. Testing a third-party API, or even an API from an app that is not the one you're building, is out of scope for a unit test.
What you're asking about is really an integration test, and there's an answer on this StackOverflow thread that might be helpful for you in the context of testing a Node or Express app.
本文标签: javascripthow to test routes and endpoints with jestStack Overflow
版权声明:本文标题:javascript - how to test routes and endpoints with jest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744469354a2607705.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论