Guest User

Untitled

a guest
Feb 4th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. const axios = require('axios');
  2.  
  3. class Users {
  4. static async all() {
  5. let res = await axios.get('http://localhost:3000/users');
  6. return res.data;
  7. }
  8. }
  9.  
  10. module.exports = Users;
  11.  
  12. const axios = require('axios');
  13. const Users = require('./users');
  14.  
  15. jest.mock('axios');
  16.  
  17. test('should fetch users', () => {
  18.  
  19. const users = [{
  20. "id": 1,
  21. "first_name": "Robert",
  22. "last_name": "Schwartz",
  23. "email": "rob23@gmail.com"
  24. }, {
  25. "id": 2,
  26. "first_name": "Lucy",
  27. "last_name": "Ballmer",
  28. "email": "lucyb56@gmail.com"
  29. }];
  30.  
  31. const resp = { data : users };
  32.  
  33. // axios.get.mockResolvedValue(resp);
  34. axios.get.mockImplementation(() => Promise.resolve(resp));
  35.  
  36. // console.log(resp.data);
  37.  
  38. return Users.all().then(resp => expect(resp.data).toEqual(users));
  39. });
  40.  
  41. expect(received).toEqual(expected)
  42.  
  43. 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"}]
  44. Received: undefined
  45.  
  46. { "users": [
  47. {
  48. "id": 1,
  49. "first_name": "Robert",
  50. "last_name": "Schwartz",
  51. "email": "rob23@gmail.com"
  52. },
  53. {
  54. "id": 2,
  55. "first_name": "Lucy",
  56. "last_name": "Ballmer",
  57. "email": "lucyb56@gmail.com"
  58. }
  59. ...
  60. ]
  61. }
Add Comment
Please, Sign In to add comment