Skip to content

Commit d33d681

Browse files
author
DavertMik
committed
fixed Playwright tests
1 parent e4297d1 commit d33d681

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

lib/command/check.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default async function (options) {
7373
const files = codecept.testFiles
7474
const mocha = Container.mocha()
7575
mocha.files = files
76-
mocha.loadFiles()
76+
await mocha.loadFiles()
7777

7878
for (const suite of mocha.suite.suites) {
7979
if (suite && suite.tests) {

lib/mocha/factory.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class MochaFactory {
3939
process.exit(1)
4040
}
4141

42-
mocha.loadFiles = fn => {
42+
mocha.loadFiles = async fn => {
4343
// load features
4444
if (mocha.suite.suites.length === 0) {
4545
const featureFiles = mocha.files.filter(file => file.match(/\.feature$/))
@@ -49,18 +49,31 @@ class MochaFactory {
4949
}
5050

5151
// remove feature files
52+
const jsFiles = mocha.files.filter(file => !file.match(/\.feature$/))
5253
mocha.files = mocha.files.filter(file => !file.match(/\.feature$/))
5354

54-
// In ESM context, tests may already be loaded during codecept initialization
55-
// Only call original Mocha loadFiles if no suites exist yet
56-
if (mocha.suite.suites.length === 0) {
55+
// Load JavaScript test files using ESM imports
56+
if (jsFiles.length > 0) {
5757
try {
58+
// Try original loadFiles first for compatibility
5859
Mocha.prototype.loadFiles.call(mocha, fn)
5960
} catch (e) {
60-
// If original loadFiles fails (e.g., ESM compatibility issues),
61-
// skip it since tests may already be loaded through codecept
62-
if (e.message.includes('getStatus') || e.message.includes('ESM')) {
63-
console.warn('Skipping Mocha.prototype.loadFiles due to ESM compatibility issues')
61+
// If original loadFiles fails, load ESM files manually
62+
if (e.message.includes('not in cache') || e.message.includes('ESM')) {
63+
console.warn('Loading ESM test files manually due to Mocha compatibility issues')
64+
// Load ESM files by importing them
65+
await Promise.all(
66+
jsFiles.map(async file => {
67+
try {
68+
// Convert file path to file:// URL for dynamic import
69+
const fileUrl = `file://${file}`
70+
await import(fileUrl)
71+
} catch (importErr) {
72+
// If dynamic import fails, the file may have syntax errors or other issues
73+
console.error(`Failed to load test file ${file}:`, importErr.message)
74+
}
75+
}),
76+
)
6477
} else {
6578
throw e
6679
}

lib/recorder.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import promiseRetry from 'promise-retry'
44
import chalk from 'chalk'
55
import { printObjectProperties } from './utils.js'
66
import output from './output.js'
7-
const { log } = output
87
import { TimeoutError } from './timeout.js'
98
const MAX_TASKS = 100
109

@@ -92,7 +91,7 @@ export default {
9291
queueId++
9392
sessionId = null
9493
asyncErr = null
95-
log(`${currentQueue()} Starting recording promises`)
94+
output.log(`${currentQueue()} Starting recording promises`)
9695
promise = Promise.resolve()
9796
oldPromises = []
9897
tasks = []
@@ -248,8 +247,9 @@ export default {
248247
.replace(/\n/g, ' ')
249248
?.slice(0, 50)
250249
debug(chalk.gray(`${currentQueue()} Queued | catch with error handler ${fnDescription || ''}`))
250+
if (!promise) promise = Promise.resolve()
251251
return (promise = promise.catch(err => {
252-
log(`${currentQueue()}Error | ${err} ${fnDescription}...`)
252+
output.log(`${currentQueue()}Error | ${err} ${fnDescription}...`)
253253
if (!(err instanceof Error)) {
254254
// strange things may happen
255255
err = new Error(`[Wrapped Error] ${printObjectProperties(err)}`) // we should be prepared for them
@@ -274,9 +274,10 @@ export default {
274274
?.replace(/\s{2,}/g, ' ')
275275
.replace(/\n/g, ' ')
276276
?.slice(0, 50)
277+
if (!promise) promise = Promise.resolve()
277278
return (promise = promise.catch(err => {
278279
if (ignoredErrs.includes(err)) return // already caught
279-
log(`${currentQueue()} Error (Non-Terminated) | ${err} | ${fnDescription || ''}...`)
280+
output.log(`${currentQueue()} Error (Non-Terminated) | ${err} | ${fnDescription || ''}...`)
280281
if (!(err instanceof Error)) {
281282
// strange things may happen
282283
err = new Error(`[Wrapped Error] ${JSON.stringify(err)}`) // we should be prepared for them
@@ -340,7 +341,7 @@ export default {
340341
*/
341342
stop() {
342343
debug(this.toString())
343-
log(`${currentQueue()} Stopping recording promises`)
344+
output.log(`${currentQueue()} Stopping recording promises`)
344345
running = false
345346
},
346347

test/acceptance/react_test.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
const { I } = inject()
2-
31
Feature('React Selectors')
42

5-
Scenario('props @Puppeteer @Playwright', () => {
3+
Scenario('props @Puppeteer @Playwright', ({ I }) => {
64
I.amOnPage('https://codecept.io/test-react-calculator/')
75
I.click('7')
86
I.click({ react: 't', props: { name: '=' } })
@@ -13,15 +11,15 @@ Scenario('props @Puppeteer @Playwright', () => {
1311
I.seeElement({ react: 't', props: { value: '10' } })
1412
})
1513

16-
Scenario('component name @Puppeteer @Playwright', () => {
14+
Scenario('component name @Puppeteer @Playwright', ({ I }) => {
1715
I.amOnPage('http://negomi.github.io/react-burger-menu/')
1816
I.click({ react: 'BurgerIcon' })
1917
I.waitForVisible('#slide', 10)
2018
I.click('Alerts')
2119
I.seeElement({ react: 'Demo' })
2220
})
2321

24-
Scenario('using playwright locator @Playwright', () => {
22+
Scenario('using playwright locator @Playwright', ({ I }) => {
2523
I.amOnPage('https://codecept.io/test-react-calculator/')
2624
I.click('7')
2725
I.click({ pw: '_react=t[name = "="]' })

0 commit comments

Comments
 (0)