|
| 1 | +import { createRequire } from 'module' |
| 2 | +import { fileURLToPath } from 'url' |
| 3 | +import { dirname } from 'path' |
| 4 | +const require = createRequire(import.meta.url) |
| 5 | +const { ClientFunction } = require('testcafe') |
| 6 | +const __filename = fileURLToPath(import.meta.url) |
| 7 | +const __dirname = dirname(__filename) |
| 8 | + |
| 9 | +import assert from 'assert' |
| 10 | +import fs from 'fs' |
| 11 | +import path from 'path' |
| 12 | +import { getParamNames } from '../../utils.js' |
| 13 | + |
| 14 | +const createTestFile = () => { |
| 15 | + assert(global.output_dir, 'global.output_dir must be set') |
| 16 | + |
| 17 | + const testFile = path.join(global.output_dir, `${Date.now()}_test.js`) |
| 18 | + const testControllerHolderDir = __dirname.replace(/\\/g, '/') |
| 19 | + |
| 20 | + fs.writeFileSync( |
| 21 | + testFile, |
| 22 | + `const testControllerHolder = require("${testControllerHolderDir}/testControllerHolder.cjs");\n\n |
| 23 | + fixture("fixture")\n |
| 24 | + test\n |
| 25 | + ("test", testControllerHolder.capture)`, |
| 26 | + ) |
| 27 | + |
| 28 | + return testFile |
| 29 | +} |
| 30 | + |
| 31 | +const mapError = testcafeErr => { |
| 32 | + if (!testcafeErr) return new Error('Unknown error') |
| 33 | + |
| 34 | + const code = testcafeErr.code |
| 35 | + if (code && testcafeErr.callsite) { |
| 36 | + return testcafeErr |
| 37 | + } |
| 38 | + |
| 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 |
| 44 | +} |
| 45 | + |
| 46 | +const createClientFunction = clientFn => { |
| 47 | + const fnString = clientFn.toString() |
| 48 | + const params = getParamNames(clientFn) |
| 49 | + |
| 50 | + return ClientFunction(fnString, { dependencies: {} }) |
| 51 | +} |
| 52 | + |
| 53 | +export { createTestFile, mapError, createClientFunction } |
0 commit comments