我如何指定——experimental-specifier-resolution标志被包含在supertest或chai



我正在构建node express应用程序,最近我想用mocha编写一些测试。我愿意使用chai-htpp插件在测试期间运行我的应用程序。问题是,我在应用程序启动时使用——experimental-specifier-resolution标志来启用目录/桶导入(我使用的是ES模块格式,目前不允许这样的事情发生)。在下面的例子。

import chai from "chai";
import chaiHttp from "chai-http";
import app from "../src/index.js";
chai.use(chaiHttp);

describe("Test one", () => {
describe("should be happy", () => {
it("something", (done) => {
chai
.request(app)
.get("/")
.end((err, res) => {
console.log("ERR", err);
console.log("RES", res);
done();
});
});
});
});

使用mocha运行此测试会抛出以下错误

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/home/konrad/Projects/MW/src/routes' is not supported resolving ES modules imported from /home/konrad/Projects/MW/src/index.js
at new NodeError (node:internal/errors:372:5)
at finalizeResolution (node:internal/modules/esm/resolve:433:17)
at moduleResolve (node:internal/modules/esm/resolve:1009:10)
at defaultResolve (node:internal/modules/esm/resolve:1218:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:580:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:294:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:80:40)
at link (node:internal/modules/esm/module_job:78:36)

是否有办法自定义chai/超测服务器启动和通过——experimental-specifier-resolution标志?或者你可以想到更优雅的方式。

设置node-option传递node或v8参数

mocha --node-option experimental-specifier-resolution=explicit tests

或通过mocha选项

"mocha": {
"node-option": [
"experimental-specifier-resolution=explicit"
]
}

相关内容

  • 没有找到相关文章

最新更新