Skip to content

Commit

Permalink
run standalone mock server in e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alan2207 committed Jun 6, 2024
1 parent e87c33c commit 7ed817e
Show file tree
Hide file tree
Showing 7 changed files with 583 additions and 38 deletions.
8 changes: 1 addition & 7 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
VITE_APP_API_URL=https://localhost:8080/api
VITE_APP_ENABLE_API_MOCKING=true


# If you want to run standalone mock server, you can use the following configuration:
# VITE_APP_ENABLE_API_MOCKING=false
# VITE_APP_MOCK_API_PORT=8080
# VITE_APP_URL=https://localhost:3000
VITE_APP_ENABLE_API_MOCKING=true
4 changes: 4 additions & 0 deletions .env.example-e2e
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
VITE_APP_API_URL=https://localhost:8080/api
VITE_APP_ENABLE_API_MOCKING=false
VITE_APP_MOCK_API_PORT=8080
VITE_APP_URL=https://localhost:3000
8 changes: 5 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ jobs:
with:
node-version: lts/*
- name: Set environment variables
run: mv .env.example .env
run: mv .env.example-e2e .env
- name: Install dependencies
run: npm install -g yarn && yarn
- name: Install Playwright Browsers
run: yarn playwright install --with-deps
- name: Run Playwright tests
run: yarn playwright test
run: yarn test-e2e-start
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
path: |
playwright-report/
mocked-db.json
retention-days: 30
12 changes: 8 additions & 4 deletions mock-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import express from 'express';
import logger from 'pino-http';

import { env } from './src/config/env';
import { initializeDb } from './src/testing/mocks/db';
import { handlers } from './src/testing/mocks/handlers';

const app = express();
Expand All @@ -19,8 +20,11 @@ app.use(express.json());
app.use(logger());
app.use(createMiddleware(...handlers));

app.listen(env.APP_MOCK_API_PORT, () => {
console.log(
`Mock API server started at https://localhost:${env.APP_MOCK_API_PORT}`,
);
initializeDb().then(() => {
console.log('Mock DB initialized');
app.listen(env.APP_MOCK_API_PORT, () => {
console.log(
`Mock API server started at https://localhost:${env.APP_MOCK_API_PORT}`,
);
});
});
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"build": "tsc && vite build --base=/",
"preview": "vite preview",
"test": "vitest",
"test-e2e-start": "pm2 start \"npm run run-mock-server\" --name server && pm2 start \"npm run dev\" --name client",
"test-e2e-stop": "pm2 stop server && pm2 stop client",
"prepare": "husky",
"lint": "eslint src --ignore-path .gitignore",
"check-types": "tsc --project tsconfig.json --pretty --noEmit",
Expand Down Expand Up @@ -99,6 +101,7 @@
"msw": "^2.2.14",
"pino-pretty": "^11.1.0",
"plop": "^4.0.1",
"pm2": "^5.4.0",
"postcss": "^8.4.38",
"prettier": "^3.2.5",
"storybook": "^8.0.9",
Expand Down
4 changes: 2 additions & 2 deletions src/testing/mocks/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const loadDb = async () => {
);
};

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

export const initializeDb = async () => {
Expand Down
Loading

0 comments on commit 7ed817e

Please sign in to comment.