Skip to content

TECH: Mock Service Worker (MSW) v2 #997

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
857 changes: 851 additions & 6 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"config": "3.3.9",
"cross-env": "7.0.3",
"jest": "29.6.2",
"msw": "2.6.8",
"nanoid": "3.3.4",
"oclif": "3.7.3",
"simple-git-hooks": "2.11.1",
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { http, HttpHandler, HttpResponse } from 'msw'
import alertChannelsJson from './json/alert-channels.json'

export const handlers: HttpHandler[] = [
http.get('https://api.checklyhq.com/v1/alert-channels', () => {
return HttpResponse.json(alertChannelsJson)
}),
]
64 changes: 64 additions & 0 deletions packages/cli/src/mocks/json/alert-channels.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[
{
"id": 240783,
"type": "SMS",
"config": {
"number": "+35612345678"
},
"created_at": "2024-12-02T10:18:24.012Z",
"updated_at": "2024-12-05T10:54:20.983Z",
"sendRecovery": false,
"sendFailure": true,
"sendDegraded": false,
"sslExpiry": true,
"sslExpiryThreshold": 25,
"autoSubscribe": false,
"subscriptions": [
{
"id": 12799454,
"checkId": "dde39bfb-f85a-4546-84d7-4085623dc4c9",
"activated": true,
"groupId": null
}
]
},
{
"id": 240784,
"type": "EMAIL",
"config": {
"address": "[email protected]"
},
"created_at": "2024-12-02T10:18:24.020Z",
"updated_at": "2024-12-05T10:54:20.988Z",
"sendRecovery": false,
"sendFailure": true,
"sendDegraded": false,
"sslExpiry": true,
"sslExpiryThreshold": 25,
"autoSubscribe": false,
"subscriptions": [
{
"id": 12799455,
"checkId": "dde39bfb-f85a-4546-84d7-4085623dc4c9",
"activated": true,
"groupId": null
}
]
},
{
"id": 240593,
"type": "EMAIL",
"config": {
"address": "[email protected]"
},
"created_at": "2024-11-28T17:24:33.252Z",
"updated_at": null,
"sendRecovery": true,
"sendFailure": true,
"sendDegraded": false,
"sslExpiry": false,
"sslExpiryThreshold": 30,
"autoSubscribe": false,
"subscriptions": []
}
]
4 changes: 4 additions & 0 deletions packages/cli/src/mocks/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { setupServer } from 'msw/node'
import { handlers } from './handlers'

export const server = setupServer(...handlers)
23 changes: 23 additions & 0 deletions packages/cli/src/rest/__tests__/alert-channels.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* TODO: add path aliases */
import { server } from '../../mocks/server'
import * as api from '../api'
import alertChannelsJson from '../../mocks/json/alert-channels.json'

describe('AlertChannels API REST', () => {
beforeAll(() => {
server.listen()
})

afterEach(() => {
server.resetHandlers()
})

afterAll(() => {
server.close()
})

it('can get data', async () => {
const { data } = await api.alertChannels.getAll()
expect(data).toEqual(alertChannelsJson)
})
})
39 changes: 39 additions & 0 deletions packages/cli/src/rest/alert-channels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { AxiosInstance } from 'axios'

interface Subscription {
id: number;
checkId: string;
activated: boolean;
groupId: string | null;
}

export interface AlertChannelApi {
id: number;
type: string;
config: {
number?: string;
address?: string;
};
created_at: string;
updated_at: string | null;
sendRecovery: boolean;
sendFailure: boolean;
sendDegraded: boolean;
sslExpiry: boolean;
sslExpiryThreshold: number;
autoSubscribe: boolean;
subscriptions: Subscription[];
}

class AlertChannels {
protected api: AxiosInstance
constructor (api: AxiosInstance) {
this.api = api
}

getAll () {
return this.api.get<AlertChannelApi[]>('/v1/alert-channels')
}
}

export default AlertChannels
2 changes: 2 additions & 0 deletions packages/cli/src/rest/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import TestSessions from './test-sessions'
import EnvironmentVariables from './environment-variables'
import HeartbeatChecks from './heartbeat-checks'
import ChecklyStorage from './checkly-storage'
import AlertChannels from './alert-channels';

export function getDefaults () {
const apiKey = config.getApiKey()
Expand Down Expand Up @@ -100,3 +101,4 @@ export const testSessions = new TestSessions(api)
export const environmentVariables = new EnvironmentVariables(api)
export const heartbeatCheck = new HeartbeatChecks(api)
export const checklyStorage = new ChecklyStorage(api)
export const alertChannels = new AlertChannels(api)
4 changes: 2 additions & 2 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
"rootDirs": ["src", "e2e"],
"strict": true,
"target": "es2019",
"sourceMap": true
"sourceMap": true,
"resolveJsonModule": true
},
"include": [
"src/**/*"
],
"exclude": [
"src/**/__tests__",
"e2e"
]
}