You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jest+TypeScript测试Express上传路由时PDFLoader报readFile错误

Jest + TypeScript 测试Express文件上传路由时出现readFile is not a function错误

我用Jest和TypeScript编写测试用例时碰到了一个无法解决的错误。

我有一个Express.js后端,其中包含一个/upload路由用于接收用户上传的文件,代码如下:

import { Request, Response } from 'express';
import { FileArray, UploadedFile } from 'express-fileupload';
import { PDFLoader } from 'langchain/document_loaders/fs/pdf';

const uploadDocuments = async (req: Request, res: Response) => {
  const fileUpload: FileArray | null | undefined = req.files;
  // 一些检查逻辑....
  const file = fileUpload[0];
  // 其他代码...
  const filepath = ... // 文件路径处理逻辑
  const loader = new PDFLoader(filepath);
  const doc = await loader.load();
  console.log(doc);
  return res.send('ok');
}

这段代码正常运行时功能正常,路由可以正常处理文件上传。

现在我尝试用Jest为该路由编写测试用例。

我的jest.config.js配置如下:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  collectCoverageFrom: [
    'src/**/*.{ts,tsx}',
    '!**/node_modules/**',
    '!**/vendor/**',
  ],
  moduleDirectories: ["node_modules", 'src'],
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "cjs", "mjs"],
  verbose: true,
  moduleNameMapper: {
    "fs/promises": "<rootDir>/node_modules/fs-extra/lib/fs"
  },
  transform: {}
};

启动测试的命令:

"test": "node --experimental-vm-modules --trace-warnings node_modules/jest/bin/jest.js"

测试用例代码:

import path from 'path';
import app from '../../../../src/app';
import request from 'supertest';

describe('POST endpoints', () => {
  const endpoint: string = '/api/v1/documents/upload';

  it('should upload a document', async () => {
    const filepath: string = path.join(
      process.cwd(),
      '__tests__/unit/routes/documents/files/test.pdf'
    );
    console.log('filepath: ', filepath);
    const res = await request(app).post(endpoint).attach('files', filepath);
    console.log('res.body: ', res.body);
    console.log('res.body.length: ', res.body.length);

    expect(res.statusCode).toEqual(200);
    expect(res.body.length).toEqual(1);
    expect(res.body).toContain([
      {
        filename: 'test.pdf',
        success: false, // 目前无法正常运行,一直报"readFile is not a function"错误
      },
    ]);
  });
});

运行测试时出现如下错误:

TypeError: readFile is not a function
        at PDFLoader.load (/some_path/node_modules/.pnpm/langchain@0.1.36_chromadb@1.8.1_mammoth@1.7.2_officeparser@4.0.8_openai@4.40.1_pdf-parse@1.1.1_vectordb@0.4.18/node_modules/langchain/dist/document_loaders/fs/buffer.cjs:34:28)
        at PDFLoader.loadAndSplit (/some_path/node_modules/.pnpm/langchain@0.1.36_chromadb@1.8.1_mammoth@1.7.2_officeparser@4.0.8_openai@4.40.1_pdf-parse@1.1.1_vectordb@0.4.18/node_modules/langchain/dist/document_loaders/base.cjs:17:22)

我使用的依赖版本:

"jest": "^29.7.0",
    "@langchain/community": "^0.0.52",
    "@langchain/core": "^0.1.60",
    "@langchain/openai": "^0.0.28",
    "@types/cookie-parser": "^1.4.7",
    "@types/express-fileupload": "^1.5.0",
    "pdf-parse": "^1.1.1",

我尝试过修改Jest配置、更换依赖版本,但都无法解决问题。有人遇到过类似情况吗?

内容的提问来源于stack exchange,提问作者gabirel2

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 21:40:21