🌐 AI搜索 & 代理 主页
Skip to content

Commit 8ef3fa3

Browse files
committed
run standalone mock server in e2e tests
update e2e tests fix e2e script fix e2e script update docs fix .env.example
1 parent e19da4d commit 8ef3fa3

File tree

9 files changed

+586
-41
lines changed

9 files changed

+586
-41
lines changed

.env.example

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
VITE_APP_API_URL=http://localhost:8080/api
2-
VITE_APP_ENABLE_API_MOCKING=true
3-
4-
5-
# If you want to run standalone mock server, you can use the following configuration:
6-
# VITE_APP_ENABLE_API_MOCKING=false
7-
# VITE_APP_MOCK_API_PORT=8080
8-
# VITE_APP_URL=http://localhost:3000
1+
VITE_APP_API_URL=https://api.bulletproofapp.com
2+
VITE_APP_ENABLE_API_MOCKING=true

.env.example-e2e

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
VITE_APP_API_URL=http://localhost:8080/api
2+
VITE_APP_ENABLE_API_MOCKING=false
3+
VITE_APP_MOCK_API_PORT=8080
4+
VITE_APP_URL=http://localhost:3000

.github/workflows/ci.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,18 @@ jobs:
3939
with:
4040
node-version: lts/*
4141
- name: Set environment variables
42-
run: mv .env.example .env
42+
run: mv .env.example-e2e .env
4343
- name: Install dependencies
4444
run: npm install -g yarn && yarn
4545
- name: Install Playwright Browsers
4646
run: yarn playwright install --with-deps
4747
- name: Run Playwright tests
48-
run: yarn playwright test
48+
run: yarn test-e2e
4949
- uses: actions/upload-artifact@v4
5050
if: always()
5151
with:
5252
name: playwright-report
53-
path: playwright-report/
53+
path: |
54+
playwright-report/
55+
mocked-db.json
5456
retention-days: 30

docs/testing.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ Integration testing checks how different parts of your application work together
2020

2121
End-to-End Testing is a method that evaluates an application as a whole. These tests involve automating the complete application, including both the frontend and backend, to confirm that the entire system functions correctly. End-to-End tests simulate how a user would interact with the application.
2222

23-
NOTE: In the sample app, the tests are ran against the mocked API server, so they are technically not fully e2e, but they are written in the same way as they would be if they were ran against the real API.
24-
2523
[E2E Example Code](../e2e/tests/smoke.spec.ts)
2624

2725
## Recommended Tooling:

mock-server.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import express from 'express';
44
import logger from 'pino-http';
55

66
import { env } from './src/config/env';
7+
import { initializeDb } from './src/testing/mocks/db';
78
import { handlers } from './src/testing/mocks/handlers';
89

910
const app = express();
@@ -19,8 +20,11 @@ app.use(express.json());
1920
app.use(logger());
2021
app.use(createMiddleware(...handlers));
2122

22-
app.listen(env.APP_MOCK_API_PORT, () => {
23-
console.log(
24-
`Mock API server started at http://localhost:${env.APP_MOCK_API_PORT}`,
25-
);
23+
initializeDb().then(() => {
24+
console.log('Mock DB initialized');
25+
app.listen(env.APP_MOCK_API_PORT, () => {
26+
console.log(
27+
`Mock API server started at http://localhost:${env.APP_MOCK_API_PORT}`,
28+
);
29+
});
2630
});

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"build": "tsc && vite build --base=/",
99
"preview": "vite preview",
1010
"test": "vitest",
11+
"test-e2e": "pm2 start \"yarn run-mock-server\" --name server && yarn playwright test",
1112
"prepare": "husky",
1213
"lint": "eslint src --ignore-path .gitignore",
1314
"check-types": "tsc --project tsconfig.json --pretty --noEmit",
@@ -99,6 +100,7 @@
99100
"pino-http": "^10.1.0",
100101
"pino-pretty": "^11.1.0",
101102
"plop": "^4.0.1",
103+
"pm2": "^5.4.0",
102104
"postcss": "^8.4.38",
103105
"prettier": "^3.2.5",
104106
"storybook": "^8.0.9",

src/testing/mocks/db.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export const loadDb = async () => {
6666
);
6767
};
6868

69-
export const storeDb = async (key: string, data: string) => {
69+
export const storeDb = async (data: string) => {
7070
// If we are running in a Node.js environment
7171
if (typeof window === 'undefined') {
7272
const { writeFile } = await import('fs/promises');
@@ -81,7 +81,7 @@ export const persistDb = async (model: Model) => {
8181
if (process.env.NODE_ENV === 'test') return;
8282
const data = await loadDb();
8383
data[model] = db[model].getAll();
84-
await storeDb('msw-db', JSON.stringify(data));
84+
await storeDb(JSON.stringify(data));
8585
};
8686

8787
export const initializeDb = async () => {

vite.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export default defineConfig({
1111
server: {
1212
port: 3000,
1313
},
14+
preview: {
15+
port: 3000,
16+
},
1417
test: {
1518
globals: true,
1619
environment: 'jsdom',

0 commit comments

Comments
 (0)