Skip to content

Commit

Permalink
Test: Add tests for Monika configuration codes (#1279)
Browse files Browse the repository at this point in the history
* test: add tests for monika configuration

* revert: revert copy

* revert: log

* ci: fix code coverage uploader

* test: add production tests

* revert: do not validate with json schema in symon mode

* chore: remove unnecessary code

* revert: test:watch script
  • Loading branch information
haricnugraha authored Apr 26, 2024
1 parent 95e46b4 commit 29bcb9e
Show file tree
Hide file tree
Showing 49 changed files with 1,187 additions and 844 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ jobs:
node-version: 20.x
- run: npm ci
- run: npm run build -w packages/notification
- run: npm test && npx codecov
- run: npm test
- uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Cancel workflow on failure
if: 'failure()'
uses: 'andymckay/[email protected]'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"prepack": "npm run clean && tsc -b && oclif manifest",
"start": "npm run prepack && ./bin/dev.js",
"start:prod": "npm run prepack && ./bin/run.js",
"test": "npm run prepack && cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text --extension .ts mocha --config .mocharc.json --forbid-only \"{packages,src,test,workers}/**/*.test.ts\" --exclude test/cli.test.ts",
"test": "npm run prepack && cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text --extension .ts mocha --config .mocharc.json --forbid-only '{packages,src,test}/**/*.test.ts'",
"cli-test": "bats test/bats/cli.bats",
"test:watch": "npm run clean && NODE_ENV=test mocha {packages,src,test}/**/*.test.ts --watch --watch-files {packages,src,test}/**/*",
"prod_test": "npm run prod_test:bin && npm run prod_test:cli",
Expand Down
2 changes: 1 addition & 1 deletion packages/notification/channel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type NotificationChannel<T = object, U = object> = {
export type Notification = {
id: string
type: string
data: object | undefined
data?: object
}

export const channels: Record<string, NotificationChannel> = {
Expand Down
2 changes: 1 addition & 1 deletion packages/notification/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hyperjumptech/monika-notification",
"version": "1.17.0",
"version": "1.17.1",
"description": "notification package for monika",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
181 changes: 181 additions & 0 deletions prod_test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,187 @@ describe('Node CLI Testing', () => {

await cleanup()
})

it('should starts Monika with a JSON config from a URL', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()

// act
const { getStdout, waitForText } = await spawn(
'node',
`${monika} -r 1 -c https://raw.githubusercontent.com/hyperjumptech/monika/main/monika.example.json`
)

// assert
await waitForText('Starting Monika.')
const stdout = getStdout().join('\r\n')
expect(stdout).to.contain('Starting Monika. Probes: 1. Notifications: 1')

await cleanup()
})

it('should starts Monika with a YAML config from a URL', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()

// act
const { getStdout, waitForText } = await spawn(
'node',
`${monika} -r 1 -c https://raw.githubusercontent.com/hyperjumptech/monika/main/monika.example.yml`
)

// assert
await waitForText('Starting Monika.')
const stdout = getStdout().join('\r\n')
expect(stdout).to.contain('Starting Monika. Probes: 1. Notifications: 1')

await cleanup()
})

it('should starts Monika with a HAR file', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()

// act
const { getStdout, waitForText } = await spawn(
'node',
`${monika} -r 1 --har ./src/components/config/__tests__/form_encoded.har`
)

// assert
await waitForText('Starting Monika.')
const stdout = getStdout().join('\r\n')
expect(stdout).to.contain('Starting Monika. Probes: 1. Notifications: 1')

await cleanup()
})

it('should starts Monika with an Insomnia file', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()

// act
const { getStdout, waitForText } = await spawn(
'node',
`${monika} -r 1 --insomnia ./src/components/config/__tests__/petstore.insomnia.yaml`
)

// assert
await waitForText('Starting Monika.')
const stdout = getStdout().join('\r\n')
expect(stdout).to.contain('Starting Monika. Probes: 18. Notifications: 1')

await cleanup()
})

it('should starts Monika with a Postman file', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()

// act
const { getStdout, waitForText } = await spawn(
'node',
`${monika} -r 1 --postman ./src/components/config/__tests__/mock_files/grouped-postman_collection-v2.1.json`
)

// assert
await waitForText('Starting Monika.')
const stdout = getStdout().join('\r\n')
expect(stdout).to.contain('Starting Monika. Probes: 2. Notifications: 1')

await cleanup()
})

it('should starts Monika with a Sitemap file', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()

// act
const { getStdout, waitForText } = await spawn(
'node',
`${monika} -r 1 --sitemap ./src/components/config/__tests__/sitemap.xml`
)

// assert
await waitForText('Starting Monika.')
const stdout = getStdout().join('\r\n')
expect(stdout).to.contain('Starting Monika. Probes: 2. Notifications: 1')

await cleanup()
})

it('should starts Monika with a text file', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()

// act
const { getStdout, waitForText } = await spawn(
'node',
`${monika} -r 1 --text ./src/components/config/__tests__/textfile`
)

// assert
await waitForText('Starting Monika.')
const stdout = getStdout().join('\r\n')
expect(stdout).to.contain('Starting Monika. Probes: 2. Notifications: 1')

await cleanup()
})

it('should overwrites native config', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()

// act
const { getStdout, waitForText } = await spawn(
'node',
`${monika} -r 1 -c ./monika.example.yml --text ./src/components/config/__tests__/textfile`
)

// assert
await waitForText('Starting Monika.')
const stdout = getStdout().join('\r\n')
expect(stdout).to.contain('Starting Monika. Probes: 2. Notifications: 1')

await cleanup()
})

it('should runs with multiple config', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()

// act
const { getStdout, waitForText } = await spawn(
'node',
`${monika} -r 1 -c ./monika.example.json ./monika.example.yml`
)

// assert
await waitForText('Starting Monika.')
const stdout = getStdout().join('\r\n')
expect(stdout).to.contain('Starting Monika. Probes: 1. Notifications: 1')

await cleanup()
})

it('should not run if no probes in the config', async () => {
// arrange
const { spawn, cleanup } = await prepareEnvironment()

// act
const { getStderr, waitForText } = await spawn(
'node',
`${monika} -r 1 -c ./test/testConfigs/noProbes.yml`
)

// assert
await waitForText('Monika configuration is invalid')
const stdout = getStderr().join('\r\n')
expect(stdout).to.contain('Monika configuration is invalid')

await cleanup()
})

it('Detect config file changes successfully', async () => {
// arrange
const { spawn, cleanup, writeFile } = await prepareEnvironment()
Expand Down
25 changes: 16 additions & 9 deletions src/commands/monika.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
import { Command, Errors } from '@oclif/core'
import pEvent from 'p-event'

import type { Config } from '../interfaces/config'
import type { ValidatedConfig } from '../interfaces/config'
import type { Probe } from '../interfaces/probe'

import { getConfig, isSymonModeFrom } from '../components/config'
import {
getValidatedConfig,
isSymonModeFrom,
initConfig,
} from '../components/config'
import { createConfig } from '../components/config/create'
import { sortProbes } from '../components/config/sort'
import { printAllLogs } from '../components/logger'
Expand Down Expand Up @@ -113,9 +117,12 @@ export default class Monika extends Command {
return
}

await initLoaders(flags, this.config)

const isSymonMode = isSymonModeFrom(flags)
if (!isSymonMode) {
await initConfig()
}

await initLoaders(flags, this.config)
await logRunningInfo({ isSymonMode, isVerbose: flags.verbose })

if (isSymonMode) {
Expand All @@ -126,7 +133,7 @@ export default class Monika extends Command {
let isFirstRun = true

for (;;) {
const config = getConfig()
const config = getValidatedConfig()
const probes = getProbes({ config, flags })

// emit the sanitized probe
Expand All @@ -142,7 +149,7 @@ export default class Monika extends Command {

const controller = new AbortController()
const { signal } = controller
const notifications = config.notifications || []
const { notifications } = config
startProbing({
notifications,
probes,
Expand All @@ -169,7 +176,7 @@ export default class Monika extends Command {
} catch (error: unknown) {
this.error((error as Error)?.message, { exit: 1 })
} finally {
await closeLog()
closeLog()
}
}
}
Expand All @@ -195,7 +202,7 @@ async function logRunningInfo({ isVerbose, isSymonMode }: RunningInfoParams) {
}

type GetProbesParams = {
config: Config
config: ValidatedConfig
flags: MonikaFlags
}

Expand All @@ -207,7 +214,7 @@ function getProbes({ config, flags }: GetProbesParams): Probe[] {
)
}

function deprecationHandler(config: Config): Config {
function deprecationHandler(config: ValidatedConfig): ValidatedConfig {
const showDeprecateMsg: Record<'query', boolean> = {
query: false,
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/config/__tests__/parse-postman.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import { expect } from 'chai'
import { Config } from '../../../interfaces/config'
import { parseConfigFromPostman } from '../parse-postman'
import { parseConfigFromPostman } from '../parser/postman'
import basicCollectionV20 from './mock_files/basic-postman_collection-v2.0.json'
import basicCollectionV21 from './mock_files/basic-postman_collection-v2.1.json'
import groupedCollectionV20 from './mock_files/grouped-postman_collection-v2.0.json'
Expand Down
59 changes: 0 additions & 59 deletions src/components/config/create-config.ts

This file was deleted.

Loading

0 comments on commit 29bcb9e

Please sign in to comment.