Skip to content

Commit 5927927

Browse files
committed
fix(typescript): normalize included step source paths
1 parent 44cb8b7 commit 5927927

7 files changed

Lines changed: 85 additions & 7 deletions

File tree

lib/container.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -892,10 +892,6 @@ async function loadSupportObject(modulePath, supportObjectName) {
892892
for (const [key, value] of mapping.entries()) {
893893
container.tsFileMapping.set(key, value)
894894
}
895-
// Step.line() maps temp paths back through store, not through the
896-
// container, so an include has to land there too. Without this a step
897-
// that originates in an included .ts page object is printed with the
898-
// deleted .temp.mjs sibling. (#5675)
899895
if (!store.tsFileMapping) {
900896
store.tsFileMapping = new Map()
901897
}

lib/step/base.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import color from 'chalk'
2+
import { pathToFileURL } from 'url'
23
import Secret from '../secret.js'
34
import { getCurrentTimeout } from '../timeout.js'
45
import { ucfirst, humanizeString, serializeError } from '../utils.js'
@@ -149,8 +150,6 @@ class Step {
149150
const lines = this.stack.split('\n')
150151
if (lines[STACK_LINE]) {
151152
let line = lines[STACK_LINE].trim()
152-
.replace(store.codeceptDir || '', '.')
153-
.trim()
154153

155154
// Map .temp.mjs back to original .ts files using container's tsFileMapping
156155
const fileMapping = store.tsFileMapping
@@ -160,10 +159,23 @@ class Step {
160159
line = line.replace(mjsFile, tsFile)
161160
break
162161
}
162+
163+
const mjsFileUrl = pathToFileURL(mjsFile).href
164+
if (line.includes(mjsFileUrl)) {
165+
line = line.replace(mjsFileUrl, pathToFileURL(tsFile).href)
166+
break
167+
}
163168
}
164169
}
165170

166-
return line
171+
const codeceptDir = store.codeceptDir || ''
172+
if (codeceptDir) {
173+
line = line
174+
.replace(pathToFileURL(codeceptDir).href, '.')
175+
.replace(codeceptDir, '.')
176+
}
177+
178+
return line.trim()
167179
}
168180
return ''
169181
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const config = {
2+
tests: './tests/*Test.ts',
3+
helpers: {
4+
FakeHelper: {
5+
require: './fakeHelper.js',
6+
},
7+
},
8+
include: {
9+
fooPage: './pages/fooPage.ts',
10+
},
11+
require: ['tsx/cjs'],
12+
name: 'typescript-step-paths',
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Helper from 'codeceptjs/lib/helper'
2+
3+
export default class FakeHelper extends Helper {
4+
doThing(label) {
5+
return label
6+
}
7+
8+
failNow(message) {
9+
throw new Error(message)
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export {}
2+
3+
const { I } = inject()
4+
5+
export default {
6+
open() {
7+
I.doThing('from page')
8+
},
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Feature('TypeScript step paths')
2+
3+
Scenario('shows original paths', ({ I, fooPage }) => {
4+
fooPage.open()
5+
I.failNow('boom')
6+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { execFile } from 'child_process'
2+
import { expect } from 'expect'
3+
import path from 'path'
4+
import { fileURLToPath } from 'url'
5+
6+
const __filename = fileURLToPath(import.meta.url)
7+
const __dirname = path.dirname(__filename)
8+
const runner = path.join(__dirname, '../../bin/codecept.js')
9+
const codeceptDir = path.join(__dirname, '../data/sandbox/typescript-step-paths')
10+
11+
describe('TypeScript step paths', () => {
12+
it('maps included page object steps back to their source file', done => {
13+
execFile(
14+
process.execPath,
15+
[runner, 'run', '--config', path.join(codeceptDir, 'codecept.conf.js')],
16+
{ cwd: codeceptDir, env: { ...process.env, FORCE_COLOR: '0' } },
17+
(err, stdout) => {
18+
try {
19+
expect(err).toBeTruthy()
20+
expect(stdout).toContain('Scenario Steps:')
21+
expect(stdout).toMatch(/at Object\.open \(\.\/pages\/fooPage\.ts:\d+:\d+\)/)
22+
expect(stdout).not.toContain('.temp.mjs')
23+
expect(stdout).not.toContain('file://./pages/fooPage.ts')
24+
done()
25+
} catch (error) {
26+
done(error)
27+
}
28+
},
29+
)
30+
})
31+
})

0 commit comments

Comments
 (0)