Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bin/codecept.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ program
.description('Creates dummy config in current dir or [path]')
.action(errorHandler(require('../lib/command/init')))

program
.command('check')
.description('Checks configuration and environment before running tests')
.option('-t, --timeout [ms]', 'timeout for checks in ms, 20000 by default')
.action(errorHandler(require('../lib/command/check')))

program
.command('migrate [path]')
.description('Migrate json config to js config in current dir or [path]')
Expand Down
174 changes: 174 additions & 0 deletions lib/command/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
const { getConfig, getTestRoot } = require('./utils')
const Codecept = require('../codecept')
const output = require('../output')
const standardActingHelpers = require('../plugin/standardActingHelpers')
const store = require('../store')
const container = require('../container')
const figures = require('figures')
const chalk = require('chalk')
const { createTest } = require('../mocha/test')
const { getMachineInfo } = require('./info')
const definitions = require('./definitions')

module.exports = async function (test, options) {
if (options.grep) process.env.grep = options.grep
const configFile = options.config

setTimeout(() => {
output.error("Something went wrong. Checks didn't pass and timed out. Please check your config and helpers.")
process.exit(1)
}, options.timeout || 50000)

const checks = {
config: false,
container: false,
pageObjects: false,
helpers: false,
setup: false,
tests: false,
def: false,
}

const testRoot = getTestRoot(configFile)
let config = getConfig(configFile)

try {
config = getConfig(configFile)
checks['config'] = true
} catch (err) {
checks['config'] = err
}

printCheck('config', checks['config'], config.name)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about config name here?


let codecept
try {
codecept = new Codecept(config, options)
codecept.init(testRoot)
await container.started()
checks.container = true
} catch (err) {
checks.container = err
}

printCheck('container', checks['container'])

if (codecept && options.bootstrap) {
try {
await codecept.bootstrap()
checks.bootstrap = true
} catch (err) {
checks.bootstrap = err
}
printCheck('bootstrap', checks['bootstrap'])
}

let numTests = 0
if (codecept) {
try {
codecept.loadTests()
const mocha = container.mocha()
mocha.files = codecept.testFiles
mocha.loadFiles()
mocha.suite.suites.forEach(suite => {
numTests += suite.tests.length
})
if (numTests > 0) {
checks.tests = true
} else {
throw new Error('No tests found')
}
} catch (err) {
checks.tests = err
}
}

printCheck('tests', checks['tests'], `Total: ${numTests} tests`)

store.dryRun = true

const helpers = container.helpers()

try {
if (!Object.keys(helpers).length) throw new Error('No helpers found')
// load helpers
for (const helper of Object.values(helpers)) {
helper._init()
}
checks.helpers = true
} catch (err) {
checks.helpers = err
}

printCheck('helpers', checks['helpers'], `${Object.keys(helpers).join(', ')}`)

const pageObjects = container.support()

try {
if (Object.keys(pageObjects).length) {
for (const pageObject of Object.values(pageObjects)) {
pageObject.name
}
}
checks.pageObjects = true
} catch (err) {
checks.pageObjects = err
}
printCheck('page objects', checks['pageObjects'], `Total: ${Object.keys(pageObjects).length} support objects`)

if (Object.keys(helpers).length) {
const suite = container.mocha().suite
const test = createTest('test', () => {})
try {
for (const helper of Object.values(helpers)) {
await helper._beforeSuite(suite)
await helper._before(test)
await helper._passed(test)
await helper._after(test)
await helper._finishTest(suite)
await helper._afterSuite(suite)
}
checks.setup = true
} catch (err) {
checks.setup = err
}
}

printCheck('Helpers Hooks', checks['setup'], standardActingHelpers.some(h => Object.keys(helpers).includes(h)) ? 'Initializing and closing browser' : '')

try {
definitions(configFile, { dryRun: true })
checks.def = true
} catch (err) {
checks.def = err
}

printCheck('TypeScript Definitions', checks['def'])

output.print('')

if (!Object.values(checks).every(check => check === true)) {
output.error("Something went wrong. Checks didn't pass.")
output.print()
getMachineInfo()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing await here I think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! thank you

process.exit(1)
}

output.print(output.styles.success('All checks passed'.toUpperCase()), 'Ready to run your tests 🚀')
process.exit(0)
}

function printCheck(name, value, comment = '') {
let status = ''
if (value == true) {
status += chalk.bold.green(figures.tick)
} else {
status += chalk.bold.red(figures.cross)
}

if (value instanceof Error) {
comment = `${comment} ${chalk.red.italic(value.message)}`.trim()
}

output.print(status, name.toUpperCase(), chalk.dim(comment))
}
2 changes: 2 additions & 0 deletions lib/command/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ module.exports = function (genPath, options) {
definitionsFileContent += `\n${translationAliases.join('\n')}`
}

if (options.dryRun) return

fs.writeFileSync(path.join(targetFolderPath, 'steps.d.ts'), definitionsFileContent)
output.print('TypeScript Definitions provide autocompletion in Visual Studio Code and other IDEs')
output.print('Definitions were generated in steps.d.ts')
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
"fn-args": "4.0.0",
"fs-extra": "11.2.0",
"glob": "^11.0.0",
"fuse.js": "^7.0.0",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got error if this lib removed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch, this is bad merge, right

"html-minifier-terser": "7.2.0",
"inquirer": "6.5.2",
"invisi-data": "^1.0.0",
Expand Down