Skip to content

Commit 9151b8c

Browse files
author
DavertMik
committed
added step timeouts tests
1 parent 3510f20 commit 9151b8c

File tree

8 files changed

+187
-14
lines changed

8 files changed

+187
-14
lines changed

lib/listener/globalTimeout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const output = require('../output')
33
const recorder = require('../recorder')
44
const Config = require('../config')
55
const { timeouts } = require('../store')
6-
const TIMEOUT_ORDER = require('../step').TIMEOUT_ORDER
6+
const { TIMEOUT_ORDER } = require('../step/timeout')
77

88
module.exports = function () {
99
let timeout

lib/step/record.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const event = require('../event')
22
const recorder = require('../recorder')
33
const StepConfig = require('./config')
4+
const { debug } = require('../output')
45
const store = require('../store')
56
const { TIMEOUT_ORDER } = require('./timeout')
67
const retryStep = require('./retry')
@@ -11,13 +12,17 @@ function recordStep(step, args) {
1112
const lastArg = args[args.length - 1]
1213
if (lastArg instanceof StepConfig) {
1314
const stepConfig = args.pop()
14-
const { options, timeout, retry } = stepConfig.getConfig()
15+
const { opts, timeout, retry } = stepConfig.getConfig()
1516

16-
if (options) {
17-
store.stepOptions = options
18-
step.opts = options
17+
if (opts) {
18+
debug(`Step ${step.name}: options applied ${JSON.stringify(opts)}`)
19+
store.stepOptions = opts
20+
step.opts = opts
21+
}
22+
if (timeout) {
23+
debug(`Step ${step.name} timeout ${timeout}s`)
24+
step.setTimeout(timeout * 1000, TIMEOUT_ORDER.codeLimitTime)
1925
}
20-
if (timeout) step.setTimeout(timeout, TIMEOUT_ORDER.codeLimitTime)
2126
if (retry) retryStep(retry)
2227
}
2328

runok.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,73 @@ ${changelog}`
546546
process.exit(1)
547547
}
548548
},
549+
550+
async runnerCreateTests(featureName) {
551+
// create runner tests for feature
552+
const fs = require('fs').promises
553+
const path = require('path')
554+
555+
// Create directories
556+
const configDir = path.join('test/data/sandbox/configs', featureName)
557+
await fs.mkdir(configDir, { recursive: true })
558+
559+
// Create codecept config file
560+
const configContent = `exports.config = {
561+
tests: './*_test.js',
562+
output: './output',
563+
helpers: {
564+
FileSystem: {},
565+
},
566+
include: {},
567+
bootstrap: false,
568+
mocha: {},
569+
name: '${featureName} tests'
570+
}
571+
`
572+
await fs.writeFile(path.join(configDir, `codecept.conf.js`), configContent)
573+
574+
// Create feature test file
575+
const testContent = `Feature('${featureName}');
576+
577+
Scenario('test ${featureName}', ({ I }) => {
578+
// Add test steps here
579+
});
580+
`
581+
await fs.writeFile(path.join(configDir, `${featureName}_test.js`), testContent)
582+
583+
// Create runner test file
584+
const runnerTestContent = `const { expect } = require('expect')
585+
const exec = require('child_process').exec
586+
const { codecept_dir, codecept_run } = require('./consts')
587+
const debug = require('debug')('codeceptjs:tests')
588+
589+
const config_run_config = (config, grep, verbose = false) =>
590+
\`\${codecept_run} \${verbose ? '--verbose' : ''} --config \${codecept_dir}/configs/${featureName}/\${config} \${grep ? \`--grep "\${grep}"\` : ''}\`
591+
592+
describe('CodeceptJS ${featureName}', function () {
593+
this.timeout(10000)
594+
595+
it('should run ${featureName} test', done => {
596+
exec(config_run_config('codecept.conf.js'), (err, stdout) => {
597+
debug(stdout)
598+
expect(stdout).toContain('OK')
599+
expect(err).toBeFalsy()
600+
done()
601+
})
602+
})
603+
})
604+
`
605+
await fs.writeFile(path.join('test/runner', `${featureName}_test.js`), runnerTestContent)
606+
607+
console.log(`Created test files for feature: ${featureName}`)
608+
609+
console.log('Run codecept tests with:')
610+
console.log(`./bin/codecept.js run --config ${configDir}/codecept.${featureName}.conf.js`)
611+
612+
console.log('')
613+
console.log('Run tests with:')
614+
console.log(`npx mocha test/runner --grep ${featureName}`)
615+
},
549616
}
550617

551618
async function processChangelog() {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
exports.config = {
2+
tests: './*_test.js',
3+
output: './output',
4+
helpers: {
5+
FileSystem: {},
6+
CustomHelper: {
7+
require: './custom_helper.js',
8+
},
9+
},
10+
include: {},
11+
bootstrap: false,
12+
mocha: {},
13+
name: 'step-enhancements tests',
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { store } = require('codeceptjs')
2+
3+
let retryCount = 0
4+
5+
class MyHelper {
6+
retryFewTimesAndPass(num) {
7+
if (retryCount < num) {
8+
retryCount++
9+
throw new Error('Failed on try ' + retryCount)
10+
}
11+
}
12+
13+
wait(timeout) {
14+
return new Promise(resolve => setTimeout(resolve, timeout))
15+
}
16+
17+
printOption() {
18+
if (store.currentStep?.opts) {
19+
console.log('Option:', store.currentStep?.opts?.text)
20+
}
21+
}
22+
}
23+
24+
module.exports = MyHelper
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const step = require('codeceptjs/steps')
2+
Feature('step-enhancements')
3+
4+
Scenario('test step opts', ({ I }) => {
5+
I.printOption(step.opts({ text: 'Hello' }))
6+
})
7+
8+
Scenario('test step timeouts', ({ I }) => {
9+
I.wait(1000, step.timeout(0.1))
10+
})
11+
12+
Scenario('test step retry', ({ I }) => {
13+
I.retryFewTimesAndPass(3, step.retry(4))
14+
})
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
Feature('no timeout');
1+
const step = require('codeceptjs/steps')
2+
3+
Feature('no timeout')
24

35
Scenario('no timeout test #first', ({ I }) => {
4-
I.waitForSleep(1000);
5-
});
6+
I.waitForSleep(1000)
7+
})
68

79
Scenario('timeout test in 0.5 #second', { timeout: 0.5 }, ({ I }) => {
8-
I.waitForSleep(1000);
9-
});
10+
I.waitForSleep(1000)
11+
})
1012

1113
Scenario('timeout step in 0.5', ({ I }) => {
12-
I.limitTime(0.2).waitForSleep(100);
13-
I.limitTime(0.1).waitForSleep(3000);
14-
});
14+
I.limitTime(0.2).waitForSleep(100)
15+
I.limitTime(0.1).waitForSleep(3000)
16+
})
17+
18+
Scenario('timeout step in 0.5 new syntax', ({ I }) => {
19+
I.waitForSleep(100, step.timeout(0.2))
20+
I.waitForSleep(3000, step.timeout(0.1))
21+
})
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const { expect } = require('expect')
2+
const exec = require('child_process').exec
3+
const { codecept_dir, codecept_run } = require('./consts')
4+
const debug = require('debug')('codeceptjs:tests')
5+
6+
const config_run_config = (config, grep, verbose = false) => `${codecept_run} ${verbose ? '--verbose' : ''} --config ${codecept_dir}/configs/step-enhancements/${config} ${grep ? `--grep "${grep}"` : ''}`
7+
8+
describe('CodeceptJS step-enhancements', function () {
9+
this.timeout(10000)
10+
11+
it('should apply step options', done => {
12+
exec(config_run_config('codecept.conf.js', 'opts', true), (err, stdout) => {
13+
debug(stdout)
14+
expect(stdout).toContain('Option: Hello')
15+
expect(stdout).toContain('options applied {"text":"Hello"}')
16+
expect(stdout).toContain('print option')
17+
expect(stdout).not.toContain('print option {"text":"Hello"}')
18+
expect(stdout).toContain('OK')
19+
expect(err).toBeFalsy()
20+
done()
21+
})
22+
})
23+
24+
it('should apply step timeouts', done => {
25+
exec(config_run_config('codecept.conf.js', 'timeouts', true), (err, stdout) => {
26+
debug(stdout)
27+
expect(err).toBeTruthy()
28+
expect(stdout).not.toContain('OK')
29+
expect(stdout).toContain('was interrupted on step timeout 100ms')
30+
done()
31+
})
32+
})
33+
34+
it('should apply step retry', done => {
35+
exec(config_run_config('codecept.conf.js', 'retry', true), (err, stdout) => {
36+
debug(stdout)
37+
expect(stdout).toContain('OK')
38+
expect(err).toBeFalsy()
39+
done()
40+
})
41+
})
42+
})

0 commit comments

Comments
 (0)