Reputation: 563
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.com/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
Upvotes: 5
Views: 10635
Reputation: 5303
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.
Upvotes: 3