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

配置ESM后Jest无法识别默认导出的问题求助

Jest + ts-jest ESM模式下无法识别默认导出问题

运行测试时Jest报错,无法识别应用的默认导出:

SyntaxError: The requested module '../app' does not provide an export named 'default'

我已经注释了除第一个测试外的所有用例,当前测试文件代码如下:

describe("User registration", () => {
    it("GET should return 404", async () => {
        const res = await request(app).get("/api/register");
        assert.equal(res.statusCode, 404);
    });
}

app.ts代码:

import dotenv from "dotenv";
import express from "express";
import cors from "cors";
import cookieParser from "cookie-parser";
import fileUpload from "express-fileupload";
import router from "./router/index";
import errorMiddleware from "./middlewares/error-middleware";
dotenv.config();
const app = express();
// enable req.ip
app.set("trust proxy", true);
app.use(fileUpload());
app.use(express.json({ limit: "1000mb" }));
app.use(cookieParser());
app.use(
  cors({
    credentials: true,
    origin: process.env.CLIENT_URL,
  })
);
app.use("/api", router);
app.use(errorMiddleware);

export default app;

我仅尝试过将导入语句从import app from "../app";替换为import { default as app } from "../app";,且确认app.ts的路径是正确的。

tsconfig.json配置:

{
  "compilerOptions": {
    /* Language and Environment */
    "target": "ES6" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,

    /* Modules */
    "module": "ES6" /* Specify what module code is generated. */,
    "rootDir": "./src" /* Specify the root folder within your source files. */,
    "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
    "baseUrl": "./src" /* Specify the base directory to resolve non-relative module names. */,
    "sourceMap": true /* Create source map files for emitted JavaScript files. */,
    "outDir": "./build" /* Specify an output folder for all emitted files. */,
    "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
    "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,

    /* Type Checking */
    "strict": true /* Enable all strict type-checking options. */,
    "noImplicitAny": true /* Enable error reporting for expressions and declarations with an implied 'any' type. */,
    "skipLibCheck": true /* Skip type checking all .d.ts files. */
  }
}

版本信息:

  • Jest版本:^29.3.1
  • TypeScript版本:4.9.4

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 19:25:49