I have a class where I am making an axios instance in the constructor. This class also has service methods (omitted in the snippet):-
export class CartCreationService extends SuperClass {
public static axiosInstance: AxiosInstance;
constructor(serviceName: string) {
super(serviceName);
CartCreationService.axiosInstance = axios.create({
baseURL: Configuration.NEW_CART_API,
headers: {
[Constants.CONTENT_TYPE]: Constants.APPLICATION_JSON,
[Constants.X_API_KEY]: Configuration.CLIENT_SIDE_API_KEY,
[Constants.CORRELATIONID]: Configuration.CORRELATIONID,
[Constants.ADD_EFFECTIVE_PRICE]: true
}
});
CartCreationService.axiosInstance = Utils.setAxiosLogInterceptor(CartCreationService.axiosInstance);
}
}
In my Mocha test file, this is what I have. I’m importing the same axios instance from my class and using it to call a service method:-
describe("Test", () => {
let cartCreationService;
before(() => {
cartCreationService = CartCreationService.Instance();
});
beforeEach(() => {
moxios.install(CartCreationService.Instance().axiosInstance);
});
afterEach(() => {
moxios.uninstall(CartCreationService.Instance().axiosInstance);
});
let requestJSON = {
"data": {}
};
const cartId = "036336e2ca5519f840dc";
const mainItemId = "";
it.only("should return", async() => {
let requrl = "url";
moxios.stubRequest(requrl, {
status: 200,
responseText: JSON.parse(JSON.stringify(cart))
});
try {
const result = await cartCreationService.addAccessoryToCart(requestJSON, cartId, mainItemId);
expect(result.type).to.equal("Cart");
requrl = "";
} catch (error) {
console.log("error ->", error);
}
});
});
I’m always getting an error Cannot read properties of undefined (reading 'url')
in the catch block. Where am I going wrong?