Nest 12 / Jest / TypeORM compatibility issue during test startup and ORM initialization #210553
Replies: 3 comments
|
💬 Your Product Feedback Has Been Submitted 🎉 Thank you for taking the time to share your insights with us! Your feedback is invaluable as we build a better GitHub experience for all our users. Here's what you can expect moving forward ⏩
Where to look to see what's shipping 👀
What you can do in the meantime 💻
As a member of the GitHub community, your participation is essential. While we can't promise that every suggestion will be implemented, we want to emphasize that your feedback is instrumental in guiding our decisions and priorities. Thank you once again for your contribution to making GitHub even better! We're grateful for your ongoing support and collaboration in shaping the future of our platform. ⭐ |
|
Hi @akieboyegodgift02! Excellent root-cause writeup. For developers hitting this exact combination (NestJS 12 ESM + TypeORM + Jest), here is a concise reference for the configuration adjustments: 1. Jest ESM Configuration with NestJS 12Nest 12 packages are published as pure ES modules. To make Jest parse them without CommonJS syntax errors: import type { Config } from 'jest';
const config: Config = {
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'node',
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\.{1,2}/.*)\.js$': '$1',
},
transform: {
'^.+\.(t|j)sx?$': [
'ts-jest',
{
useESM: true,
},
],
},
};
export default config;And execute your test runner with Node's experimental VM modules enabled: "scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
}2. TypeORM Driver for SQLiteAs of recent TypeORM releases, the legacy npm install better-sqlite3
npm install -D @types/better-sqlite3In your test TypeOrmModule.forRoot({
type: 'better-sqlite3',
database: ':memory:',
entities: [User, Wallet, Transaction],
synchronize: true,
})3. Circular Entity RelationsWhen entities reference each other (e.g. import type { Wallet } from './wallet.entity.js';
@Entity()
export class User {
@OneToMany(() => Wallet, (wallet) => wallet.user)
wallets: Wallet[];
} |

Uh oh!
There was an error while loading. Please reload this page.
🏷️ Discussion Type
Bug
💬 Feature/Topic Area
API
Body
GitHub Issue Format
Title
Fix Nest 12/Jest/TypeORM compatibility issues during test startup and ORM initialization
Description
The project initially failed during test startup because Nest 12 dependencies were being loaded as ES modules while the Jest configuration was still effectively using CommonJS-style loading. This caused Node to reject imports from
@nestjs/testingand related Nest packages before any tests executed.After resolving the ESM issue, the test setup still failed because the TypeORM SQLite configuration used an invalid driver name for the installed TypeORM version. The project used
sqlite, but the supported option in this environment wasbetter-sqlite3.A final issue also appeared due to circular entity references between
User,Wallet, andTransaction, which caused TypeORM metadata initialization to fail at runtime.Steps to Reproduce
Expected Behavior
Actual Behavior
sqliteas an unsupported driver.Root Cause
This was a dependency/runtime compatibility issue rather than an application logic bug:
sqliteas the driver nameFix
sqlitetobetter-sqlite3Impact
This blocked all test execution before assertions could run and prevented TypeORM metadata from building correctly.
Labels
All reactions