Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,12 @@ async function loadSupportObject(modulePath, supportObjectName) {
for (const [key, value] of mapping.entries()) {
container.tsFileMapping.set(key, value)
}
if (!store.tsFileMapping) {
store.tsFileMapping = new Map()
}
for (const [key, value] of mapping.entries()) {
store.tsFileMapping.set(key, value)
}
} catch (tsError) {
throw new Error(`Failed to load TypeScript file ${importPath}: ${tsError.message}. Make sure 'typescript' package is installed.`)
}
Expand Down
18 changes: 15 additions & 3 deletions lib/step/base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import color from 'chalk'
import { pathToFileURL } from 'url'
import Secret from '../secret.js'
import { getCurrentTimeout } from '../timeout.js'
import { ucfirst, humanizeString, serializeError } from '../utils.js'
Expand Down Expand Up @@ -149,8 +150,6 @@ class Step {
const lines = this.stack.split('\n')
if (lines[STACK_LINE]) {
let line = lines[STACK_LINE].trim()
.replace(store.codeceptDir || '', '.')
.trim()

// Map .temp.mjs back to original .ts files using container's tsFileMapping
const fileMapping = store.tsFileMapping
Expand All @@ -160,10 +159,23 @@ class Step {
line = line.replace(mjsFile, tsFile)
break
}

const mjsFileUrl = pathToFileURL(mjsFile).href
if (line.includes(mjsFileUrl)) {
line = line.replace(mjsFileUrl, pathToFileURL(tsFile).href)
break
}
}
}

return line
const codeceptDir = store.codeceptDir || ''
if (codeceptDir) {
line = line
.replace(pathToFileURL(codeceptDir).href, '.')
.replace(codeceptDir, '.')
}

return line.trim()
}
return ''
}
Expand Down
13 changes: 13 additions & 0 deletions test/data/sandbox/typescript-step-paths/codecept.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const config = {
tests: './tests/*Test.ts',
helpers: {
FakeHelper: {
require: './fakeHelper.js',
},
},
include: {
fooPage: './pages/fooPage.ts',
},
require: ['tsx/cjs'],
Comment thread
luantaraschi marked this conversation as resolved.
name: 'typescript-step-paths',
}
7 changes: 7 additions & 0 deletions test/data/sandbox/typescript-step-paths/codecept.esm.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { config as baseConfig } from './codecept.conf.js'

export const config = {
...baseConfig,
require: ['tsx/esm'],
name: 'typescript-step-paths-esm',
}
11 changes: 11 additions & 0 deletions test/data/sandbox/typescript-step-paths/fakeHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Helper from 'codeceptjs/lib/helper'

export default class FakeHelper extends Helper {
doThing(label) {
return label
}

failNow(message) {
throw new Error(message)
}
}
9 changes: 9 additions & 0 deletions test/data/sandbox/typescript-step-paths/pages/fooPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export {}

const { I } = inject()

export default {
open() {
I.doThing('from page')
},
}
6 changes: 6 additions & 0 deletions test/data/sandbox/typescript-step-paths/tests/fooTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Feature('TypeScript step paths')

Scenario('shows original paths', ({ I, fooPage }) => {
fooPage.open()
I.failNow('boom')
})
33 changes: 33 additions & 0 deletions test/runner/typescript_step_paths_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { execFile } from 'child_process'
import { expect } from 'expect'
import path from 'path'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const runner = path.join(__dirname, '../../bin/codecept.js')
const codeceptDir = path.join(__dirname, '../data/sandbox/typescript-step-paths')

describe('TypeScript step paths', () => {
for (const configFile of ['codecept.conf.js', 'codecept.esm.conf.js']) {
it(`maps included page object steps back to their source file with ${configFile}`, done => {
execFile(
process.execPath,
[runner, 'run', '--config', path.join(codeceptDir, configFile)],
{ cwd: codeceptDir, env: { ...process.env, FORCE_COLOR: '0' } },
(err, stdout) => {
try {
expect(err).toBeTruthy()
expect(stdout).toContain('Scenario Steps:')
expect(stdout).toMatch(/at Object\.open \(\.\/pages\/fooPage\.ts:\d+:\d+\)/)
expect(stdout).not.toContain('.temp.mjs')
expect(stdout).not.toContain('file://./pages/fooPage.ts')
done()
} catch (error) {
done(error)
}
},
)
})
}
})