link1096 link1097 link1098 link1099 link1100 link1101 link1102 link1103 link1104 link1105 link1106 link1107 link1108 link1109 link1110 link1111 link1112 link1113 link1114 link1115 link1116 link1117 link1118 link1119 link1120 link1121 link1122 link1123 link1124 link1125 link1126 link1127 link1128 link1129 link1130 link1131 link1132 link1133 link1134 link1135 link1136 link1137 link1138 link1139 link1140 link1141 link1142 link1143 link1144 link1145 link1146 link1147 link1148 link1149 link1150 link1151 link1152 link1153 link1154 link1155 link1156 link1157 link1158 link1159 link1160 link1161 link1162 link1163 link1164 link1165 link1166 link1167 link1168 link1169 link1170 link1171 link1172 link1173 link1174 link1175 link1176 link1177 link1178 link1179 link1180 link1181 link1182 link1183 link1184 link1185 link1186 link1187 link1188 link1189 link1190 link1191 link1192 link1193 link1194 link1195 link1196 link1197 link1198 link1199 link1200 link1201 link1202 link1203 link1204 link1205 link1206 link1207 link1208 link1209 link1210 link1211 link1212 link1213 link1214 link1215 link1216 link1217 link1218 link1219 link1220 link1221 link1222 link1223 link1224 link1225 link1226 link1227 link1228 link1229 link1230 link1231 link1232

[Vue.js] axios mock can't handle default headers when using jest

I’m making a test that includes an axios call to an endpoints. to mock out the call to the endpoint and return some custom data so that I’m not hitting a server everytime I’m testing.

Here is the code for the action that is in it’s own action.js file.

login ({commit}, user) {
return new Promise((resolve, reject) => {
axios.post(‘https://backendauth.free.beeceptor.com/api/login', user)
.then(resp => {
console.log(‘response here’)
console.log(resp)
console.log(resp.data)
const token = resp.data.success.token
const user = resp.data.user
localStorage.setItem(‘token’, token)
axios.defaults.headers.common[“Authorization”] = “Bearer “ + token;
commit(‘LOGIN_SUCCESS’, token, user)

When logging in the action calls out to an endpoint and that endpoint returns a token which is stored in local storage. The token is also appended to the axios default headers so I don’t have to attach the token everytime to make an axios call in the application.

Now to the test. I’ve written a test that sucessfully mocks the axios post however fails with an error when setting the default headers for the mock.

Here is the error

TypeError: Cannot read property ‘headers’ of undefined

37 | const user = resp.data.user
38 | localStorage.setItem(‘token’, token)
\> 39 | axios.defaults.headers.common[“Authorization”] = “Bearer “ + token;
| ^
40 | commit(‘LOGIN_SUCCESS’, token, user)
41 | resolve(resp)
42 | })

Here is the test including the mock

import actions from “../../src/store/actions”;

let url = “”;
let body = “”;

jest.mock(“axios”, () => ({
post: (_url, _body, config) => {
return new Promise((resolve) => {
resolve({‘data’ : {‘success’: {‘token’ : ‘test’}})
})
},

}))

describe(‘login action’, () => {
it(“let the user login and access login success mutator”, async() => {
const commit = jest.fn()
const username = ‘test’
const password = ‘test’

await actions.login({commit}, {username, password})
})
})

The jest mock is a full mock which returns a promise when called, with the data I need, however the code fails when trying to set the headers. Is there a way I can mock this out as well or do I need to write the mock in a different way?

Solution :

Here is how you do it for anyone with the same problem. Npm install axios-mock-adapter and then use the following code but change it for the needs.

import actions from “../../src/store/actions”;
import axios from ‘axios’;
import MockAdapter from ‘axios-mock-adapter’;
describe(‘login action’, () => {
it(“let the user login and access login success mutator”, async() => {

let mockAdapter = new MockAdapter(axios);

mockAdapter.onPost(‘https://hotel-dev.devtropolis.co.uk/api/apilogin').reply(200, {
token: ‘test token’,
user: {username: ‘test’, password: ‘test’}
});

const commit = jest.fn()
const username = ‘test’
const password = ‘test’

await actions.login({commit}, {username, password})

expect(axios.defaults.headers.common.Authorization).toBe(‘Bearer test token’)

expect(commit).toHaveBeenCalledWith(
“LOGIN_SUCCESS”,’test token’, {username, password})
})

})