Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created April 16, 2019 17:50
Show Gist options
  • Save Ciantic/be6a8b8ca27ee15e2223f642b5e01549 to your computer and use it in GitHub Desktop.
Save Ciantic/be6a8b8ca27ee15e2223f642b5e01549 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");
});
@vagnerwentz
Copy link

I would like to know how can I mock the generated uuid when my id property has PrimaryGeneratedColumn('uuid'). Who can help?

@andresjesse
Copy link

perfect, thanks!

@VladPetriv
Copy link

Thanks

@BenFortner
Copy link

BenFortner commented Feb 5, 2022

@vagnerwentz you can generate uuid's by importing the v4 method from uuid.


import { v4 as uuidv4} from 'uuid';

const id = uuidv4();


@ayoubjamouhi
Copy link

Thank You

@pang0103
Copy link

pang0103 commented Jul 26, 2022

Anyone ran into exception:

DataTypeNotSupportedError: Data type "timestamp" in "SomeTable.CTS" is not supported by "sqlite" database.

Seems sqlite does not support "timestamp", any alternative approach ?

https://deno.land/x/[email protected]/docs/entities.md#column-types-for-sqlite--cordova--react-native--expo

@alexdw
Copy link

alexdw commented Oct 31, 2022

@pang0103 try to use better-sqlite3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment