Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const axios = require('axios');
- class Users {
- static async all() {
- let res = await axios.get('http://localhost:3000/users');
- return res.data;
- }
- }
- module.exports = Users;
- const axios = require('axios');
- const Users = require('./users');
- jest.mock('axios');
- test('should fetch users', () => {
- const users = [{
- "id": 1,
- "first_name": "Robert",
- "last_name": "Schwartz",
- "email": "rob23@gmail.com"
- }, {
- "id": 2,
- "first_name": "Lucy",
- "last_name": "Ballmer",
- "email": "lucyb56@gmail.com"
- }];
- const resp = { data : users };
- // axios.get.mockResolvedValue(resp);
- axios.get.mockImplementation(() => Promise.resolve(resp));
- // console.log(resp.data);
- return Users.all().then(resp => expect(resp.data).toEqual(users));
- });
- expect(received).toEqual(expected)
- Expected: [{"email": "rob23@gmail.com", "first_name": "Robert", "id": 1, "last_name": "Schwartz"}, {"email": "lucyb56@gmail.com", "first_name": "Lucy", "id": 2, "last_name": "Ballmer"}]
- Received: undefined
- { "users": [
- {
- "id": 1,
- "first_name": "Robert",
- "last_name": "Schwartz",
- "email": "rob23@gmail.com"
- },
- {
- "id": 2,
- "first_name": "Lucy",
- "last_name": "Ballmer",
- "email": "lucyb56@gmail.com"
- }
- ...
- ]
- }
Add Comment
Please, Sign In to add comment