出现错误消息 TypeError: this.subQuery is not a function

Smn*_*Hgr 7 typescript jestjs typeorm

如果我用 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' });
Run Code Online (Sandbox Code Playgroud)

.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
Run Code Online (Sandbox Code Playgroud)

src/models/test.model.ts

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

    @Column()
    name!: string;

    @OneToMany(() => OtherModel, (other) => other.testModel)
    otherModels!: OtherModel[];
}
Run Code Online (Sandbox Code Playgroud)

测试/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);
                
            });
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

小智 4

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