sequelize / sequelize-typescript

Decorators and some other features for sequelize

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can this project be used with vitest?

dlandtaa opened this issue · comments

Issue

I am attempting to write some unit tests for a classes that uses my sequelize-typescript models but I am getting this error when I try to create an instance of a model:

FAIL  MyClass.test.ts [ MyClass.test.ts ]
Error: Specified type of property 'myId'
            cannot be automatically resolved to a sequelize data type. Please
            define the data type manually

Versions

  • sequelize: 6.37.1
  • sequelize-typescript: 2.1.6
  • vitest 1.3.1
  • typescript: 5.3.3
  • node v20.11.0
  • npm 10.2.4
  • macOS 13.5 Ventura

Issue type

  • bug report
  • feature request

Actual behavior

npx vitest

FAIL  MyClass.test.ts [ MyClass.test.ts ]
Error: Specified type of property 'myId'
            cannot be automatically resolved to a sequelize data type. Please
            define the data type manually
 ❯ getSequelizeTypeByDesignType node_modules/sequelize-typescript/dist/model/shared/model-service.js:64:11
 ❯ annotate node_modules/sequelize-typescript/dist/model/column/column.js:32:77
 ❯ Column node_modules/sequelize-typescript/dist/model/column/column.js:14:9
 ❯ __decorateClass MyModel.model.ts:8:24
      7|     myId: string;
      8| 
      9|     @Column
       |                       ^
     10|     name: string;
     11| }
 ❯ MyModel.model.ts:7:5
 ❯ MyClass.test.ts:2:31

Expected behavior

I should be able to create objects for unit tests.

Steps to reproduce

I created a repro repo if you want to be able to see the error happen:

git clone https://github.com/dlandtaa/sequelize-typescript-test
cd sequelize-typescript-test
npm i
npm start # should run and print out a model
npx vitest # should give you the error

Related code

If you don't want to download the repro repo, you can see the code here:

Main.ts

const { Sequelize } = require('sequelize-typescript');
import { MyClass } from './MyClass';
import { MyModel } from './MyModel.model';

const sqlize = new Sequelize('a', 'b', 'c', {
    host: 'd',
    dialect: 'mysql',
    models: [
        MyModel
    ]
});

const myModel = new MyModel();
const myObj = new MyClass();
myObj.doSomething(myModel);

MyModel.ts

import { Table, Column, Model, PrimaryKey } from 'sequelize-typescript';

@Table({ tableName: 'some_table', timestamps: false })
class MyModel extends Model {
    @PrimaryKey
    @Column
    myId: string;

    @Column
    name: string;
}

export { MyModel };

MyClass.ts

import { MyModel } from './MyModel.model';

class MyClass {
    doSomething(model: MyModel) {
        console.log('here is the model you passed in:', model);
    }   
}

export { MyClass };

MyClass.test.ts

import { test } from 'vitest';
import { MyModel } from './MyModel.model';
import { MyClass } from './MyClass';

test('generate D2 task ids', async () => {
    const model = new MyModel();
    const myObject = new MyClass();
    myObject.doSomething(model);
});