Closed ScriptPup closed 2 weeks ago
I had to downgrade a bunch of stuff to get this working. My new package.json looks like this.
{
"name": "ProjectName",
"version": "1.0.0",
"description": "Description",
"main": "./dist/server.js",
"scripts": {
"test": "mocha test/**/*.spec.ts --exit",
"predev": "tsc",
"pretest": "export MONGOMS_VERSION=6.0.9; ts-node test/start-mongdb-memsrv.ts",
"dev": "concurrently \"nodemon dist/server.js\" \"tsc -w\"",
"server-logs": "cat ./logs/Server.log | pino-pretty | code -",
"start": "nodemon dist/server.js"
},
"dependencies": {
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
"dotenv": "^16.4.5",
"jsonwebtoken": "^9.0.2",
"mongodb": "^6.10.0",
"mongoose": "^8.8.0",
"multer-gridfs-storage": "^5.0.2",
"pino": "^9.5.0",
"pino-pretty": "^11.3.0",
"restify": "^11.1.0",
"restify-errors": "^8.0.2",
"restify-pino-logger": "^3.0.0"
},
"devDependencies": {
"@types/chai": "^4.3.20",
"@types/chai-http": "^4.2.4",
"@types/jsonwebtoken": "^9.0.7",
"@types/mocha": "^10.0.9",
"@types/node": "^20.17.6",
"@types/restify": "^8.5.12",
"@types/restify-errors": "^4.3.9",
"chai": "^4.5.0",
"chai-http": "^4.4.0",
"chai-json-schema": "^1.5.1",
"chai-like": "^1.1.3",
"chai-things": "^0.2.0",
"concurrently": "^9.0.1",
"cross-env": "^7.0.3",
"esm": "^3.2.25",
"mocha": "^10.8.2",
"mongodb-memory-server": "^10.1.2",
"nodemon": "^3.1.7",
"npm-run-all": "^4.1.5",
"run-script-os": "^1.1.6",
"sinon": "^19.0.2",
"ts-auto-mock": "^3.7.4",
"ts-node": "^10.9.2",
"typescript": "^5.6.3"
}
}
I had to fully remove all packages (rm -f package-lock.json; rm -rf node_modules; npm cache clean --force
) and re-install with this configuration.
I guess there was some kind of breaking change between the last time I've used chai-http and now? I really don't know. It'd be great to be able to use the latest version with whatever enhancements have been made... But seems like the latest versions of things are broken. Don't know why, don't know where the problem lies.
I tried to mess with the packages locally to get the export
error to go away, but nothing I did fixed it. So, I have no clue.
chai-http 5.x and chai 5.x are both esm only
So if you're using require(...)
, you will need to stick with 4.x or use latest node which can require esm (23 I think?)
I do not use require(...)
syntax in any of my projects. Here's the test file I was trying to run in full:
process.env.LOG_LEVEL_CONSOLE = 'silent';
process.env.LOG_LEVEL = 'trace';
process.env.PORT = '8081';
process.env.PROTOCOL = "http";
import chai, { expect } from "chai";
import chaiHttp from "chai-http";
import { describe } from "mocha";
import { MongoMemoryServer } from "mongodb-memory-server";
import { setup, tearDown, server } from "../test.common";
chai.use(chaiHttp);
describe("Basic API Testing", function () {
const srv: { mongodb: null | MongoMemoryServer } = { mongodb: null };
this.beforeAll(async function () {
this.timeout(4000);
[srv.mongodb] = await setup();
// console.log("Setup complete");
return;
});
this.afterAll(async()=>{
await tearDown(srv);
});
// this.afterAll(async ()=> { await tearDown(srv); return; });
describe("Incorrect requests properly indicate their failure", () => {
it("GET - Will return 404 for non-existent resources", async () => {
const res = await chai.request(server).get("/_api/users_").send();
expect(res).to.have.status(404);
expect(res).to.be.json;
expect(res.body).to.include.keys("code", "message");
expect(res.body["code"]).to.eq("ResourceNotFound");
expect(res.body["message"]).to.eq("/_api/users_ does not exist");
});
it("POST - Will return 404 for non-existent resources", async () => {
const res = await chai.request(server).post("/_api/users_").send({});
expect(res).to.have.status(404);
expect(res).to.be.json;
expect(res.body).to.include.keys("code", "message");
expect(res.body["code"]).to.eq("ResourceNotFound");
expect(res.body["message"]).to.eq("/_api/users_ does not exist");
});
it("DELETE - Will return 404 for non-existent resources", async () => {
const res = await chai.request(server).delete("/_api/users_").send();
expect(res).to.have.status(404);
expect(res).to.be.json;
expect(res.body).to.include.keys("code", "message");
expect(res.body["code"]).to.eq("ResourceNotFound");
expect(res.body["message"]).to.eq("/_api/users_ does not exist");
});
});
});
Note: This is after getting it working again. Previously I had made the changes to the syntax to use directly imported { request } from chai-http rather than going through the
chai.use(...)
method.
The stack suggests it is being required. Does your tsconfig specify module: "commonjs"
? Does your package.json have type: "module"
?
Your source might be written as esm but the stack trace and error suggest your build output is commonjs
Sorry, since I downgraded to a working (for me) version I haven't been hopping on to check this feed very often. The module is commonjs, I guess that might be the issue then. I thought I tried to use es-next as well and still had the problem, but can't remember for sure... Maybe eventually I'll try upgrading again to mess with it. It's probably fair to say whatever is up is a me problem vs. a package problem though. :shrug:
I don't have type: "module"
in my package.json.
I'm unable to use chai-http in a new project. Every time I try to run my tests with
mocha test/**/*.spec.ts
, it fails to build with the following message:My package.json looks like this
And my tsconfig.json looks like this:
I've googled around and there are plenty of people with this error, but the solution always seems to be "add
main: <myfile.js>
to your package.json!" index.js is already defined in the 'chai-http' module, my own project already has a main defined... I have no idea why it's failing.If I remove chai-http, my tests that don't really on the
request
component work fine.Any help or ideas are greatly appreciated, been slamming my head into my desk for a few hours now.