Skip to content

Commit 779c881

Browse files
fix: fix api test (#587)
* fix: fix api test * fix: fix api test result * Create flat-apes-help.md
1 parent d9fd2eb commit 779c881

23 files changed

+231
-174
lines changed

Diff for: .changeset/flat-apes-help.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@codeimage/api": patch
3+
---
4+
5+
fix: fix api test

Diff for: .github/workflows/main.yml

+25-22
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ jobs:
1414
steps:
1515
- name: Checkout PR
1616
uses: actions/checkout@v3
17-
# with:
18-
# submodules: 'true'
17+
# with:
18+
# submodules: 'true'
1919
- name: Use Node.js ${{ matrix.node-version }}
2020
uses: actions/setup-node@v3
2121
with:
@@ -58,8 +58,8 @@ jobs:
5858
steps:
5959
- name: Checkout PR
6060
uses: actions/checkout@v3
61-
# with:
62-
# submodules: 'true'
61+
# with:
62+
# submodules: 'true'
6363
- name: Use Node.js ${{ matrix.node-version }}
6464
uses: actions/setup-node@v3
6565
with:
@@ -83,8 +83,8 @@ jobs:
8383

8484
steps:
8585
- uses: actions/checkout@v3
86-
# with:
87-
# submodules: 'true'
86+
# with:
87+
# submodules: 'true'
8888
- name: Use Node.js ${{ matrix.node-version }}
8989
uses: actions/setup-node@v3
9090
with:
@@ -115,8 +115,8 @@ jobs:
115115
runs-on: ubuntu-latest
116116
steps:
117117
- uses: actions/checkout@v3
118-
# with:
119-
# submodules: 'true'
118+
# with:
119+
# submodules: 'true'
120120
- name: Use Node.js ${{ matrix.node-version }}
121121
uses: actions/setup-node@v3
122122
with:
@@ -155,8 +155,8 @@ jobs:
155155
steps:
156156
- name: Checkout PR
157157
uses: actions/checkout@v3
158-
# with:
159-
# submodules: 'true'
158+
# with:
159+
# submodules: 'true'
160160
- name: Use Node.js ${{ matrix.node-version }}
161161
uses: actions/setup-node@v3
162162
with:
@@ -209,7 +209,7 @@ jobs:
209209
uses: actions/checkout@v3
210210
with:
211211
ref: 'refs/pull/${{ github.event.number }}/merge'
212-
# submodules: 'true'
212+
# submodules: 'true'
213213
- name: Use Node.js ${{ matrix.node-version }}
214214
uses: actions/setup-node@v3
215215
with:
@@ -262,7 +262,7 @@ jobs:
262262
uses: actions/checkout@v3
263263
with:
264264
submodules: 'true'
265-
# ref: 'refs/pull/${{ github.event.number }}/merge'
265+
# ref: 'refs/pull/${{ github.event.number }}/merge'
266266
- name: Use Node.js ${{ matrix.node-version }}
267267
uses: actions/setup-node@v3
268268
with:
@@ -305,7 +305,7 @@ jobs:
305305
uses: actions/checkout@v3
306306
with:
307307
ref: 'refs/pull/${{ github.event.number }}/merge'
308-
# submodules: 'true'
308+
# submodules: 'true'
309309
- name: Use Node.js ${{ matrix.node-version }}
310310
uses: actions/setup-node@v3
311311
with:
@@ -355,7 +355,7 @@ jobs:
355355
uses: actions/checkout@v3
356356
with:
357357
ref: 'refs/pull/${{ github.event.number }}/merge'
358-
# submodules: 'true'
358+
# submodules: 'true'
359359
- name: Use Node.js ${{ matrix.node-version }}
360360
uses: actions/setup-node@v3
361361
with:
@@ -383,8 +383,8 @@ jobs:
383383

384384
steps:
385385
- uses: actions/checkout@v3
386-
# with:
387-
# submodules: 'true'
386+
# with:
387+
# submodules: 'true'
388388
- name: Use Node.js ${{ matrix.node-version }}
389389
uses: actions/setup-node@v3
390390
with:
@@ -445,8 +445,8 @@ jobs:
445445

446446
steps:
447447
- uses: actions/checkout@v3
448-
# with:
449-
# submodules: 'true'
448+
# with:
449+
# submodules: 'true'
450450
- name: 📥 Monorepo install
451451
uses: ./.github/actions/pnpm-install
452452

@@ -464,8 +464,11 @@ jobs:
464464
pnpm exec prisma generate
465465
pnpm --filter=@codeimage/prisma-models build
466466
467-
- name: Test
468-
run: pnpm --filter=@codeimage/api test:ci
467+
- name: Test (unit)
468+
run: pnpm --filter=@codeimage/api test:unit
469+
470+
- name: Test (integration)
471+
run: pnpm --filter=@codeimage/api test:integration
469472

470473
deploy-api-next:
471474
if: ${{ github.head_ref == 'next' }}
@@ -481,8 +484,8 @@ jobs:
481484
runs-on: ubuntu-latest
482485
steps:
483486
- uses: actions/checkout@v3
484-
# with:
485-
# submodules: 'true'
487+
# with:
488+
# submodules: 'true'
486489
- name: Use Node.js ${{ matrix.node-version }}
487490
uses: actions/setup-node@v3
488491
with:

Diff for: .github/workflows/prod-deploy.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,11 @@ jobs:
166166
pnpm exec prisma generate
167167
pnpm --filter=@codeimage/prisma-models build
168168
169-
- name: Test
170-
run: pnpm --filter=@codeimage/api test:ci
169+
- name: Test (unit)
170+
run: pnpm --filter=@codeimage/api test:unit
171+
172+
- name: Test (integration)
173+
run: pnpm --filter=@codeimage/api test:integration
171174

172175
typecheck-packages:
173176
name: Type-checking packages/**

Diff for: .idea/vcs.xml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: apps/api/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"scripts": {
1616
"test": "vitest",
1717
"test:coverage": "vitest --coverage",
18-
"test:ci": "vitest run",
18+
"test:unit": "RUN_TEST=unit vitest run",
19+
"test:integration": "RUN_TEST=integration vitest run",
1920
"test:ui": "vitest --ui",
2021
"start": "npm run build:ts && fastify start -l info dist/app.js",
2122
"start:railway": "fastify start -l info dist/app.js",
@@ -29,6 +30,7 @@
2930
"prisma:migrate:test": "dotenv -e .env.test -- pnpm exec prisma migrate dev",
3031
"prisma:migrate:deploy": "prisma migrate deploy",
3132
"prisma:migrate:deploy-test": "dotenv -e .env.test -- pnpm exec prisma migrate deploy",
33+
"prisma:migrate:reset-test": "dotenv -e .env.test -- pnpm exec prisma migrate reset --force",
3234
"prisma:generate": "prisma generate dev && pnpm --filter=@codeimage/prisma-models build",
3335
"docker:dev": "docker compose -f docker-compose.dev.yml up",
3436
"dev:start": "tsx watch node_modules/fastify-cli/cli.js start --ignore-watch=.ts$ -w -l info -P src/app.ts"

Diff for: apps/api/test/before-test.ts

-17
This file was deleted.

Diff for: apps/api/test/helper.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This file contains code that we reuse between our tests.
2-
import Fastify from 'fastify';
2+
import Fastify, {FastifyInstance} from 'fastify';
33
import fp from 'fastify-plugin';
4-
import {Test} from 'vitest';
4+
import {TestContext} from 'vitest';
55
import App from '../src/app.js';
66
import {auth0Mock} from './helpers/auth0Mock.js';
77

@@ -14,17 +14,27 @@ async function config(t: any) {
1414
}
1515

1616
// Automatically build and tear down our instance
17-
async function build(t: Test) {
17+
async function build(t: TestContext) {
1818
const app = Fastify();
1919

2020
// fastify-plugin ensures that all decorators
2121
// are exposed for testing purposes, this is
2222
// different from the production setup
23-
void app.register(fp(App), await config(t));
23+
await app.register(fp(App), await config(t));
2424

2525
await app.ready();
26-
2726
return app;
2827
}
2928

29+
export function withFastifyApp<T>(
30+
test: (context: TestContext & T, fastify: FastifyInstance) => Promise<void>,
31+
configFn: (a: any) => Promise<any> = config,
32+
) {
33+
return async (context: TestContext & T) => {
34+
const app = await build(await configFn(context));
35+
await test(context, app);
36+
await app.close();
37+
};
38+
}
39+
3040
export {config, build};

Diff for: apps/api/test/helpers/seed.ts

+23-9
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@ import {PrismaClient} from '@codeimage/prisma-models';
22
import * as crypto from 'crypto';
33
import {testPresetUtils} from '../__internal__/presetUtils.js';
44

5-
const client = new PrismaClient();
5+
export const client = new PrismaClient({
6+
datasources: {
7+
db: {url: import.meta.env['DATABASE_URL']},
8+
},
9+
});
10+
611
export const userSeed = {
7-
clean: () => client.user.deleteMany().then(),
8-
createUser(email = `email-${crypto.randomUUID()}@example.it`) {
12+
clean: async () => await client.user.deleteMany(),
13+
async createUser(email?: string) {
14+
const id = crypto.randomUUID();
915
return client.user.create({
1016
data: {
11-
email: email,
17+
id,
18+
email: email || `email-${id}@example.it`,
1219
},
1320
});
1421
},
1522
};
1623

1724
export const presetSeed = {
18-
clean: () => client.preset.deleteMany().then(),
19-
createPresetV1(presetName: string, ownerId: string, data?: object) {
25+
clean: async () => await client.preset.deleteMany(),
26+
async createPresetV1(presetName: string, ownerId: string, data?: object) {
2027
return client.preset.create({
2128
data: {
2229
name: presetName,
@@ -28,9 +35,14 @@ export const presetSeed = {
2835
},
2936
};
3037

38+
export const clearAllSeeds = async () => {
39+
await projectSeed.clean();
40+
await presetSeed.clean();
41+
await userSeed.clean();
42+
};
3143
export const projectSeed = {
32-
clean: () => client.project.deleteMany().then(),
33-
createProject(projectName: string, ownerId: string) {
44+
clean: async () => await client.project.deleteMany(),
45+
async createProject(projectName: string, ownerId: string) {
3446
return client.project.create({
3547
data: {
3648
name: projectName,
@@ -51,7 +63,9 @@ export const projectSeed = {
5163
themeId: 'themeId',
5264
},
5365
},
54-
owner: {connect: {id: ownerId}},
66+
owner: {
67+
connect: {id: ownerId},
68+
},
5569
},
5670
include: {
5771
owner: true,

Diff for: apps/api/test/routes/v1/preset/create.integration.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {User} from '@codeimage/prisma-models';
22
import * as sinon from 'sinon';
3-
import {afterEach, assert, beforeAll, beforeEach, test} from 'vitest';
3+
import {afterEach, assert, beforeEach, test} from 'vitest';
44
import {PresetCreateDto} from '../../../../src/modules/preset/schema/preset-create-dto.schema.js';
55
import {PresetDto} from '../../../../src/modules/preset/schema/preset-dto.schema.js';
66
import {testPresetUtils} from '../../../__internal__/presetUtils.js';
77
import {build} from '../../../helper.js';
8-
import {userSeed} from '../../../helpers/seed.js';
8+
import {clearAllSeeds, userSeed} from '../../../helpers/seed.js';
99

1010
interface TestContext {
1111
user: User;
@@ -18,7 +18,7 @@ beforeEach<TestContext>(async context => {
1818
});
1919

2020
afterEach(async () => {
21-
await userSeed.clean();
21+
await clearAllSeeds();
2222
});
2323

2424
test<TestContext>('POST /v1/preset [Create Preset] -> 200', async t => {
+5-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import {Preset, User} from '@codeimage/prisma-models';
2-
import * as sinon from 'sinon';
3-
import {afterEach, assert, beforeEach, test} from 'vitest';
2+
import {afterEach, assert, beforeEach, expect, test, vi} from 'vitest';
43

54
import {build} from '../../../helper.js';
6-
import {presetSeed, userSeed} from '../../../helpers/seed.js';
5+
import {clearAllSeeds, presetSeed, userSeed} from '../../../helpers/seed.js';
76

87
interface TestContext {
98
user: User;
109
preset1: Preset;
1110
}
1211

13-
beforeEach(() => sinon.restore());
14-
1512
beforeEach<TestContext>(async context => {
1613
const user = await userSeed.createUser();
1714
const preset1 = await presetSeed.createPresetV1('preset-1', user.id);
@@ -20,20 +17,20 @@ beforeEach<TestContext>(async context => {
2017
});
2118

2219
afterEach(async () => {
23-
await Promise.all([userSeed.clean(), presetSeed.clean()]);
20+
await clearAllSeeds();
2421
});
2522

2623
test<TestContext>('DELETE /v1/project/:id [Delete Project] -> 200', async t => {
2724
const fastify = await build(t);
2825
const userId = t.user.id;
2926
const presetId = t.preset1.id;
30-
const spy = sinon.spy(fastify.presetService, 'deletePreset');
27+
const spy = vi.spyOn(fastify.presetService, 'deletePreset');
3128

3229
const response = await fastify.inject({
3330
url: `/api/v1/preset/${presetId}`,
3431
method: 'DELETE',
3532
});
3633

3734
assert.equal(response.statusCode, 200, 'return status 200');
38-
assert.ok(spy.withArgs(userId, presetId).calledOnce, 'has been called once');
35+
expect(spy).toHaveBeenCalledWith(userId, presetId);
3936
});

0 commit comments

Comments
 (0)