You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用sequelize-typescript运行Mocha测试时遇Model构造函数错误

TypeError: Class constructor Model cannot be invoked without 'new' in sequelize-typescript Mocha Tests

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 for sequelize-typescript decorators like @Table and @Column to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:00:45