I am doing Integration test using Jest in my node express Rest API application. I am using node version 18.19.1 and using Common Javascript Modules(cjs). I am getting Jest encountered an unexpected token error even thought I made a plenty of changes in my jest.config.js. Nothing helps to fix this issue.
FAIL test/user.test.js
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/node_modules/bson/lib/bson.cjs:593
inspect ??= defaultInspect;
^^^
SyntaxError: Unexpected token '??='
> 1 | const mongoose = require('mongoose');
| ^
2 | const request = require('supertest')
3 | const app = require("../app");
4 |
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
at Object.<anonymous> (node_modules/mongodb/src/bson.ts:3:1)
at Object.<anonymous> (node_modules/mongodb/src/admin.ts:1:1)
at Object.<anonymous> (node_modules/mongodb/src/index.ts:1:1)
at Object.<anonymous> (node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:9:20)
at Object.<anonymous> (node_modules/mongoose/lib/drivers/node-mongodb-native/index.js:7:22)
at Object.<anonymous> (node_modules/mongoose/lib/index.js:7:25)
at Object.<anonymous> (node_modules/mongoose/index.js:8:18)
at Object.<anonymous> (test/user.test.js:1:120)
What are the exact config transform should I use to make this working? Even through I tried to make this file Module Javascript (mjs), and added type:'module' in the package.json. But nothing working. If I change this, its affecting my entire project execution.
What are the steps I need to do here without touch anything on my existing project?
My test code is,
const mongoose = require('mongoose');
const request = require('supertest')
const app = require("../app");
require("dotenv").config();
/* Connecting to the database before each test. */
beforeEach(async () => {
await mongoose.connect('mydb_connection_string');
});
describe("POST /api/register", () => {
it("should register a user", async () => {
const res = await request(app).get("/api/register").send({
username: '[email protected]',
password: 'password123',
firstName: 'John',
lastName: 'Doe',
});
expect(res.statusCode).toBe(200);
expect(res.body.length).toBeGreaterThan(0);
});
});
/* Closing database connection after each test. */
afterEach(async () => {
await mongoose.connection.close();
});
https://www.freecodecamp.org/news/how-to-test-in-express-and-mongoose-apps/