使用sequelize-typescript运行Mocha测试时遇Model构造函数错误
Problem Statement
When running Mocha tests with sequelize-typescript, I encountered this error:
TypeError: Class constructor Model cannot be invoked without 'new'
The error points directly to the line where I try to instantiate my model: new User({ name: 'someName' }).
Relevant Code Snippets
My model definition:
@Table export class User extends Model<User> { @Column({primaryKey: true}) id: string; @Column name: string; } async function asyncSaveAndFindTest(): Promise<User[]> { return await new User({ name: 'someName' }).save().then(res => User.findAll()); }
Current TypeScript Config (backend/tsconfig.json)
"target": "es2017", "lib": [ "es2017" ]
Reproduction Steps
git clone https://github.com/avidanyum/test-node-sequelize cd test-node-sequelize npm install npm run-script test-mocha
Root Cause
This error stems from mismatched TypeScript compilation settings that break ES6 class inheritance, which sequelize-typescript relies on heavily. Sequelize's base Model class uses native ES6 class features, but if your config is missing critical flags or uses incompatible settings, the compiled code will try to invoke the Model constructor in an ES5-style way—leading to this exact error.
Common gaps in the config include missing decorator support, incorrect module system settings, or flags that don't preserve ES6 class semantics.
Solutions
1. Fix TypeScript Configuration
Update your backend/tsconfig.json to include these mandatory settings:
{ "compilerOptions": { "target": "es2017", "module": "commonjs", "lib": ["es2017"], "experimentalDecorators": true, "emitDecoratorMetadata": true, "strictPropertyInitialization": false, // Optional but avoids issues with dynamic model properties "outDir": "./dist" // Optional, but organizes compiled output } }
experimentalDecorators&emitDecoratorMetadata: Required forsequelize-typescriptdecorators like@Tableand@Columnto function properly.module: "commonjs": Ensures compatibility with Node.js's module system.strictPropertyInitialization: false: Prevents TypeScript from throwing errors about uninitialized model properties, since Sequelize handles this dynamically.
2. Adjust Mocha Run Command
Make sure Mocha respects your updated TypeScript config by adding the --project flag:
mocha --require ts-node/register --project ./backend/tsconfig.json ./backend/src/test/ts/**/*.mocha.test.ts
This ensures ts-node uses your custom config instead of default settings during test execution.
3. Verify Dependency Compatibility
Ensure sequelize and sequelize-typescript are using compatible versions. Check the official sequelize-typescript documentation for the recommended Sequelize version range, then update if needed:
npm install sequelize@<compatible-version> sequelize-typescript@latest
Verification
After applying these changes, re-run your Mocha tests. The error should be resolved, and your User model will instantiate correctly without the "Class constructor Model cannot be invoked without 'new'" message.
内容的提问来源于stack exchange,提问作者Jas

