Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/hono #1

Merged
merged 20 commits into from
Mar 11, 2024
Merged
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
26 changes: 26 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
on: [pull_request]

jobs:
build:
runs-on: ubuntu-latest
name: Mock Mirror

steps:
- name: Checkout
uses: actions/checkout@v4

- uses: oven-sh/setup-bun@v1
with:
bun-version: latest

- name: Install dependencies
run: bun install

- name: Lint
run: bun run lint

- name: Test
run: bun run test

- name: Build
run: bun run build
1 change: 0 additions & 1 deletion build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import dts from 'bun-plugin-dts';

await Bun.build({
Expand Down
Binary file modified bun.lockb
Binary file not shown.
15 changes: 10 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
],
"types": "dist/src/client.d.ts",
"scripts": {
"test": "bun test",
"test": "bun test src/*.test.ts",
"lint": "xo",
"build": "bunx tsc",
"dev": "bun run --watch src/index.ts",
"dev": "bun run --hot src/index.ts",
"release": "bunx np"
},
"dependencies": {
"@elysiajs/eden": "0.8.1",
"elysia": "0.8.17",
"@hono/zod-validator": "0.2.0",
"hono": "4.0.10",
"isomorphic-fetch": "3.0.0",
"minimatch": "9.0.3",
"pino": "8.19.0",
"pino-pretty": "10.3.1"
"pino-pretty": "10.3.1",
"zod": "3.22.4"
},
"devDependencies": {
"@types/isomorphic-fetch": "0.0.39",
"bun-plugin-dts": "^0.2.1",
"bun-types": "latest",
"prettier": "^3.2.5",
Expand All @@ -43,6 +46,8 @@
"@typescript-eslint/object-curly-spacing": "off",
"@typescript-eslint/naming-convention": "off",
"new-cap": "off",
"unicorn/prevent-abbreviations": "off",
"import/no-anonymous-default-export": "off",
"import/extensions": [
"error",
"ignorePackages",
Expand Down
6 changes: 5 additions & 1 deletion src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ exports[`server should respond with defined delay: add mock route 1`] = `
}
`;

exports[`server should respond with defined delay: mock response 1`] = `"user will be created"`;
exports[`server should respond with defined delay: mock response 1`] = `
{
"message": "user will be created",
}
`;

exports[`server should respond with default scope if no scope is provided: add mock route 1`] = `
{
Expand Down
85 changes: 50 additions & 35 deletions src/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
import { beforeEach, describe, expect, it } from 'bun:test';
import { Elysia } from 'elysia';
import { Hono } from 'hono';
import { testClient } from 'hono/testing';
import type { StatusCode } from 'hono/utils/http-status';
import { MOCK_MIRROR_HEADER } from './const';
import { createMockMirror } from './client';
import { app } from '.';
import server from '.';

const serverUrl = `http://localhost:${app.server?.port}`;
const serverUrl = `http://localhost:3211`;

const mockMirror = await createMockMirror({ mockMirrorUrl: serverUrl });
Bun.serve({
...server,
port: 3211,
});

const mockMirror = createMockMirror({
mockMirrorUrl: serverUrl,
});

const testServiceUrl = 'http://this-service-does-not-exist.local';

const testBackend = new Elysia()
.derive(({ headers: originalHeaders }) => {
const headers = originalHeaders as Record<string, string>;
const testBackend = new Hono().get('/api/users/:id', async (ctx) => {
const headers = ctx.req.raw.headers;
const hasMockMirrorScope = ctx.req.header(MOCK_MIRROR_HEADER);

const api = headers[MOCK_MIRROR_HEADER]
? async (url: string) => fetch(`${serverUrl}${url}`, { headers }) // This is the mock
: async (url: string) => fetch(`${testServiceUrl}${url}`, { headers }); // This would be the actual service, but we're mocking it
const api = hasMockMirrorScope
? async (url: string) => fetch(`${serverUrl}${url}`, { headers }) // This is the mock
: async (url: string) => fetch(`${testServiceUrl}${url}`, { headers }); // This would be the actual service, but we're mocking it

return {
api,
};
})
.get('/api/users/:id', async ({ params, api }) => {
return api(`/api/users/${params.id}`);
})
.listen(9898);
const result = await api(`/api/users/${ctx.req.param('id')}`);

const testBackendUrl = `http://${testBackend.server?.hostname}:${testBackend.server?.port}`;
return ctx.body(await result.text(), result.status as StatusCode);
});

describe('client', () => {
beforeEach(async () => {
Expand All @@ -36,11 +39,14 @@ describe('client', () => {

it('should fail if mocks are not provided', async () => {
await mockMirror(async ({ scope }) => {
const response = await fetch(`${testBackendUrl}/api/users/777`, {
headers: {
[MOCK_MIRROR_HEADER]: scope,
const response = await testClient(testBackend).api.users[':id'].$get(
{ param: { id: '777' } },
{
headers: {
[MOCK_MIRROR_HEADER]: scope,
},
},
});
);

expect(response.status).toBe(404);
});
Expand All @@ -53,11 +59,14 @@ describe('client', () => {
response: 'This is a mock response for the user',
});

const response = await fetch(`${testBackendUrl}/api/users/777`, {
headers: {
[MOCK_MIRROR_HEADER]: scope,
const response = await testClient(testBackend).api.users[':id'].$get(
{ param: { id: '777' } },
{
headers: {
[MOCK_MIRROR_HEADER]: scope,
},
},
});
);

expect(await response.text()).toBe('This is a mock response for the user');
});
Expand All @@ -66,11 +75,14 @@ describe('client', () => {
it('should be able to mock the service behind the backend on the go', async () => {
await mockMirror(async ({ addRoute, scope }) => {
{
const response = await fetch(`${testBackendUrl}/api/users/777`, {
headers: {
[MOCK_MIRROR_HEADER]: scope,
const response = await testClient(testBackend).api.users[':id'].$get(
{ param: { id: '777' } },
{
headers: {
[MOCK_MIRROR_HEADER]: scope,
},
},
});
);

expect(response.status).toBe(404);
}
Expand All @@ -81,11 +93,14 @@ describe('client', () => {
response: 'This is a mock response for the user',
});

const response = await fetch(`${testBackendUrl}/api/users/777`, {
headers: {
[MOCK_MIRROR_HEADER]: scope,
const response = await testClient(testBackend).api.users[':id'].$get(
{ param: { id: '777' } },
{
headers: {
[MOCK_MIRROR_HEADER]: scope,
},
},
});
);

expect(await response.text()).toBe('This is a mock response for the user');
}
Expand Down
33 changes: 23 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,62 @@
import { randomUUID } from 'node:crypto';
import { edenTreaty } from '@elysiajs/eden';
import type { ClientRequestOptions } from 'hono';
import fetch from 'isomorphic-fetch';
import { hc } from 'hono/client';
import type { MockedRoute, MockedRoutes } from './types';
import type { app } from '.';
import type { App } from '.';

export type { MockedRoute, MockedRoutes } from './types';

export const createMockMirror = ({
mockMirrorUrl,
defaultRoutes,
options,
}: {
mockMirrorUrl?: string;
defaultRoutes?: MockedRoutes;
options?: ClientRequestOptions;
}) => {
const api = edenTreaty<typeof app>(mockMirrorUrl ?? Bun.env.MOCK_MIRROR_URL ?? 'http://localhost:3210', {});
const client = hc<App>(mockMirrorUrl ?? Bun.env.MOCK_MIRROR_URL ?? 'http://localhost:3210', {
...options,
fetch: options?.fetch ?? fetch,
});

if (defaultRoutes) {
void api['mock-mirror'].add.post({ routes: defaultRoutes });
void client['mock-mirror'].add.$post({ json: { routes: defaultRoutes } });
}

const getTools = ({ scope }: { scope: string }) => ({
async addRoute(route: MockedRoute) {
return api['mock-mirror'].add.post({ scope, routes: [route] });
return client['mock-mirror'].add.$post({ json: { scope, routes: [route] } });
},
async addRoutes(routes: MockedRoutes) {
return api['mock-mirror'].add.post({ scope, routes });
return client['mock-mirror'].add.$post({ json: { scope, routes } });
},
async clearScope() {
return api['mock-mirror']['clear-scope'].post({ scope });
return client['mock-mirror']['clear-scope'].$post({ json: { scope } });
},
async reset() {
return api['mock-mirror'].reset.post();
return client['mock-mirror'].reset.$post();
},
scope,
});

return (
return async (
integrationTest: (tools: ReturnType<typeof getTools>) => unknown,
options?: {
scope?: string;
},
) => {
const scope = options?.scope ?? randomUUID();

return integrationTest(
const testResults = await integrationTest(
getTools({
scope,
}),
);

void client['mock-mirror']['clear-scope'].$post({ json: { scope } });

return testResults;
};
};
Loading