|
| 1 | +const { getConfig, getTestRoot } = require('./utils') |
| 2 | +const Codecept = require('../codecept') |
| 3 | +const output = require('../output') |
| 4 | +const standardActingHelpers = require('../plugin/standardActingHelpers') |
| 5 | +const store = require('../store') |
| 6 | +const container = require('../container') |
| 7 | +const figures = require('figures') |
| 8 | +const chalk = require('chalk') |
| 9 | +const { createTest } = require('../mocha/test') |
| 10 | +const { getMachineInfo } = require('./info') |
| 11 | +const definitions = require('./definitions') |
| 12 | + |
| 13 | +module.exports = async function (options) { |
| 14 | + const configFile = options.config |
| 15 | + |
| 16 | + setTimeout(() => { |
| 17 | + output.error("Something went wrong. Checks didn't pass and timed out. Please check your config and helpers.") |
| 18 | + process.exit(1) |
| 19 | + }, options.timeout || 50000) |
| 20 | + |
| 21 | + const checks = { |
| 22 | + config: false, |
| 23 | + container: false, |
| 24 | + pageObjects: false, |
| 25 | + helpers: false, |
| 26 | + setup: false, |
| 27 | + tests: false, |
| 28 | + def: false, |
| 29 | + } |
| 30 | + |
| 31 | + const testRoot = getTestRoot(configFile) |
| 32 | + let config = getConfig(configFile) |
| 33 | + |
| 34 | + try { |
| 35 | + config = getConfig(configFile) |
| 36 | + checks['config'] = true |
| 37 | + } catch (err) { |
| 38 | + checks['config'] = err |
| 39 | + } |
| 40 | + |
| 41 | + printCheck('config', checks['config'], config.name) |
| 42 | + |
| 43 | + let codecept |
| 44 | + try { |
| 45 | + codecept = new Codecept(config, options) |
| 46 | + codecept.init(testRoot) |
| 47 | + await container.started() |
| 48 | + checks.container = true |
| 49 | + } catch (err) { |
| 50 | + checks.container = err |
| 51 | + } |
| 52 | + |
| 53 | + printCheck('container', checks['container']) |
| 54 | + |
| 55 | + if (codecept) { |
| 56 | + try { |
| 57 | + if (options.bootstrap) await codecept.bootstrap() |
| 58 | + checks.bootstrap = true |
| 59 | + } catch (err) { |
| 60 | + checks.bootstrap = err |
| 61 | + } |
| 62 | + printCheck('bootstrap', checks['bootstrap'], options.bootstrap ? 'Bootstrap was executed' : 'No bootstrap command') |
| 63 | + } |
| 64 | + |
| 65 | + let numTests = 0 |
| 66 | + if (codecept) { |
| 67 | + try { |
| 68 | + codecept.loadTests() |
| 69 | + const mocha = container.mocha() |
| 70 | + mocha.files = codecept.testFiles |
| 71 | + mocha.loadFiles() |
| 72 | + mocha.suite.suites.forEach(suite => { |
| 73 | + numTests += suite.tests.length |
| 74 | + }) |
| 75 | + if (numTests > 0) { |
| 76 | + checks.tests = true |
| 77 | + } else { |
| 78 | + throw new Error('No tests found') |
| 79 | + } |
| 80 | + } catch (err) { |
| 81 | + checks.tests = err |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + printCheck('tests', checks['tests'], `Total: ${numTests} tests`) |
| 86 | + |
| 87 | + store.dryRun = true |
| 88 | + |
| 89 | + const helpers = container.helpers() |
| 90 | + |
| 91 | + try { |
| 92 | + if (!Object.keys(helpers).length) throw new Error('No helpers found') |
| 93 | + // load helpers |
| 94 | + for (const helper of Object.values(helpers)) { |
| 95 | + if (helper._init) helper._init() |
| 96 | + } |
| 97 | + checks.helpers = true |
| 98 | + } catch (err) { |
| 99 | + checks.helpers = err |
| 100 | + } |
| 101 | + |
| 102 | + printCheck('helpers', checks['helpers'], `${Object.keys(helpers).join(', ')}`) |
| 103 | + |
| 104 | + const pageObjects = container.support() |
| 105 | + |
| 106 | + try { |
| 107 | + if (Object.keys(pageObjects).length) { |
| 108 | + for (const pageObject of Object.values(pageObjects)) { |
| 109 | + pageObject.name |
| 110 | + } |
| 111 | + } |
| 112 | + checks.pageObjects = true |
| 113 | + } catch (err) { |
| 114 | + checks.pageObjects = err |
| 115 | + } |
| 116 | + printCheck('page objects', checks['pageObjects'], `Total: ${Object.keys(pageObjects).length} support objects`) |
| 117 | + |
| 118 | + if (Object.keys(helpers).length) { |
| 119 | + const suite = container.mocha().suite |
| 120 | + const test = createTest('test', () => {}) |
| 121 | + try { |
| 122 | + for (const helper of Object.values(helpers)) { |
| 123 | + if (helper._beforeSuite) await helper._beforeSuite(suite) |
| 124 | + if (helper._before) await helper._before(test) |
| 125 | + if (helper._passed) await helper._passed(test) |
| 126 | + if (helper._after) await helper._after(test) |
| 127 | + if (helper._finishTest) await helper._finishTest(suite) |
| 128 | + if (helper._afterSuite) await helper._afterSuite(suite) |
| 129 | + } |
| 130 | + checks.setup = true |
| 131 | + } catch (err) { |
| 132 | + checks.setup = err |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + printCheck('Helpers Before/After', checks['setup'], standardActingHelpers.some(h => Object.keys(helpers).includes(h)) ? 'Initializing and closing browser' : '') |
| 137 | + |
| 138 | + try { |
| 139 | + definitions(configFile, { dryRun: true }) |
| 140 | + checks.def = true |
| 141 | + } catch (err) { |
| 142 | + checks.def = err |
| 143 | + } |
| 144 | + |
| 145 | + printCheck('TypeScript Definitions', checks['def']) |
| 146 | + |
| 147 | + output.print('') |
| 148 | + |
| 149 | + if (!Object.values(checks).every(check => check === true)) { |
| 150 | + output.error("Something went wrong. Checks didn't pass.") |
| 151 | + output.print() |
| 152 | + await getMachineInfo() |
| 153 | + process.exit(1) |
| 154 | + } |
| 155 | + |
| 156 | + output.print(output.styles.success('All checks passed'.toUpperCase()), 'Ready to run your tests 🚀') |
| 157 | + process.exit(0) |
| 158 | +} |
| 159 | + |
| 160 | +function printCheck(name, value, comment = '') { |
| 161 | + let status = '' |
| 162 | + if (value == true) { |
| 163 | + status += chalk.bold.green(figures.tick) |
| 164 | + } else { |
| 165 | + status += chalk.bold.red(figures.cross) |
| 166 | + } |
| 167 | + |
| 168 | + if (value instanceof Error) { |
| 169 | + comment = `${comment} ${chalk.red.italic(value.message)}`.trim() |
| 170 | + } |
| 171 | + |
| 172 | + output.print(status, name.toUpperCase(), chalk.dim(comment)) |
| 173 | +} |
0 commit comments