Skip to content

Commit 70c2f56

Browse files
committed
feat: improve performance of tests and cleanup code
1 parent f023203 commit 70c2f56

File tree

8 files changed

+18
-27
lines changed

8 files changed

+18
-27
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"start": "next start",
99
"lint": "next lint",
1010
"test": "vitest run",
11-
"format": "prettier --write \"**/*.{css,js,json,jsx,ts,tsx}\""
11+
"format": "prettier --write \"**/*.{css,js,json,jsx,ts,tsx}\"",
12+
"reset": "prisma migrate reset"
1213
},
1314
"dependencies": {
1415
"@auth/prisma-adapter": "^2.4.1",

tests/auth/external-api-auth-error.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { prisma } from '@/lib/db';
21
import { IntegrationHarness } from '@/tests/utils/integration';
32
import { OasisTestContext } from '@/tests/utils/setup';
4-
import { afterAll, expect, test } from 'vitest';
3+
import { expect, test } from 'vitest';
54

65
test('EXTERNAL API AUTH ERROR', async (ctx: OasisTestContext) => {
76
ctx.apiToken = 'INVALID API TOKEN';
@@ -23,7 +22,3 @@ test('EXTERNAL API AUTH ERROR', async (ctx: OasisTestContext) => {
2322
expect(success).toBe(false);
2423
expect(error).toBe('Unauthorized: Invalid API Token');
2524
});
26-
27-
afterAll(async () => {
28-
await prisma.apiToken.deleteMany({});
29-
});

tests/auth/internal-api-auth-error.test.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { prisma } from '@/lib/db';
21
import { IntegrationHarness } from '@/tests/utils/integration';
32
import { OasisTestContext } from '@/tests/utils/setup';
4-
import { afterAll, expect, test } from 'vitest';
3+
import { expect, test } from 'vitest';
54

65
test('INTERNAL API AUTH ERROR', async (ctx: OasisTestContext) => {
76
const h = new IntegrationHarness(ctx);
@@ -21,7 +20,3 @@ test('INTERNAL API AUTH ERROR', async (ctx: OasisTestContext) => {
2120
expect(success).toBe(false);
2221
expect(error).toBe('Unauthorized');
2322
});
24-
25-
afterAll(async () => {
26-
await prisma.apiToken.deleteMany({});
27-
});

tests/bookmarks/create-bookmark.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,4 @@ test('POST /bookmarks', async (ctx: OasisTestContext) => {
3939

4040
afterAll(async () => {
4141
await prisma.bookmark.deleteMany({});
42-
await prisma.apiToken.deleteMany({});
4342
});

tests/bookmarks/get-bookmarks.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,4 @@ test('GET /bookmarks', async (ctx: OasisTestContext) => {
4747

4848
afterAll(async () => {
4949
await prisma.bookmark.deleteMany({});
50-
await prisma.apiToken.deleteMany({});
5150
});

tests/tokens/create-token.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { hashApiToken } from '@/lib/api/apiTokens/utils';
22
import { prisma } from '@/lib/db';
33
import { IntegrationHarness } from '@/tests/utils/integration';
44
import { OasisTestContext, getSetupData } from '@/tests/utils/setup';
5-
import { afterAll, expect, test } from 'vitest';
5+
import { expect, test } from 'vitest';
66

77
test('POST /tokens', async (ctx: OasisTestContext) => {
88
const setup = await getSetupData();
@@ -43,7 +43,3 @@ test('POST /tokens', async (ctx: OasisTestContext) => {
4343
}),
4444
);
4545
});
46-
47-
afterAll(async () => {
48-
await prisma.apiToken.deleteMany({});
49-
});

tests/utils/setup.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ export interface OasisTestContext extends TestContext {
99
apiToken: string;
1010
}
1111

12-
let apiToken: string;
13-
let user: User;
12+
declare global {
13+
var user: User;
14+
var apiToken: string;
15+
}
1416

1517
beforeAll(async () => {
18+
if (global.user && global.apiToken) return;
19+
1620
const apiUser = await prisma.user.upsert({
1721
where: {
1822
@@ -23,23 +27,24 @@ beforeAll(async () => {
2327
2428
},
2529
});
26-
user = apiUser;
30+
31+
global.user = apiUser;
2732

2833
const token = randomBytes(32).toString('hex');
2934
await prisma.apiToken.create({
3035
data: {
31-
userId: user.id,
36+
userId: apiUser.id,
3237
name: faker.lorem.word(),
3338
token: await hashApiToken(token),
3439
},
3540
});
3641

37-
apiToken = token;
42+
global.apiToken = token;
3843
});
3944

4045
export function getSetupData() {
4146
return {
42-
apiToken,
43-
user,
47+
apiToken: global.apiToken,
48+
user: global.user,
4449
};
4550
}

vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ export default defineConfig({
99
testTimeout: 50000,
1010
setupFiles: './tests/utils/setup.ts',
1111
fileParallelism: false,
12+
isolate: false,
1213
},
1314
});

0 commit comments

Comments
 (0)