我正在使用JEST测试我用TS制作的Express应用程序,它使用Multer上传文件.但我的测试以错误"TypeError: Cannot read properties of undefined (reading 'diskStorage')"
结束.
这个应用运行得很好,错误只出现在我测试的时候.
我的错误发生在-
import multer from "multer";
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "images/");
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + "-" + Math.round(Math.random() * 1e9);
cb(
null,
(
file.fieldname +
"-" +
uniqueSuffix +
"-" +
file.originalname
).replace(/ /g, "_")
);
},
});
function filter(req: object, file: Express.Multer.File, cb: Function) {
cb(null, file.mimetype === "image/jpg" || file.mimetype === "image/jpeg");
}
const postStorage = multer({
storage: storage,
fileFilter: filter,
limits: {
fileSize: 1024 * 1024, // max size in bytes
},
});
export default postStorage;
然后我在我的航线上用它-
const router = express.Router();
router.get("/", getAll);
router.post(
"/",
postStorage.fields([
{ name: "main_image", maxCount: 1 },
{ name: "additional_images", maxCount: 5 },
]),
RequestValidator(PostSchema, "body"),
insert
);
export default router;
我的jest密码是-
describe("do ", () => {
describe("dodo", () => {
it("dododo", async () => {
await supertest(app).get("/").expect(200);
return true;
});
});
});
如果能有任何帮助,我将不胜感激.谢谢.