【问题标题】:Error message TypeError: this.subQuery is not a function appears出现错误信息 TypeError: this.subQuery is not a function
【发布时间】:2022-04-21 15:21:29
【问题描述】:

如果我用 jest 运行测试,我总是会收到错误消息 TypeError: this.subQuery is not a function,并在 testModelDb.test.ts 文件中的标记行上注明

测试/jest.setup.ts

import 'reflect-metadata';
import dotenv from 'dotenv';
dotenv.config({ path: './.env.test' });

.env.test

TYPEORM_CONNECTION=sqlite
TYPEORM_DATABASE=:memory:
TYPEORM_SYNCHRONIZE=true
TYPEORM_LOGGING=false
TYPEORM_ENTITIES=dist/models/**/*.js,modules/**/entity/*.ts
TYPEORM_MIGRATIONS=dist/services/db/seeding.js
TYPEORM_MIGRATIONS_RUN=true

src/models/test.model.ts

@Entity()
export default class TestModel {
    @PrimaryGeneratedColumn()
    id!: number;

    @Column()
    name!: string;

    @OneToMany(() => OtherModel, (other) => other.testModel)
    otherModels!: OtherModel[];
}

测试/testModelDb.test.ts

describe('Integrationtests', () => {
    let connection: Connection;

    beforeAll(async () => { connection = await createConnection(); });

    afterAll(() => connection?.close());

    beforeEach(async () => { await connection.synchronize(true); });

    describe(`functions`, () => {
        describe(`#function1()`, () => {
            test('Test 1', async () => {
                await connection
                    .createQueryBuilder()
                    .insert()
                    .into(TestModel) // <- here
                    .values([ {name: 'Test item 1'} ])
                    .execute();
              
                expect(true).toBe(true);
                
            });
        });
    });
});

【问题讨论】:

    标签: typescript jestjs typeorm


    【解决方案1】:

    在连接配置中,确保您在entities:[] 配置属性中添加了实体名称。

    【讨论】:

      猜你喜欢
      • 2015-11-01
      • 2016-06-24
      • 2015-05-04
      • 2018-07-23
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多