From 61e15beb716697c80e531a7d4f656ec0d66987ec Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 6 Feb 2025 19:36:20 -0500 Subject: [PATCH] fix: convert strings to numbers, closes #350 --- src/index.js | 2 +- src/parse-inputs.js | 20 ++++++++++++++++++++ test/parse-inputs.test.js | 12 +++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index e1c3bb3..cdd4f22 100644 --- a/src/index.js +++ b/src/index.js @@ -194,7 +194,7 @@ function cypressSplit(on, config, userSpecOrderFn = undefined) { } on('after:run', () => { - if (SPLIT_FILE) { + if (SPLIT_FILE && SPLIT_OUTPUT_FILE) { console.log('%s here are passing spec timings', label) const specDurations = batchSpecs diff --git a/src/parse-inputs.js b/src/parse-inputs.js index 430bf80..2484bee 100644 --- a/src/parse-inputs.js +++ b/src/parse-inputs.js @@ -5,6 +5,18 @@ const { getSpecs } = require('find-cypress-specs') const globby = require('globby') const { createShuffle } = require('fast-shuffle') +/** + * @typedef {Object} ParsedSplitInputs + * @property {number} SPLIT + * @property {number} SPLIT_INDEX + * @property {string|undefined} SPLIT_FILE + * @property {string|undefined} SPLIT_OUTPUT_FILE + * @property {string|undefined} ciName + */ + +/** + * @returns {ParsedSplitInputs} + */ function parseSplitInputs(env = {}, configEnv = {}) { let SPLIT = env.SPLIT || configEnv.split || configEnv.SPLIT let SPLIT_INDEX = env.SPLIT_INDEX || configEnv.splitIndex @@ -55,6 +67,14 @@ function parseSplitInputs(env = {}, configEnv = {}) { } } + // convert values that should be numbers + if (typeof SPLIT === 'string') { + SPLIT = Number(SPLIT) + } + if (typeof SPLIT_INDEX === 'string') { + SPLIT_INDEX = Number(SPLIT_INDEX) + } + return { SPLIT, SPLIT_INDEX, SPLIT_FILE, SPLIT_OUTPUT_FILE, ciName } } diff --git a/test/parse-inputs.test.js b/test/parse-inputs.test.js index 4442bb8..eac53a2 100644 --- a/test/parse-inputs.test.js +++ b/test/parse-inputs.test.js @@ -1,12 +1,22 @@ const test = require('ava') const path = require('path') -const { getSpecsToSplit } = require('../src/parse-inputs') +const { getSpecsToSplit, parseSplitInputs } = require('../src/parse-inputs') function toRelative(filenames) { const cwd = process.cwd() return filenames.map((filename) => path.relative(cwd, filename)) } +test('parseSplitInputs converts strings to numbers', (t) => { + const inputs = parseSplitInputs({ + SPLIT: '1', + SPLIT_INDEX: '2', + }) + + t.is(inputs.SPLIT, 1) + t.is(inputs.SPLIT_INDEX, 2) +}) + test('getSpecsToSplit spec pattern', (t) => { const specs = getSpecsToSplit({ SPEC: 'cypress/spec-a.js,cypress/integration/spec-b.js',