Skip to content

Commit

Permalink
test: improve test performance
Browse files Browse the repository at this point in the history
  • Loading branch information
yujiosaka committed Oct 26, 2023
1 parent acf9151 commit 3886536
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 36 deletions.
10 changes: 4 additions & 6 deletions test/integrations/mongodb.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe } from "bun:test";
import { afterAll, afterEach, beforeAll, describe } from "bun:test";
import type { Connection, Model } from "mongoose";
import { createConnection } from "mongoose";
import type MongodbJobLock from "../../src/job-lock/mongodb";
Expand All @@ -15,17 +15,15 @@ describe("integration tests", () => {
beforeAll(async () => {
conn = createConnection(Bun.env.MONGO_URI!);
await waitUntil(() => conn.asPromise());

jobStore = new MongodbJobStore(conn);
model = conn.models.JobLock;
});

afterAll(async () => {
await jobStore.close();
});

beforeEach(() => {
jobStore = new MongodbJobStore(conn);
model = conn.models.JobLock;
});

afterEach(async () => {
await model.deleteMany({});
});
Expand Down
8 changes: 3 additions & 5 deletions test/integrations/redis.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe } from "bun:test";
import { afterAll, afterEach, beforeAll, describe } from "bun:test";
import { createClient } from "redis";
import RedisJobStore from "../../src/job-store/redis";
import { waitUntil } from "../helper";
Expand All @@ -14,16 +14,14 @@ describe("integration tests", () => {
beforeAll(async () => {
client = createClient({ url: Bun.env.REDIS_URI });
await waitUntil(() => client.connect());

jobStore = new RedisJobStore(client);
});

afterAll(async () => {
await jobStore.close();
});

beforeEach(() => {
jobStore = new RedisJobStore(client);
});

afterEach(async () => {
await client.flushAll();
});
Expand Down
7 changes: 5 additions & 2 deletions test/integrations/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterEach, beforeEach, expect, Mock, mock, setSystemTime, test } from "bun:test";
import { afterEach, beforeAll, beforeEach, expect, Mock, mock, setSystemTime, test } from "bun:test";
import { add, sub } from "date-fns";
import Cronyx from "../../src";
import type { JobLockId } from "../../src";
Expand Down Expand Up @@ -27,9 +27,12 @@ export function testBehavesLikeCronyx<S extends BaseJobStore<I>, I = JobLockId<S
let jobTask: Mock<() => Promise<void>>;
let requiredJobTask: Mock<() => Promise<void>>;

beforeEach(() => {
beforeAll(() => {
jobStore = getJobStore();
cronyx = new Cronyx({ jobStore, timezone });
});

beforeEach(() => {
jobTask = mock(() => Promise.resolve());
requiredJobTask = mock(() => Promise.resolve());
setSystemTime(requestedAt);
Expand Down
10 changes: 4 additions & 6 deletions test/integrations/typeorm.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe } from "bun:test";
import { afterAll, afterEach, beforeAll, describe } from "bun:test";
import { DataSource } from "typeorm";
import type { Repository } from "typeorm";
import type TypeormJobLock from "../../src/job-lock/typeorm";
Expand Down Expand Up @@ -35,17 +35,15 @@ describe.each([

beforeAll(async () => {
await waitUntil(() => dataSource.initialize());

jobStore = new JobStore(dataSource);
repository = dataSource.getRepository<TypeormJobLock>(TypeormJobLockEntity);
});

afterAll(async () => {
await jobStore.close();
});

beforeEach(() => {
jobStore = new JobStore(dataSource);
repository = dataSource.getRepository<TypeormJobLock>(TypeormJobLockEntity);
});

afterEach(async () => {
await repository.delete({});
});
Expand Down
10 changes: 4 additions & 6 deletions test/job-store/mongodb.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe } from "bun:test";
import { afterAll, afterEach, beforeAll, describe } from "bun:test";
import type { Connection, Model } from "mongoose";
import { createConnection } from "mongoose";
import type MongodbJobLock from "../../src/job-lock/mongodb";
Expand All @@ -14,17 +14,15 @@ describe("MongodbJobStore", () => {
beforeAll(async () => {
conn = createConnection(Bun.env.MONGO_URI!);
await waitUntil(() => conn.asPromise());

jobStore = new MongodbJobStore(conn);
model = conn.models.JobLock;
});

afterAll(async () => {
await jobStore.close();
});

beforeEach(() => {
jobStore = new MongodbJobStore(conn);
model = conn.models.JobLock;
});

afterEach(async () => {
await model.deleteMany({});
});
Expand Down
8 changes: 3 additions & 5 deletions test/job-store/redis.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe } from "bun:test";
import { afterAll, afterEach, beforeAll, describe } from "bun:test";
import { createClient } from "redis";
import RedisJobStore from "../../src/job-store/redis";
import { waitUntil } from "../helper";
Expand All @@ -13,16 +13,14 @@ describe("RedisJobStore", () => {
beforeAll(async () => {
client = createClient({ url: Bun.env.REDIS_URI });
await waitUntil(() => client.connect());

jobStore = new RedisJobStore(client);
});

afterAll(async () => {
await jobStore.close();
});

beforeEach(() => {
jobStore = new RedisJobStore(client);
});

afterEach(async () => {
await client.flushAll();
});
Expand Down
3 changes: 3 additions & 0 deletions test/job-store/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export function testBehavesLikeJobStore<I>(getJobStore: () => BaseJobStore<I>) {

beforeEach(() => {
jobStore = getJobStore();
});

beforeEach(() => {
retryIntervalStartedAt = subMilliseconds(new Date(), retryInterval);
});

Expand Down
10 changes: 4 additions & 6 deletions test/job-store/typeorm.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe } from "bun:test";
import { afterAll, afterEach, beforeAll, describe } from "bun:test";
import { DataSource } from "typeorm";
import type { Repository } from "typeorm";
import type TypeormJobLock from "../../src/job-lock/typeorm";
Expand Down Expand Up @@ -35,17 +35,15 @@ describe.each([

beforeAll(async () => {
await waitUntil(() => dataSource.initialize());

jobStore = new JobStore(dataSource);
repository = dataSource.getRepository<TypeormJobLock>(TypeormJobLockEntity);
});

afterAll(async () => {
await jobStore.close();
});

beforeEach(() => {
jobStore = new JobStore(dataSource);
repository = dataSource.getRepository<TypeormJobLock>(TypeormJobLockEntity);
});

afterEach(async () => {
await repository.delete({});
});
Expand Down

0 comments on commit 3886536

Please sign in to comment.