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

Refactor: Upgrade MSW to Version 2 #1254

Merged
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
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 @@
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 @@
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 @@
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 @@
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
Loading