From d6baf0cebc5127eb3ce8977a5d20378b88f17975 Mon Sep 17 00:00:00 2001 From: Yaroslav Serhieiev Date: Sat, 13 Jul 2024 09:55:35 +0300 Subject: [PATCH] fix: parameters serialization (#48) --- .../allure-adapter/normalizeParameters.ts | 10 +++++++--- src/runtime/AllureRuntimeImplementation.ts | 3 +-- src/runtime/__snapshots__/runtime.test.ts.snap | 4 ++++ src/runtime/runtime.test.ts | 17 ++++++++++------- src/runtime/types.ts | 2 +- 5 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/reporter/allure-adapter/normalizeParameters.ts b/src/reporter/allure-adapter/normalizeParameters.ts index 19978d1..78b31b0 100644 --- a/src/reporter/allure-adapter/normalizeParameters.ts +++ b/src/reporter/allure-adapter/normalizeParameters.ts @@ -1,9 +1,13 @@ import type { AllureTestCaseResult, AllureTestStepResult, Parameter } from 'jest-allure2-reporter'; export function normalizeParameters(result: AllureTestCaseResult | AllureTestStepResult) { - return result.parameters?.map(stringifyParameter); + return result.parameters?.filter(excludeParameters).map(stringifyParameter); } -function stringifyParameter({ name, value }: Parameter) { - return { name, value: String(value) }; +function excludeParameters({ excluded }: Parameter) { + return !excluded; +} + +function stringifyParameter(parameter: Parameter) { + return { ...parameter, value: String(parameter.value) }; } diff --git a/src/runtime/AllureRuntimeImplementation.ts b/src/runtime/AllureRuntimeImplementation.ts index 01552d8..1e2e208 100644 --- a/src/runtime/AllureRuntimeImplementation.ts +++ b/src/runtime/AllureRuntimeImplementation.ts @@ -100,11 +100,10 @@ export class AllureRuntimeImplementation implements AllureRuntime { parameter: AllureRuntime['parameter'] = (name, value, options) => { typeAssertions.assertString(name, 'name'); - typeAssertions.assertPrimitive(value, 'value'); this.#coreModule.parameter({ name, - value: String(value), + value: typeof value === 'string' ? value : util.inspect(value), ...options, }); }; diff --git a/src/runtime/__snapshots__/runtime.test.ts.snap b/src/runtime/__snapshots__/runtime.test.ts.snap index 88adaa7..9c1750c 100644 --- a/src/runtime/__snapshots__/runtime.test.ts.snap +++ b/src/runtime/__snapshots__/runtime.test.ts.snap @@ -68,6 +68,10 @@ exports[`AllureRuntime should add attachments within the steps 1`] = ` "name": "0", "value": "fourth", }, + { + "name": "1", + "value": "{ key: 'value' }", + }, ], "stage": "interrupted", "start": 5, diff --git a/src/runtime/runtime.test.ts b/src/runtime/runtime.test.ts index 89e8ae7..25136eb 100644 --- a/src/runtime/runtime.test.ts +++ b/src/runtime/runtime.test.ts @@ -43,13 +43,16 @@ describe('AllureRuntime', () => { expect(resultingPath).toBe('/attachments/first'); - const innerStep3 = runtime.createStep('inner step 3 ({{0}})', async (message: string) => { - runtime.attachment('attachment4', message, 'text/plain'); + const innerStep3 = runtime.createStep( + 'inner step 3 ({{0}})', + async (message: string, _extra: object) => { + runtime.attachment('attachment4', message, 'text/plain'); - const error = new Error('Async error'); - error.stack = 'Error: Async error\n at innerStep3'; - throw error; - }); + const error = new Error('Async error'); + error.stack = 'Error: Async error\n at innerStep3'; + throw error; + }, + ); await runtime.step('outer step', async () => { try { @@ -68,7 +71,7 @@ describe('AllureRuntime', () => { /* empty */ }); runtime.attachment('attachment3', 'third', 'text/plain'); - await innerStep3('fourth').catch(() => { + await innerStep3('fourth', { key: 'value' }).catch(() => { /* empty */ }); }); diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 6e3e07f..83e2826 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -44,7 +44,7 @@ export interface AllureRuntime { parameter(name: string, value: unknown, options?: ParameterOptions): void; - parameters(parameters: Record): void; + parameters(parameters: object): void; status(status: Status, statusDetails?: StatusDetails): void;