Reputation: 95
I am trying to run an API test for getting the version using Chai. I have this test in src/tests/version.tests.js
. I am using mocha ./src/tests/**/*.tests.js
to run it.
For some reason I get this error when running the command above:
Exception during run: version.tests.js:1
import chai, { expect } from "chai";
^^^^
SyntaxError: The requested module 'chai' does not provide an export named 'default'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
at async importModuleDynamicallyWrapper (node:internal/vm/module:430:15)
at async formattedImport (node_modules\mocha\lib\nodejs\esm-utils.js:9:14)
at async exports.requireOrImport (node_modules\mocha\lib\nodejs\esm-utils.js:42:28)
at async exports.loadFilesAsync (node_modules\mocha\lib\nodejs\esm-utils.js:100:20)
at async singleRun (node_modules\mocha\lib\cli\run-helpers.js:125:3)
at async exports.handler (node_modules\mocha\lib\cli\run.js:370:5)
My version.tests.js
file looks like this:
import chai, { expect } from "chai";
import chaiHttp from "chai-http";
import app from "../app.js";
chai.use(chaiHttp);
describe('Version API', () => {
it('should return the correct version', async () => {
try {
const res = await chai.request(app).get('/api/version');
expect(res).to.have.status(200);
} catch (error) {
throw new Error(error);
}
});
});
The packages' versions are:
"chai": "^5.1.1",
"chai-http": "^4.4.0",
"mocha": "^10.4.0"
Why is this happening?
Upvotes: 0
Views: 644
Reputation: 2120
Chai 5 cannot be imported as a default as it does not have default export. You can use chai 5.1 and chai-http 5.1 as below in your test files.
import { expect, use } from 'chai';
import { default as chaiHttp, request } from 'chai-http';
use(chaiHttp);
// execute method will take express app as input and returns ChaiHttp.Agent
req = request.execute(expressApp);
// Use req object to request API calls on app
req.get('/')
If you are using commonJS for your project and haven't specified package.json type as module
, then you will have to write test cases in .mjs
files.
Upvotes: 0