1+ import { createRequire } from 'module'
2+ import { fileURLToPath } from 'url'
3+ import { dirname } from 'path'
4+ const require = createRequire ( import . meta. url )
15const { ClientFunction } = require ( 'testcafe' )
6+ const __filename = fileURLToPath ( import . meta. url )
7+ const __dirname = dirname ( __filename )
28
3- const assert = require ( 'assert' )
4- const fs = require ( 'fs' )
5- const path = require ( 'path' )
6- const { getParamNames } = require ( '../../utils' )
9+ import assert from 'assert'
10+ import fs from 'fs'
11+ import path from 'path'
12+ import { getParamNames } from '../../utils.js'
713
814const createTestFile = ( ) => {
915 assert ( global . output_dir , 'global.output_dir must be set' )
@@ -13,7 +19,7 @@ const createTestFile = () => {
1319
1420 fs . writeFileSync (
1521 testFile ,
16- `import testControllerHolder from "${ testControllerHolderDir } /testControllerHolder.js" ;\n\n
22+ `const testControllerHolder = require( "${ testControllerHolderDir } /testControllerHolder.cjs") ;\n\n
1723 fixture("fixture")\n
1824 test\n
1925 ("test", testControllerHolder.capture)` ,
@@ -22,40 +28,26 @@ const createTestFile = () => {
2228 return testFile
2329}
2430
25- // TODO Better error mapping (actual, expected)
26- const mapError = testcafeError => {
27- // console.log('TODO map error better', JSON.stringify(testcafeError, null, 2));
28- if ( testcafeError . errMsg ) {
29- throw new Error ( testcafeError . errMsg )
30- }
31- const errorInfo = `${ testcafeError . callsite ? JSON . stringify ( testcafeError . callsite ) : '' } ${ testcafeError . apiFnChain || JSON . stringify ( testcafeError ) } `
32- throw new Error ( `TestCafe Error: ${ errorInfo } ` )
33- }
31+ const mapError = testcafeErr => {
32+ if ( ! testcafeErr ) return new Error ( 'Unknown error' )
3433
35- function createClientFunction ( func , args ) {
36- if ( ! args || ! args . length ) {
37- return ClientFunction ( func )
34+ const code = testcafeErr . code
35+ if ( code && testcafeErr . callsite ) {
36+ return testcafeErr
3837 }
39- const paramNames = getParamNames ( func )
40- const dependencies = { }
41- paramNames . forEach ( ( param , i ) => ( dependencies [ param ] = args [ i ] ) )
4238
43- return ClientFunction ( getFuncBody ( func ) , { dependencies } )
39+ const stack = testcafeErr . stack || testcafeErr . toString ( )
40+ const message = testcafeErr . message || testcafeErr . toString ( )
41+ const error = new Error ( message )
42+ error . stack = stack
43+ return error
4444}
4545
46- function getFuncBody ( func ) {
47- let fnStr = func . toString ( )
48- const arrowIndex = fnStr . indexOf ( '=>' )
49- if ( arrowIndex >= 0 ) {
50- fnStr = fnStr . slice ( arrowIndex + 2 )
46+ const createClientFunction = clientFn => {
47+ const fnString = clientFn . toString ( )
48+ const params = getParamNames ( clientFn )
5149
52- return eval ( `() => ${ fnStr } ` )
53- }
54- // TODO: support general functions
50+ return ClientFunction ( fnString , { dependencies : { } } )
5551}
5652
57- module . exports = {
58- createTestFile,
59- mapError,
60- createClientFunction,
61- }
53+ export { createTestFile , mapError , createClientFunction }
0 commit comments