'Error message TypeError: this.subQuery is not a function appears
If I run the tests with jest, I got always the error message TypeError: this.subQuery is not a function
with a reference on the marked line in the testModelDb.test.ts file
tests/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[];
}
tests/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);
});
});
});
});
Solution 1:[1]
In connection configuration, make sure you added the entity name in the entities:[]
config property.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Giulio Caccin |