I'm building node express app and recently I wanted to write some tests with mocha. I'm willing to use chai-htpp plugin to run my app during tests. The problem is, I'm using --experimental-specifier-resolution flag on application launch to enable directory/barrel imports(I'm using ES modules format which somehow does not tolerate such thing for now). Example below.
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();
});
});
});
});
running this test with mocha throws following error
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)
Is there a way to customize chai/supertest server launch and pass --experimental-specifier-resolution flag? Or maybe there is more elegant way you can think of.
CodePudding user response:
Set node-option
to pass a node or v8 argument.
mocha --node-option experimental-specifier-resolution=explicit tests
or via mocha opts
"mocha": {
"node-option": [
"experimental-specifier-resolution=explicit"
]
}