diff --git a/packages/driver/src/cypress/source_map_utils.ts b/packages/driver/src/cypress/source_map_utils.ts index 035e4db11dd2..a6a5b2f31b91 100644 --- a/packages/driver/src/cypress/source_map_utils.ts +++ b/packages/driver/src/cypress/source_map_utils.ts @@ -6,7 +6,7 @@ import type { BasicSourceMapConsumer } from 'source-map' import mappingsWasm from 'source-map/lib/mappings.wasm' import $utils from './utils' -import stackUtils from './stack_utils' +import { toPosix } from './util/to_posix' const sourceMapExtractionRegex = /\/\/\s*[@#]\s*sourceMappingURL\s*=\s*(data:[^\s]*)/g const regexDataUrl = /data:[^;\n]+(?:;charset=[^;\n]+)?;base64,([a-zA-Z0-9+/]+={0,2})/ // matches data urls @@ -23,7 +23,7 @@ const initializeSourceMapConsumer = async (script, sourceMap): Promise { } const getSourceContents = (filePath, sourceFile) => { - const posixFilePath = stackUtils.toPosix(filePath) + const posixFilePath = toPosix(filePath) if (!sourceMapConsumers[posixFilePath]) return null @@ -72,7 +72,7 @@ const getSourceContents = (filePath, sourceFile) => { } const getSourcePosition = (filePath, position) => { - const posixFilePath = stackUtils.toPosix(filePath) + const posixFilePath = toPosix(filePath) const sourceMapConsumer = sourceMapConsumers[posixFilePath] if (!sourceMapConsumer) return null diff --git a/packages/driver/src/cypress/stack_utils.ts b/packages/driver/src/cypress/stack_utils.ts index 3da82879709f..ae4875a4eaf0 100644 --- a/packages/driver/src/cypress/stack_utils.ts +++ b/packages/driver/src/cypress/stack_utils.ts @@ -15,7 +15,7 @@ import { codeFrameColumns } from '@babel/code-frame' import $utils from './utils' import $sourceMapUtils from './source_map_utils' - +import { toPosix } from './util/to_posix' // Intentionally deep-importing from @packages/errors so as to not bundle the entire @packages/errors in the client unnecessarily import { getStackLines, replacedStack, stackWithoutMessage, splitStack, unsplitStack, stackLineRegex } from '@packages/errors/src/stackUtils' @@ -184,12 +184,6 @@ const getCodeFrameFromSource = (sourceCode, { line, column: originalColumn, rela } } -export const toPosix = (file: string) => { - return Cypress.config('platform') === 'win32' - ? file.replaceAll('\\', '/') - : file -} - const getRelativePathFromRoot = (relativeFile: string, absoluteFile?: string) => { // at this point relativeFile is relative to the cypress config // we need it to be relative to the repo root, which is different for monorepos @@ -549,5 +543,4 @@ export default { stackWithUserInvocationStackSpliced, captureUserInvocationStack, getInvocationDetails, - toPosix, } diff --git a/packages/driver/src/cypress/util/to_posix.ts b/packages/driver/src/cypress/util/to_posix.ts new file mode 100644 index 000000000000..4361abd46bee --- /dev/null +++ b/packages/driver/src/cypress/util/to_posix.ts @@ -0,0 +1,5 @@ +export const toPosix = (file: string) => { + return Cypress.config('platform') === 'win32' + ? file.replaceAll('\\', '/') + : file +} diff --git a/packages/driver/test/unit/cypress/util/toPosix.spec.ts b/packages/driver/test/unit/cypress/util/toPosix.spec.ts new file mode 100644 index 000000000000..b1c76f821471 --- /dev/null +++ b/packages/driver/test/unit/cypress/util/toPosix.spec.ts @@ -0,0 +1,40 @@ +/** + * @vitest-environment jsdom + */ + +import { vi, describe, it, expect, beforeEach, MockedFunction } from 'vitest' + +import { toPosix } from '../../../../src/cypress/util/to_posix' + +describe('toPosix', () => { + let config: MockedFunction + + beforeEach(() => { + config = vi.fn() + + // @ts-expect-error + global.Cypress = { + config, + } + }) + + describe('on windows', () => { + beforeEach(() => { + config.mockReturnValue('win32') + }) + + it('replaces backslashes with forward slashes', () => { + expect(toPosix('C:\\some\\file')).toEqual('C:/some/file') + }) + }) + + describe(`on other OS'`, () => { + beforeEach(() => { + config.mockReturnValue('darwin64') + }) + + it('performs as an identity function', () => { + expect(toPosix('/some/file')).toEqual('/some/file') + }) + }) +})