【发布时间】:2022-07-04 11:15:30
【问题描述】:
我在一个带有 Vue + Vite 的项目中使用 Vistest 作为测试单元。我有一个将图像上传到 Cloudinary 的助手,问题是运行测试时,Vitest 在控制台中返回此错误
Ocurrio un error al intentar subir la imagen TypeError: FormData is 不是构造函数
这是我的助手
import axios from "axios";
const uploadImage = async (file) => {
if (!file) return;
try {
const formData = new FormData();
const objData = {
file,
upload_preset: "journal-vue",
};
Object.entries(objData).forEach(([key, value]) => {
formData.append(key, value);
});
const url = "https://api.cloudinary.com/v1_1/christian-door/image/upload";
const { data } = await axios.post(url, formData);
return data.secure_url;
} catch (error) {
console.log("Ocurrio un error al intentar subir la imagen", error);
return null;
}
};
export default uploadImage;
这就是测试
import uploadImage from "@/modules/journal/helpers/uploadImage.js";
import axios from "axios";
describe("Test in helper uploadImage", () => {
test("Must be upload a file and return an url", async () => {
const url =
"https://res.cloudinary.com/christian-door/image/upload/v1653891463/fas3px2zm7eq8gt6mfaw.jpg";
const { data } = await axios.get(url, { responseType: "arraybuffer" });
const file = new File([data], "image.jpg");
const urc = await uploadImage(file);
console.log(urc);
});
});
构造函数是对的,它是大写的。我还更改了文件vite.config.js中“happy-dom”的环境@
【问题讨论】:
标签: javascript vue.js vite vitest