最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to test routes and endpoints with jest - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 3

What 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.

发布评论

评论列表(0)

  1. 暂无评论