Skip to content

Commit

Permalink
injecting test & siute to container (#4785)
Browse files Browse the repository at this point in the history
* injecting test & siute to container

* added container const

* fix: export STANDARD_ACTING_HELPERS type

---------

Co-authored-by: DavertMik <[email protected]>
Co-authored-by: kobenguyent <[email protected]>
  • Loading branch information
3 people authored Jan 30, 2025
1 parent 7030c7f commit 9aae736
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/codecept.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ class Codecept {
}
const done = () => {
event.emit(event.all.result, container.result())
event.emit(event.all.after)
event.emit(event.all.after, this)
resolve()
}

try {
event.emit(event.all.before)
event.emit(event.all.before, this)
mocha.run(() => done())
} catch (e) {
output.error(e.stack)
Expand Down
13 changes: 11 additions & 2 deletions lib/command/check.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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')
Expand All @@ -23,6 +22,7 @@ module.exports = async function (options) {
container: false,
pageObjects: false,
plugins: false,
ai: true, // we don't need to check AI
helpers: false,
setup: false,
tests: false,
Expand Down Expand Up @@ -51,6 +51,8 @@ module.exports = async function (options) {
checks.container = err
}

const standardActingHelpers = container.STANDARD_ACTING_HELPERS

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

if (codecept) {
Expand Down Expand Up @@ -83,6 +85,13 @@ module.exports = async function (options) {
}
}

if (config?.ai?.request) {
checks.ai = true
printCheck('ai', checks['ai'], 'AI configuration is enabled, request function is set')
} else {
printCheck('ai', checks['ai'], 'AI is disabled')
}

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

store.dryRun = true
Expand Down Expand Up @@ -170,7 +179,7 @@ function printCheck(name, value, comment = '') {
}

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

output.print(status, name.toUpperCase(), chalk.dim(comment))
Expand Down
2 changes: 1 addition & 1 deletion lib/command/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { getConfig, getTestRoot } = require('./utils')
const Codecept = require('../codecept')
const container = require('../container')
const output = require('../output')
const actingHelpers = [...require('../plugin/standardActingHelpers'), 'REST']
const actingHelpers = [...container.STANDARD_ACTING_HELPERS, 'REST']

/**
* Prepare data and generate content of definitions file
Expand Down
2 changes: 1 addition & 1 deletion lib/command/interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Codecept = require('../codecept')
const Container = require('../container')
const event = require('../event')
const output = require('../output')
const webHelpers = require('../plugin/standardActingHelpers')
const webHelpers = Container.STANDARD_ACTING_HELPERS

module.exports = async function (path, options) {
// Backward compatibility for --profile
Expand Down
7 changes: 7 additions & 0 deletions lib/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ let container = {
* Dependency Injection Container
*/
class Container {
/**
* Get the standard acting helpers of CodeceptJS Container
*
*/
static get STANDARD_ACTING_HELPERS() {
return ['Playwright', 'WebDriver', 'Puppeteer', 'Appium', 'TestCafe']
}
/**
* Create container with all required helpers and support objects
*
Expand Down
3 changes: 2 additions & 1 deletion lib/helper/AI.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ const ora = require('ora-classic')
const fs = require('fs')
const path = require('path')
const ai = require('../ai')
const standardActingHelpers = require('../plugin/standardActingHelpers')
const Container = require('../container')
const { splitByChunks, minifyHtml } = require('../html')
const { beautify } = require('../utils')
const output = require('../output')
const { registerVariable } = require('../pause')

const standardActingHelpers = Container.STANDARD_ACTING_HELPERS

const gtpRole = {
user: 'user',
}
Expand Down
4 changes: 3 additions & 1 deletion lib/mocha/asyncWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ module.exports.injected = function (fn, suite, hookName) {
const opts = suite.opts || {}
const retries = opts[`retry${ucfirst(hookName)}`] || 0

const currentTest = hookName === 'before' || hookName === 'after' ? suite?.ctx?.currentTest : null

promiseRetry(
async (retry, number) => {
try {
recorder.startUnlessRunning()
await fn.call(this, getInjectedArguments(fn))
await fn.call(this, { ...getInjectedArguments(fn), suite, test: currentTest })
await recorder.promise().catch(err => retry(err))
} catch (err) {
retry(err)
Expand Down
5 changes: 5 additions & 0 deletions lib/mocha/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const getInjectedArguments = (fn, test) => {
const testArgs = {}
const params = parser.getParams(fn) || []
const objects = container.support()

for (const key of params) {
testArgs[key] = {}
if (test && test.inject && test.inject[key]) {
Expand All @@ -18,6 +19,10 @@ const getInjectedArguments = (fn, test) => {
testArgs[key] = container.support(key)
}

if (test) {
testArgs.suite = test?.parent
testArgs.test = test
}
return testArgs
}

Expand Down
4 changes: 2 additions & 2 deletions lib/plugin/autoDelay.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const Container = require('../container')
const store = require('../store')
const recorder = require('../recorder')
const event = require('../event')
const log = require('../output').log
const supportedHelpers = require('./standardActingHelpers').slice()
const { log } = require('../output')
const standardActingHelpers = Container.STANDARD_ACTING_HELPERS

const methodsToDelay = ['click', 'fillField', 'checkOption', 'pressKey', 'doubleClick', 'rightClick']

Expand Down
2 changes: 1 addition & 1 deletion lib/plugin/pageInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')
const Container = require('../container')
const recorder = require('../recorder')
const event = require('../event')
const supportedHelpers = require('./standardActingHelpers')
const supportedHelpers = Container.STANDARD_ACTING_HELPERS
const { scanForErrorMessages } = require('../html')
const { output } = require('..')
const { humanizeString, ucfirst } = require('../utils')
Expand Down
2 changes: 1 addition & 1 deletion lib/plugin/screenshotOnFail.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const defaultConfig = {
fullPageScreenshots: false,
}

const supportedHelpers = require('./standardActingHelpers')
const supportedHelpers = Container.STANDARD_ACTING_HELPERS

/**
* Creates screenshot on failure. Screenshot is saved into `output` directory.
Expand Down
5 changes: 4 additions & 1 deletion lib/plugin/standardActingHelpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const standardActingHelpers = ['Playwright', 'WebDriver', 'Puppeteer', 'Appium', 'TestCafe']
const Container = require('../container')
// due to using this in internal tooling we won't post deprecation warning
// but please switch to Container.STANDARD_ACTING_HELPERS
const standardActingHelpers = Container.STANDARD_ACTING_HELPERS

module.exports = standardActingHelpers
2 changes: 1 addition & 1 deletion lib/plugin/stepByStepReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const event = require('../event')
const output = require('../output')
const { template, deleteDir } = require('../utils')

const supportedHelpers = require('./standardActingHelpers')
const supportedHelpers = Container.STANDARD_ACTING_HELPERS

const defaultConfig = {
deleteSuccessful: true,
Expand Down
11 changes: 11 additions & 0 deletions test/data/sandbox/configs/store-test-and-suite/codecept.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exports.config = {
tests: './*_test.js',
output: './output',
helpers: {
FileSystem: {},
},
include: {},
bootstrap: false,
mocha: {},
name: 'store-test-and-suite tests',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const assert = require('assert')

Feature('store-test-and-suite suite')

BeforeSuite(({ suite, test }) => {
assert.strictEqual(suite.title, 'store-test-and-suite suite')
assert(!test)
})

Before(({ test }) => {
assert.strictEqual(test.title, 'test store-test-and-suite test')
test.artifacts.screenshot = 'screenshot'
})

Scenario('test store-test-and-suite test', ({ test }) => {
assert.strictEqual(test.title, 'test store-test-and-suite test')
assert(test.artifacts)
assert(test.meta)
assert.strictEqual(test.artifacts.screenshot, 'screenshot')
test.meta.browser = 'chrome'
})

After(({ test }) => {
assert.strictEqual(test.meta.browser, 'chrome')
})
20 changes: 20 additions & 0 deletions test/runner/store-test-and-suite_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { expect } = require('expect')
const exec = require('child_process').exec
const { codecept_dir, codecept_run } = require('./consts')
const debug = require('debug')('codeceptjs:tests')

const config_run_config = (config, grep, verbose = false) => `${codecept_run} ${verbose ? '--verbose' : ''} --config ${codecept_dir}/configs/store-test-and-suite/${config} ${grep ? `--grep "${grep}"` : ''}`

describe('CodeceptJS store-test-and-suite', function () {
this.timeout(10000)

it('should run store-test-and-suite test', done => {
exec(config_run_config('codecept.conf.js'), (err, stdout) => {
debug(stdout)
expect(stdout).toContain('test store-test-and-suite test')
expect(stdout).toContain('OK')
expect(err).toBeFalsy()
done()
})
})
})

0 comments on commit 9aae736

Please sign in to comment.