Skip to content

Commit

Permalink
Refactor: Upgrade MSW to Version 2 (#1254)
Browse files Browse the repository at this point in the history
* refactor: upgrade msw dependency

* refactor: change rest to http

* refactor: change how to get json request body

* refactor: change how to response

* refactor: adjust the tests

* refactor: increment notification package version

* refactor: remove unnecessary codes
  • Loading branch information
haricnugraha authored Mar 18, 2024
1 parent ab0f741 commit f6ecb27
Show file tree
Hide file tree
Showing 12 changed files with 471 additions and 416 deletions.
1 change: 0 additions & 1 deletion .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
"require": ["ts-node/register"],
"bail": true,
"timeout": 5000,
"bail": true,
"exit": true
}
410 changes: 236 additions & 174 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@
"husky": "^4.3.8",
"lint-staged": "^10.5.4",
"mocha": "^9.1.2",
"msw": "^1.3.2",
"node-request-interceptor": "^0.6.3",
"msw": "^2.2.3",
"nyc": "^15.1.0",
"oclif": "4.0.2",
"pkg": "^5.8.1",
Expand Down
24 changes: 14 additions & 10 deletions packages/notification/channel/__test__/pagerduty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
**********************************************************************************/

import { expect } from 'chai'
import { rest } from 'msw'
import { type DefaultBodyType, HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
import type { NotificationMessage } from '../../channel'
import { send, validator } from '../pagerduty'
Expand Down Expand Up @@ -59,22 +59,26 @@ describe('PagerDuty notification', () => {
})

describe('send the event', () => {
let body = {}
let body: DefaultBodyType = {}
const server = setupServer(
rest.post(
http.post(
'https://events.pagerduty.com/v2/enqueue',
async (req, res, ctx) => {
body = await req.json()
async ({ request }) => {
body = await request.json()

return res(ctx.status(202))
return new HttpResponse(null, {
status: 202,
})
}
),
rest.post(
http.post(
'https://events.pagerduty.com/v2/enqueue',
async (req, res, ctx) => {
body = await req.json()
async ({ request }) => {
body = await request.json()

return res(ctx.status(202))
return new HttpResponse(null, {
status: 202,
})
}
)
)
Expand Down
12 changes: 7 additions & 5 deletions packages/notification/channel/__test__/webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
**********************************************************************************/

import { expect } from 'chai'
import { rest } from 'msw'
import { type DefaultBodyType, HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'

import { validateNotification } from '../../validator/notification'
Expand Down Expand Up @@ -75,12 +75,14 @@ describe('notificationChecker - webhookNotification', () => {

describe('Webhook Notification', () => {
describe('Send', () => {
let body = {}
let body: DefaultBodyType = {}
const server = setupServer(
rest.post('https://example.com', async (req, res, ctx) => {
body = await req.json()
http.post('https://example.com', async ({ request }) => {
body = await request.json()

return res(ctx.status(200))
return new HttpResponse(null, {
status: 200,
})
})
)

Expand Down
4 changes: 2 additions & 2 deletions packages/notification/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hyperjumptech/monika-notification",
"version": "1.16.0",
"version": "1.16.1",
"description": "notification package for monika",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down Expand Up @@ -30,7 +30,7 @@
"form-data": "^4.0.0",
"joi": "^17.4.0",
"mailgen": "^2.0.15",
"msw": "^1.3.2",
"msw": "^2.2.3",
"nodemailer": "6.9.4",
"pino": "8.14.1",
"sqlite": "4.2.1",
Expand Down
20 changes: 14 additions & 6 deletions src/components/probe/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
**********************************************************************************/

import { expect } from '@oclif/test'
import { rest } from 'msw'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
import sinon from 'sinon'
import mariadb from 'mariadb'
Expand All @@ -44,14 +44,22 @@ let notificationAlert: Record<
Record<string, Record<string, never>>
> = {}
const server = setupServer(
rest.get('https://example.com', (_, res, ctx) => res(ctx.status(200))),
rest.post('https://example.com/webhook', async (req, res, ctx) => {
const requestBody = await req.json()
http.get(
'https://example.com',
() =>
new HttpResponse(null, {
status: 200,
})
),
http.post('https://example.com/webhook', async ({ request }) => {
const requestBody = (await request.json()) as Record<string, any>

Check warning on line 55 in src/components/probe/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (requestBody?.body?.url) {
notificationAlert[requestBody.body.url] = requestBody
notificationAlert[requestBody?.body?.url] = requestBody
}

return res(ctx.status(200))
return new HttpResponse(null, {
status: 200,
})
})
)

Expand Down
28 changes: 18 additions & 10 deletions src/components/probe/prober/http/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import { expect } from '@oclif/test'
import { AxiosError } from 'axios'
import { rest } from 'msw'
import { HttpResponse, http } from 'msw'
import { setupServer } from 'msw/node'
import sinon from 'sinon'
import * as httpRequest from '../../../../utils/http'
Expand All @@ -42,17 +42,21 @@ let notificationAlert: Record<
Record<string, Record<string, never>>
> = {}
const server = setupServer(
rest.get('https://example.com', (_, res, ctx) => {
http.get('https://example.com', () => {
urlRequestTotal += 1
return res(ctx.status(200))
return new HttpResponse(null, {
status: 200,
})
}),
rest.post('https://example.com/webhook', async (req, res, ctx) => {
const requestBody = await req.json()
http.post('https://example.com/webhook', async ({ request }) => {
const requestBody = (await request.json()) as Record<string, any>

Check warning on line 52 in src/components/probe/prober/http/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
if (requestBody?.body?.url) {
notificationAlert[requestBody.body.url] = requestBody
}

return res(ctx.status(200))
return new HttpResponse(null, {
status: 200,
})
})
)
const probes: Probe[] = [
Expand Down Expand Up @@ -252,9 +256,11 @@ describe('HTTP Probe processing', () => {
it('should send incident notification when assertion fails', async () => {
// arrange
server.use(
rest.get('https://example.com', (_, res, ctx) => {
http.get('https://example.com', () => {
urlRequestTotal += 1
return res(ctx.status(404))
return new HttpResponse(null, {
status: 404,
})
})
)
const probe = {
Expand Down Expand Up @@ -305,9 +311,11 @@ describe('HTTP Probe processing', () => {
it('should send recovery notification', async () => {
// arrange
server.use(
rest.get('https://example.com', (_, res, ctx) => {
http.get('https://example.com', () => {
urlRequestTotal += 1
return res(ctx.status(404))
return new HttpResponse(null, {
status: 404,
})
})
)
const probe = {
Expand Down
Loading

0 comments on commit f6ecb27

Please sign in to comment.