const axios = require("axios").default const axiosCookieJarSupport = require("axios-cookiejar-support").default const CookieJar = require("tough-cookie").CookieJar const assert = require('assert') let instanceId = 1 const createAxiosInstance = () => { const axiosInstance = axios.create({ baseURL: "https://test.site/wp-json/cocart/v1", withCredentials: true, }) axiosCookieJarSupport(axiosInstance) axiosInstance.defaults.jar = new CookieJar() axiosInstance.instanceId = instanceId++ return axiosInstance } const getCart = async (axiosInstance) => axiosInstance.get("/get-cart").then((response) => { console.log(axiosInstance.instanceId, "get-cart", response.data) return response.data }) const addItem = async (axiosInstance, productId) => axiosInstance .post("/add-item", { product_id: productId, }) .then((response) => { console.log(axiosInstance.instanceId, "add-item", response.data) return response.data }) const clearCart = async (axiosInstance) => axiosInstance.post("/clear").then((response) => { console.log(axiosInstance.instanceId, "clear", response.data) return response.data }) async function run(axiosInstance) { let cart = await getCart(axiosInstance) assert(cart.length == 0) await addItem(axiosInstance, 1) cart = await getCart(axiosInstance) assert(cart.totals.subtotal == '$10.00') await addItem(axiosInstance, 2) cart = await getCart(axiosInstance) assert(cart.totals.subtotal == '$20.00') } run(createAxiosInstance()) run(createAxiosInstance())