Skip to content

Commit 04defb9

Browse files
committed
feat: add basic tests
1 parent 48de1bf commit 04defb9

File tree

9 files changed

+2313
-131
lines changed

9 files changed

+2313
-131
lines changed

.changeset/sixty-bobcats-pump.md

-36
This file was deleted.

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module.exports = {
55
},
66
"extends": [
77
"standard-with-typescript",
8-
"plugin:prettier/recommended"
8+
"plugin:prettier/recommended",
9+
'plugin:jest/recommended'
910
],
1011
"overrides": [
1112
{

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
**/node_modules
22
**/dist
3-
/coverage
3+
**/coverage
44
/temp
55
/tests/**/temp
66
/tests/generated-entities

examples/graphql/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void (async () => {
6868
// return await author.books.load();
6969
// return await author.books.load({ dataloader: true });
7070
// return await em.find(Book, { author: author.id });
71-
return entityDataLoader.find(Book, { author: author.id });
71+
return await entityDataLoader.find(Book, { author: author.id });
7272
},
7373
},
7474
},

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
],
88
"scripts": {
99
"build": "yarn workspace mikro-orm-find-dataloader run build",
10+
"test": "yarn workspace mikro-orm-find-dataloader run test",
1011
"version": "yarn changeset version && yarn install --immutable",
1112
"release": "yarn run compile && yarn changeset publish"
1213
},
@@ -22,18 +23,22 @@
2223
"@changesets/changelog-github": "^0.4.8",
2324
"@changesets/cli": "^2.26.2",
2425
"@types/eslint": "^8.44.7",
26+
"@types/jest": "^29.5.8",
2527
"@types/node": "^20.9.1",
2628
"@typescript-eslint/eslint-plugin": "^6.11.0",
2729
"eslint": "^8.54.0",
2830
"eslint-config-prettier": "^9.0.0",
2931
"eslint-config-standard-with-typescript": "^40.0.0",
3032
"eslint-plugin-import": "^2.29.0",
33+
"eslint-plugin-jest": "^27.6.0",
3134
"eslint-plugin-n": "^16.3.1",
3235
"eslint-plugin-prettier": "^5.0.1",
3336
"eslint-plugin-promise": "^6.1.1",
37+
"jest": "^29.7.0",
3438
"nodemon": "^3.0.1",
3539
"prettier": "^3.1.0",
3640
"rimraf": "^5.0.5",
41+
"ts-jest": "^29.1.1",
3742
"ts-node": "^10.9.1",
3843
"typescript": "^5.2.2"
3944
}

packages/find/package.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,21 @@
3232
"scripts": {
3333
"build": "yarn clean && yarn compile",
3434
"clean": "yarn run -T rimraf ./dist",
35-
"compile": "yarn run -T tsc"
35+
"compile": "yarn run -T tsc",
36+
"test": "yarn run -T jest"
37+
},
38+
"jest": {
39+
"testEnvironment": "node",
40+
"preset": "ts-jest",
41+
"collectCoverage": true,
42+
"testPathIgnorePatterns": ["/node_modules/", "/dist/"]
3643
},
3744
"publishConfig": {
3845
"access": "public"
3946
},
4047
"devDependencies": {
41-
"@mikro-orm/core": "6.0.0-dev.187"
48+
"@mikro-orm/core": "6.0.0-dev.187",
49+
"@mikro-orm/sqlite": "6.0.0-dev.187"
4250
},
4351
"peerDependencies": {
4452
"@mikro-orm/core": "^6.0.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`find should fetch books with the find dataloader 1`] = `
4+
[
5+
[
6+
{
7+
"author": 1,
8+
"id": 1,
9+
"title": "One",
10+
},
11+
{
12+
"author": 1,
13+
"id": 2,
14+
"title": "Two",
15+
},
16+
],
17+
[
18+
{
19+
"author": 2,
20+
"id": 3,
21+
"title": "Three",
22+
},
23+
],
24+
[
25+
{
26+
"author": 3,
27+
"id": 4,
28+
"title": "Four",
29+
},
30+
{
31+
"author": 3,
32+
"id": 5,
33+
"title": "Five",
34+
},
35+
{
36+
"author": 3,
37+
"id": 6,
38+
"title": "Six",
39+
},
40+
],
41+
[],
42+
[],
43+
]
44+
`;

packages/find/src/find.test.ts

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2+
import {
3+
type SqlEntityManager,
4+
MikroORM,
5+
Entity,
6+
PrimaryKey,
7+
Property,
8+
Collection,
9+
OneToMany,
10+
Ref,
11+
ref,
12+
ManyToOne,
13+
} from "@mikro-orm/sqlite";
14+
import { EntityDataLoader } from "./EntityDataLoader";
15+
16+
@Entity()
17+
class Author {
18+
@PrimaryKey()
19+
id!: number;
20+
21+
@Property()
22+
name: string;
23+
24+
@OneToMany(() => Book, (book) => book.author)
25+
books = new Collection<Book>(this);
26+
27+
constructor({ id, name }: { id?: number; name: string }) {
28+
if (id != null) {
29+
this.id = id;
30+
}
31+
this.name = name;
32+
}
33+
}
34+
35+
@Entity()
36+
class Book {
37+
@PrimaryKey()
38+
id!: number;
39+
40+
@Property()
41+
title: string;
42+
43+
@ManyToOne(() => Author, { ref: true })
44+
author: Ref<Author>;
45+
46+
constructor({ id, title, author }: { id?: number; title: string; author: Author | Ref<Author> }) {
47+
if (id != null) {
48+
this.id = id;
49+
}
50+
this.title = title;
51+
this.author = ref(author);
52+
}
53+
}
54+
55+
async function populateDatabase(em: MikroORM["em"]): Promise<void> {
56+
const authors = [
57+
new Author({ id: 1, name: "a" }),
58+
new Author({ id: 2, name: "b" }),
59+
new Author({ id: 3, name: "c" }),
60+
new Author({ id: 4, name: "d" }),
61+
new Author({ id: 5, name: "e" }),
62+
];
63+
em.persist(authors);
64+
65+
const books = [
66+
new Book({ id: 1, title: "One", author: authors[0]! }),
67+
new Book({ id: 2, title: "Two", author: authors[0]! }),
68+
new Book({ id: 3, title: "Three", author: authors[1]! }),
69+
new Book({ id: 4, title: "Four", author: authors[2]! }),
70+
new Book({ id: 5, title: "Five", author: authors[2]! }),
71+
new Book({ id: 6, title: "Six", author: authors[2]! }),
72+
];
73+
em.persist(books);
74+
await em.flush();
75+
}
76+
77+
describe("find", () => {
78+
let orm: MikroORM;
79+
let emFork: SqlEntityManager;
80+
let dataloader: EntityDataLoader;
81+
82+
beforeAll(async () => {
83+
orm = await MikroORM.init({
84+
dbName: ":memory:",
85+
entities: [Author, Book],
86+
});
87+
try {
88+
await orm.schema.clearDatabase();
89+
} catch {}
90+
try {
91+
const generator = orm.getSchemaGenerator();
92+
await generator.createSchema({ wrap: true });
93+
} catch {}
94+
await populateDatabase(orm.em.fork());
95+
});
96+
97+
beforeEach(async () => {
98+
emFork = orm.em.fork();
99+
dataloader = new EntityDataLoader(orm.em.fork());
100+
});
101+
102+
it("should fetch books with the find dataloader", async () => {
103+
const authors = await emFork.find(Author, {});
104+
const authorBooks = await Promise.all(authors.map(async ({ id }) => await dataloader.find(Book, { author: id })));
105+
expect(authorBooks).toBeDefined();
106+
expect(authorBooks).toMatchSnapshot();
107+
});
108+
109+
it("should return the same books as find", async () => {
110+
const authors = await emFork.find(Author, {});
111+
const dataloaderBooks = await Promise.all(
112+
authors.map(async ({ id }) => await dataloader.find(Book, { author: id })),
113+
);
114+
const findBooks = await Promise.all(authors.map(async ({ id }) => await emFork.find(Book, { author: id })));
115+
expect(dataloaderBooks.map((res) => res.map(({ id }) => id))).toEqual(
116+
findBooks.map((res) => res.map(({ id }) => id)),
117+
);
118+
});
119+
120+
afterEach(async () => {});
121+
122+
afterAll(async () => {
123+
await orm.close(true);
124+
});
125+
});

0 commit comments

Comments
 (0)