From 3cd800e3f00cf7c49f29a4136261459b3aa13892 Mon Sep 17 00:00:00 2001 From: Tian Feng Date: Wed, 20 Nov 2024 19:34:28 -0800 Subject: [PATCH] support simple format strings --- src/cucumber-runner.ts | 25 ++++++++++++++++--------- tests/unit/src/cucumber-runner.spec.js | 10 +++------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/cucumber-runner.ts b/src/cucumber-runner.ts index 8647bb8..c7c5450 100644 --- a/src/cucumber-runner.ts +++ b/src/cucumber-runner.ts @@ -66,25 +66,32 @@ export function buildArgs(runCfg: CucumberRunnerConfig, cucumberBin: string) { } /** - * Normalizes a Cucumber format string by ensuring it is in the form of `"key":"value"`. + * Normalizes a Cucumber-js format string. * - * @param {string} format - The input format string, which can be in various forms: + * For structured inputs (`key:value` or `"key:value"`), returns a string in the + * form `"key":"value"`, with the asset directory prepended to relative paths. + * For simple inputs (e.g., `progress-bar`), returns the input as-is. + * + * @param {string} format - The input format string. Examples include: * - `"key:value"` * - `"key":"value"` * - `key:value` - * @param {string} assetDir - The asset directory. - * @throws {Error} If the input format is invalid (e.g., missing a colon separator). - * @returns {string} The normalized format string in the form of `"key":"value"`, - * with the asset directory prepended to relative paths. + * - `progress-bar` + * @param {string} assetDir - The directory to prepend to the value for relative paths. + * @returns {string} The normalized format string. For structured inputs, it returns + * a string in the form `"key":"value"`. For simple inputs, it + * returns the input unchanged. * * Example: - * Input: `"html:formatter/report.html"`, `"/project/assets"` - * Output: `"html":"/project/assets/formatter/report.html"` + * - Input: `"html":"formatter/report.html"`, `"/project/assets"` + * Output: `"html":"/project/assets/formatter/report.html"` + * - Input: `"progress-bar"`, `"/project/assets"` + * Output: `"progress-bar"` */ export function normalizeFormat(format: string, assetDir: string): string { const match = format.match(/^"?([^:]+):"?([^"]+)"?$/); if (!match) { - throw new Error(`Invalid format: ${format}`); + return format; } let [, key, value] = match; diff --git a/tests/unit/src/cucumber-runner.spec.js b/tests/unit/src/cucumber-runner.spec.js index f51539d..ce530f4 100644 --- a/tests/unit/src/cucumber-runner.spec.js +++ b/tests/unit/src/cucumber-runner.spec.js @@ -54,12 +54,8 @@ describe('normalizeFormat', () => { ); }); - it('should throw an error for invalid formats', () => { - expect(() => - normalizeFormat(`html-formatter/report.html`, assetDir), - ).toThrow('Invalid format: html-formatter/report.html'); - expect(() => - normalizeFormat(`"htmlformatter/report.html"`, assetDir), - ).toThrow('Invalid format: "htmlformatter/report.html"'); + it('should return simple strings as-is', () => { + expect(normalizeFormat(`"progress-bar"`, assetDir)).toBe('"progress-bar"'); + expect(normalizeFormat(`progress-bar`, assetDir)).toBe('progress-bar'); }); });