From d7e459e5f6b2a37a80d9ab138282f013a3bf663a Mon Sep 17 00:00:00 2001 From: Adam McCrea Date: Fri, 24 May 2024 15:13:37 -0400 Subject: [PATCH] Fix tests --- .vscode/launch.json | 15 +++++++++++++++ fastify/src/plugin.js | 2 +- fastify/test/plugin.test.js | 11 +++++------ node-core/src/lib/report.js | 2 +- node-core/src/test/report.test.js | 22 +++------------------- 5 files changed, 25 insertions(+), 27 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..94cf975 --- /dev/null +++ b/.vscode/launch.json @@ -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" + } + ] +} diff --git a/fastify/src/plugin.js b/fastify/src/plugin.js index 0d00681..6cd3f91 100644 --- a/fastify/src/plugin.js +++ b/fastify/src/plugin.js @@ -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()) diff --git a/fastify/test/plugin.test.js b/fastify/test/plugin.test.js index 79d271b..0652253 100644 --- a/fastify/test/plugin.test.js +++ b/fastify/test/plugin.test.js @@ -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 @@ -27,7 +26,7 @@ describe('Judoscale Fastify Plugin', () => { }, }) - const metrics = metricsStore.flush() + const metrics = Judoscale.adapters[0].collector.collect() expect(metrics.length).toEqual(1) diff --git a/node-core/src/lib/report.js b/node-core/src/lib/report.js index c5f663c..5243828 100644 --- a/node-core/src/lib/report.js +++ b/node-core/src/lib/report.js @@ -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 { diff --git a/node-core/src/test/report.test.js b/node-core/src/test/report.test.js index 5b463bb..adc9a9e 100644 --- a/node-core/src/test/report.test.js +++ b/node-core/src/test/report.test.js @@ -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() @@ -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', () => {