Skip to content

Instantly share code, notes, and snippets.

@enieber
Forked from Ciantic/example-typeorm-jest.test.ts
Created February 16, 2022 04:58
Show Gist options
  • Save enieber/82568e77fd365290ddf6a44bc286af12 to your computer and use it in GitHub Desktop.
Save enieber/82568e77fd365290ddf6a44bc286af12 to your computer and use it in GitHub Desktop.
Example of testing TypeOrm with Jest and Sqlite in-memory database
import { createConnection, getConnection, Entity, getRepository } from "typeorm";
import { PrimaryGeneratedColumn, Column } from "typeorm";
@Entity()
export class MyEntity {
@PrimaryGeneratedColumn()
id?: number;
@Column()
name?: string;
}
beforeEach(() => {
return createConnection({
type: "sqlite",
database: ":memory:",
dropSchema: true,
entities: [MyEntity],
synchronize: true,
logging: false
});
});
afterEach(() => {
let conn = getConnection();
return conn.close();
});
test("store Joe and fetch it", async () => {
await getRepository(MyEntity).insert({
name: "Joe"
});
let joe = await getRepository(MyEntity).find({
where: {
id: 1
}
});
expect(joe[0].name).toBe("Joe");
});
test("store Another and fetch it", async () => {
await getRepository(MyEntity).insert({
name: "Another"
});
let joe = await getRepository(MyEntity).find({
where: {
id: 1
}
});
expect(joe[0].name).toBe("Another");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment