Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
527 changes: 527 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dotenv": "^16.3.1",
"eslint": "^9.39.2",
"eslint-plugin-import": "^2.32.0",
"nock": "^13.4.0",
"msw": "^2.12.11",
"typedoc": "^0.28.14",
"typedoc-plugin-markdown": "^4.9.0",
"typescript": "^5.3.2",
Expand Down
47 changes: 47 additions & 0 deletions tests/mocks/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* MSW (Mock Service Worker) server for unit tests.
*
* ## How to add new handlers
*
* Call `server.use()` inside a test to register per-test handlers.
* They are automatically removed after each test by the global `afterEach`
* in `tests/setup.js` (via `server.resetHandlers()`).
*
* ```ts
* import { http, HttpResponse } from 'msw';
* import { server } from '../mocks/server';
*
* test('my test', async () => {
* server.use(
* http.get('https://api.base44.com/api/apps/test-app-id/entities/Todo', () =>
* HttpResponse.json([{ id: '1', title: 'Test' }])
* )
* );
* // ... test code
* });
* ```
*
* ## Architecture
*
* ```
* Vitest test → SDK (axios / fetch) → MSW Node server → handler → fake response
* ```
*
* MSW intercepts requests at the Node.js http layer (`@mswjs/interceptors`)
* and also intercepts native `fetch` calls. No axios mocking or `vi.stubGlobal`
* needed.
*
* ## Modules and their base URL patterns
*
* | Module | Base path |
* |--------------|------------------------------------------------------------------|
* | entities | `/api/apps/:appId/entities/:entityName` |
* | auth | `/api/apps/:appId/entities/User/me`, `/api/apps/:appId/auth/...` |
* | functions | `/api/apps/:appId/functions/:name`, `/api/functions/:name` |
* | integrations | `/api/apps/:appId/integration-endpoints/:pkg/:endpoint` |
* | custom-int | `/api/apps/:appId/integrations/custom/:slug/:operationId` |
* | connectors | `/api/apps/:appId/external-auth/tokens/:type` |
*/
import { setupServer } from 'msw/node';

export const server = setupServer();
16 changes: 11 additions & 5 deletions tests/setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Load environment variables from .env file
import dotenv from 'dotenv';
import './utils/circular-json-handler.js';
import { beforeAll, afterAll, test } from 'vitest';
import { beforeAll, afterAll, afterEach } from 'vitest';
import { server } from './mocks/server.ts';

try {
dotenv.config({ path: './tests/.env' });
Expand All @@ -16,13 +17,18 @@ try {
console.warn('Failed to load circular JSON handler:', err.message);
}

// Global beforeAll and afterAll hooks
// MSW server lifecycle
beforeAll(() => {
server.listen({ onUnhandledRequest: 'warn' });
console.log('Starting Base44 SDK tests...');
// Add any global setup here
});

afterEach(() => {
// Remove per-test handlers registered via server.use()
server.resetHandlers();
});

afterAll(() => {
server.close();
console.log('Completed Base44 SDK tests');
// Add any global teardown here
});
});
Loading
Loading