Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlogic committed May 24, 2024
1 parent 3ecae1f commit d7e459e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 27 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Jest Test",
"runtimeExecutable": "${workspaceFolder}/node-core/node_modules/.bin/jest",
"args": ["${relativeFile}"],
"console": "integratedTerminal",
"internalConsoleOptions": "openOnSessionStart",
"cwd": "${workspaceFolder}/node-core"
}
]
}
2 changes: 1 addition & 1 deletion fastify/src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const packageInfo = require('../package.json')

const metricsStore = new MetricsStore()

async function rawPlugin(fastify, judoscale) {
async function rawPlugin(fastify) {
fastify.addHook('onRequest', async (request, _reply) => {
try {
const queueTime = requestMetrics.queueTimeFromHeaders(request.headers, Date.now())
Expand Down
11 changes: 5 additions & 6 deletions fastify/test/plugin.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
const fastify = require('fastify')
const judoscalePlugin = require('../src/plugin')
const { Judoscale, plugin } = require('../src/plugin')
const { MetricsStore } = require('judoscale-node-core')

describe('Judoscale Fastify Plugin', () => {
let app, metricsStore
let app

beforeEach(async () => {
metricsStore = new MetricsStore()
app = fastify()
app.register(judoscalePlugin, { metricsStore })
app.register(plugin)

await app.ready()
})

afterEach(() => app.close())

test('captures queue time in MetricsStore', async () => {
test('captures request queue time and returns it from the collector', async () => {
// Simulate 100ms queue time
const simulatedHeaderTime = Date.now() - 100

Expand All @@ -27,7 +26,7 @@ describe('Judoscale Fastify Plugin', () => {
},
})

const metrics = metricsStore.flush()
const metrics = Judoscale.adapters[0].collector.collect()

expect(metrics.length).toEqual(1)

Expand Down
2 changes: 1 addition & 1 deletion node-core/src/lib/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Report {
payload() {
const adapterMetadata = {}
for (const adapter of this.adapters) {
adapterMetadata[adapter.identifier] = adapter.meta
adapterMetadata[adapter.identifier] = adapter.meta || {}
}

return {
Expand Down
22 changes: 3 additions & 19 deletions node-core/src/test/report.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,10 @@
import Report from '../lib/report'
import Metric from '../lib/metric'

const collectors = [{ a: 'collector' }]
const adapter = {
asJson: () => {
'payload'
},
collectors,
}
const adapter = { identifier: 'some-adapter' }
const exampleConfig = { container: 'web.007' }
const metric = new Metric('some-identifier', new Date(), '1234')
const report = new Report(adapter, exampleConfig, [metric])

describe('constructor', () => {
test('adapter property', () => {
expect(report.adapter).toEqual(adapter)
})

test('config property', () => {
expect(report.config).toEqual(exampleConfig)
})
})
const report = new Report([adapter], exampleConfig, [metric])

describe('payload', () => {
const payload = report.payload()
Expand All @@ -40,7 +24,7 @@ describe('payload', () => {
})

test('adapters with property value', () => {
expect(payload).toHaveProperty('adapters', report.adapter.asJson())
expect(payload.adapters['some-adapter']).toEqual({})
})

test('config with property value', () => {
Expand Down

0 comments on commit d7e459e

Please sign in to comment.