开玩笑看不到配置了 ESM 的默认导出



我使用Jest与ts-jest模块。我已经按照说明启动了ESM,看起来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 version:^29.3.1TypeScript版本:4.9.4

消除了这个误差。看起来你只是忘记了导出变量app…因此,如果一切都配置正确,将这一行添加到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; // <--- THIS LINE HERE 

最新更新