From 1c790ee6aa51d8c51e93f0953222801f3540b98d Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Fri, 23 Feb 2024 15:06:39 +0100 Subject: [PATCH] fix: more runner tests --- bin/codecept.js | 4 +- lib/command/list.js | 4 +- lib/command/run-multiple.js | 1 - lib/command/run-rerun.js | 2 +- lib/rerun.js | 4 +- runok.js | 19 ++++---- .../configs/step_timeout/first_test.js | 4 +- test/runner/definitions_test.js | 43 +++++++++++-------- test/runner/list_test.js | 6 ++- test/runner/run_multiple_test.js | 12 +++--- test/runner/run_rerun_test.js | 14 +++--- test/runner/session_test.js | 5 ++- test/runner/within_test.js | 18 +++----- 13 files changed, 72 insertions(+), 64 deletions(-) diff --git a/bin/codecept.js b/bin/codecept.js index b852fe138..77886cf31 100755 --- a/bin/codecept.js +++ b/bin/codecept.js @@ -15,7 +15,7 @@ import * as generate from '../lib/command/generate.js'; import * as run from '../lib/command/run.js'; import * as runWorkers from '../lib/command/run-workers.js'; import * as runMultiple from '../lib/command/run-multiple.js'; -import * as rerun from '../lib/command/run-rerun.js'; +import { runRerun } from '../lib/command/run-rerun.js'; import * as dryRun from '../lib/command/dryRun.js'; import * as info from '../lib/command/info.js'; @@ -244,7 +244,7 @@ program.command('run-rerun [test]') .option('--trace', 'trace function calls') .option('--child ', 'option for child processes') - .action(rerun); + .action(runRerun); program.on('command:*', (cmd) => { console.log(`\nUnknown command ${cmd}\n`); diff --git a/lib/command/list.js b/lib/command/list.js index 6c30961ce..3449b1b19 100644 --- a/lib/command/list.js +++ b/lib/command/list.js @@ -20,7 +20,7 @@ export default function (path) { methodsOfObject(helper).forEach((action) => { const params = getParamsToString(helper[action]); actions[action] = 1; - output.print(` ${output.colors.grey(name)} I.${output.colors.bold(action)}(${params})`); + output.print(` ${output.output.colors.grey(name)} I.${output.output.colors.bold(action)}(${params})`); }); } for (const name in supportI) { @@ -29,7 +29,7 @@ export default function (path) { } const actor = supportI[name]; const params = getParamsToString(actor); - output.print(` I.${output.colors.bold(name)}(${params})`); + output.print(` I.${output.output.colors.bold(name)}(${params})`); } output.print('PS: Actions are retrieved from enabled helpers. '); output.print('Implement custom actions in your helper classes.'); diff --git a/lib/command/run-multiple.js b/lib/command/run-multiple.js index ecd83c978..785a1c07d 100644 --- a/lib/command/run-multiple.js +++ b/lib/command/run-multiple.js @@ -11,7 +11,6 @@ import { getConfig, getTestRoot, fail } from './utils.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); const runner = path.join(__dirname, '../../bin/codecept.js'); -console.log(runner) let config; const childOpts = {}; const copyOptions = ['override', 'steps', 'reporter', 'verbose', 'config', 'reporter-options', 'grep', 'fgrep', 'invert', 'debug', 'plugins', 'colors']; diff --git a/lib/command/run-rerun.js b/lib/command/run-rerun.js index 9afb82ecb..8690bd118 100644 --- a/lib/command/run-rerun.js +++ b/lib/command/run-rerun.js @@ -4,7 +4,7 @@ import { import Config from '../config.js'; import Codecept from '../rerun.js'; -export default async function (test, options) { +export async function runRerun(test, options) { // registering options globally to use in config // Backward compatibility for --profile process.profile = options.profile; diff --git a/lib/rerun.js b/lib/rerun.js index 0ec53d7b3..53d34e802 100644 --- a/lib/rerun.js +++ b/lib/rerun.js @@ -11,7 +11,7 @@ class CodeceptRerunner extends BaseCodecept { container.createMocha(); const mocha = container.mocha(); this.testFiles.forEach((file) => { - delete require.cache[file]; + // delete require.cache[file]; }); mocha.files = this.testFiles; if (test) { @@ -53,7 +53,7 @@ class CodeceptRerunner extends BaseCodecept { try { await this.runOnce(test); successCounter++; - output.success(`\nProcess run ${rerunsCounter} of max ${maxReruns}, success runs ${successCounter}/${minSuccess}\n`); + output.output.success(`\nProcess run ${rerunsCounter} of max ${maxReruns}, success runs ${successCounter}/${minSuccess}\n`); } catch (e) { output.output.error(`\nFail run ${rerunsCounter} of max ${maxReruns}, success runs ${successCounter}/${minSuccess} \n`); console.error(e); diff --git a/runok.js b/runok.js index 582bb4987..a996a9bae 100755 --- a/runok.js +++ b/runok.js @@ -1,15 +1,16 @@ #!/usr/bin/env node -const fs = require('fs'); -const path = require('path'); -const axios = require('axios'); -const documentation = require('documentation'); +import fs from 'fs'; +import path from 'path'; +import axios from 'axios'; +import documentation from 'documentation'; +import runok0 from 'runok'; +import contributors from 'contributor-faces'; + const { stopOnFail, chdir, tasks: { git, copy, exec, replaceInFile, npmRun, npx, writeToFile, }, runok, -} = require('runok'); -const contributors = require('contributor-faces'); - +} = runok0; const helperMarkDownFile = function (name) { return `docs/helpers/${name}.md`; }; @@ -17,7 +18,7 @@ const documentjsCliArgs = '-f md --shallow --markdown-toc=false --sort-order=alp stopOnFail(); -module.exports = { +export default { async docs() { // generate all docs (runs all docs:* commands in parallel) await Promise.all([ @@ -491,4 +492,4 @@ async function processChangelog() { }); } -if (require.main === module) runok(module.exports); +//if (require.main === module) runok(module.exports); diff --git a/test/data/sandbox/configs/step_timeout/first_test.js b/test/data/sandbox/configs/step_timeout/first_test.js index 0d1e39da0..e2c10753c 100644 --- a/test/data/sandbox/configs/step_timeout/first_test.js +++ b/test/data/sandbox/configs/step_timeout/first_test.js @@ -1,4 +1,6 @@ -const given = when = then = global.codeceptjs.container.plugins('commentStep'); +const Container = require('../../../../../lib/container.js').default; + +const given = when = then = Container.plugins('commentStep'); const { I } = inject(); Feature('Steps'); diff --git a/test/runner/definitions_test.js b/test/runner/definitions_test.js index d485ded9d..2cae6d006 100644 --- a/test/runner/definitions_test.js +++ b/test/runner/definitions_test.js @@ -1,14 +1,19 @@ import fs from 'fs'; import assert from 'assert'; -import path from 'path'; +import path, { dirname } from 'path'; import { exec, execSync } from 'child_process'; import { Project, StructureKind, ts } from 'ts-morph'; +import { expect } from 'chai'; import chai_subset from 'chai-subset'; +import { fileURLToPath } from 'url'; +import { createRequire } from 'node:module'; -const __dirname = path.resolve('.'); -const runner = path.join(__dirname, 'bin/codecept.js'); -const codecept_dir = path.join(__dirname, 'test/data/sandbox/configs/definitions'); +const require = createRequire(import.meta.url); + +const __dirname = dirname(fileURLToPath(import.meta.url)); +const runner = path.join(__dirname, '../../bin/codecept.js'); +const codecept_dir = path.join(__dirname, '../../test/data/sandbox/configs/definitions'); const pathToRootOfProject = path.join(__dirname, '../../'); const pathOfStaticDefinitions = path.join(pathToRootOfProject, 'typings/index.d.ts'); const pathOfJSDocDefinitions = path.join(pathToRootOfProject, 'typings/types.d.ts'); @@ -54,11 +59,11 @@ describe('Definitions', function () { it('should have internal object that is available as variable codeceptjs', (done) => { exec(`${runner} def --config ${codecept_dir}/codecept.inject.po.js`, () => { const types = typesFrom(`${codecept_dir}/steps.d.ts`); - types.should.be.valid; + expect(types).to.be.valid; const definitionsFile = types.getSourceFileOrThrow(pathOfJSDocDefinitions); const index = definitionsFile.getModule('CodeceptJS').getModule('index').getStructure(); - index.statements.should.containSubset([ + expect(index.statements).to.containSubset([ { declarations: [{ name: 'recorder', type: 'CodeceptJS.recorder' }] }, { declarations: [{ name: 'event', type: 'typeof CodeceptJS.event' }] }, { declarations: [{ name: 'output', type: 'typeof CodeceptJS.output' }] }, @@ -66,7 +71,7 @@ describe('Definitions', function () { { declarations: [{ name: 'container', type: 'typeof CodeceptJS.Container' }] }, ]); const codeceptjs = types.getSourceFileOrThrow(pathOfStaticDefinitions).getVariableDeclarationOrThrow('codeceptjs').getStructure(); - codeceptjs.type.should.equal('typeof CodeceptJS.index'); + expect(codeceptjs.type).to.equal('typeof CodeceptJS.index'); done(); }); }); @@ -76,11 +81,11 @@ describe('Definitions', function () { exec(`${runner} def ${codecept_dir}`, (err, stdout) => { expect(stdout).to.include('Definitions were generated in steps.d.ts'); const types = typesFrom(`${codecept_dir}/steps.d.ts`); - types.should.be.valid; + expect(types).to.valid; const definitionFile = types.getSourceFileOrThrow(`${codecept_dir}/steps.d.ts`); const extend = getExtends(definitionFile.getModule('CodeceptJS').getInterfaceOrThrow('I')); - extend.should.containSubset([{ + expect(extend).to.containSubset([{ methods: [{ name: 'amInPath', returnType: 'void', @@ -100,12 +105,12 @@ describe('Definitions', function () { exec(`${runner} def --config ${codecept_dir}/codecept.inject.po.js`, (err, stdout) => { expect(stdout).to.include('Definitions were generated in steps.d.ts'); const types = typesFrom(`${codecept_dir}/steps.d.ts`); - types.should.be.valid; + expect(types).to.valid; const definitionFile = types.getSourceFileOrThrow(`${codecept_dir}/steps.d.ts`); const extend = definitionFile.getFullText(); - extend.should.include("type CurrentPage = typeof import('./po/custom_steps.js');"); + expect(extend).to.include("type CurrentPage = typeof import('./po/custom_steps.js');"); assert(!err); done(); }); @@ -115,7 +120,7 @@ describe('Definitions', function () { exec(`${runner} def --config ${codecept_dir}/../../codecept.ddt.js`, (err, stdout) => { expect(stdout).to.include('Definitions were generated in steps.d.ts'); const types = typesFrom(`${codecept_dir}/../../steps.d.ts`); - types.should.be.valid; + expect(types).to.valid; assert(!err); done(); }); @@ -124,7 +129,7 @@ describe('Definitions', function () { it('def should create definition file with support object', (done) => { exec(`${runner} def --config ${codecept_dir}/codecept.inject.po.js`, () => { const types = typesFrom(`${codecept_dir}/steps.d.ts`); - types.should.be.valid; + expect(types).to.valid; const definitionsFile = types.getSourceFileOrThrow(`${codecept_dir}/steps.d.ts`); const MyPage = getAliasStructure(definitionsFile.getTypeAliasOrThrow('MyPage')); @@ -148,7 +153,7 @@ describe('Definitions', function () { it('def should create definition file with inject which contains support objects', (done) => { exec(`${runner} def --config ${codecept_dir}/codecept.inject.po.js`, () => { const types = typesFrom(`${codecept_dir}/steps.d.ts`); - types.should.be.valid; + expect(types).to.valid; const definitionsFile = types.getSourceFileOrThrow(pathOfStaticDefinitions); const returned = getReturnStructure(definitionsFile.getFunctionOrThrow('inject')); @@ -166,7 +171,7 @@ describe('Definitions', function () { exec(`${runner} def --config ${codecept_dir}/codecept.inject.po.js`, (err) => { assert(!err); const types = typesFrom(`${codecept_dir}/steps.d.ts`); - types.should.be.valid; + expect(types).to.valid; const definitionsFile = types.getSourceFileOrThrow(pathOfStaticDefinitions); const returned = getReturnStructure(definitionsFile.getFunctionOrThrow('inject')); @@ -185,7 +190,7 @@ describe('Definitions', function () { it('def should create definition file with inject which contains I object from helpers', (done) => { exec(`${runner} def --config ${codecept_dir}/codecept.inject.powi.js`, () => { const types = typesFrom(`${codecept_dir}/steps.d.ts`); - types.should.be.valid; + expect(types).to.valid; const definitionsFile = types.getSourceFileOrThrow(pathOfStaticDefinitions); const returned = getReturnStructure(definitionsFile.getFunctionOrThrow('inject')); @@ -199,7 +204,7 @@ describe('Definitions', function () { it('def should create definition file with callback params', (done) => { exec(`${runner} def --config ${codecept_dir}/codecept.inject.po.js`, () => { const types = typesFrom(`${codecept_dir}/steps.d.ts`); - types.should.be.valid; + expect(types).to.valid; const definitionsFile = types.getSourceFileOrThrow(`${codecept_dir}/steps.d.ts`); const CallbackOrder = definitionsFile.getModule('CodeceptJS').getInterfaceOrThrow('SupportObject').getStructure(); @@ -216,11 +221,11 @@ describe('Definitions', function () { exec(`${runner} def --config ${codecept_dir}/codecept.promise.based.js`, (err, stdout) => { expect(stdout).to.include('Definitions were generated in steps.d.ts'); const types = typesFrom(`${codecept_dir}/steps.d.ts`); - types.should.be.valid; + expect(types).to.valid; const definitionFile = types.getSourceFileOrThrow(`${codecept_dir}/steps.d.ts`); const extend = getExtends(definitionFile.getModule('CodeceptJS').getInterfaceOrThrow('I')); - extend.should.containSubset([{ + expect(extend).to.containSubset([{ methods: [{ name: 'amInPath', returnType: 'Promise', diff --git a/test/runner/list_test.js b/test/runner/list_test.js index 03ee4f599..216dc33e9 100644 --- a/test/runner/list_test.js +++ b/test/runner/list_test.js @@ -1,7 +1,8 @@ import assert from 'assert'; -import path, {dirname} from 'path'; +import path, { dirname } from 'path'; import { exec } from 'child_process'; -import {fileURLToPath} from "url"; +import { fileURLToPath } from 'url'; +import { expect } from 'chai'; const __dirname = dirname(fileURLToPath(import.meta.url)); const runner = path.join(__dirname, '../../bin/codecept.js'); @@ -10,6 +11,7 @@ const codecept_dir = path.join(__dirname, '../../test/data/sandbox'); describe('list commands', () => { it('list should print actions', (done) => { exec(`${runner} list ${codecept_dir}`, (err, stdout) => { + console.log(stdout, err) expect(stdout).to.include('FileSystem'); // helper name expect(stdout).to.include('FileSystem I.amInPath(openPath)'); // action name expect(stdout).to.include('FileSystem I.seeFile(name)'); diff --git a/test/runner/run_multiple_test.js b/test/runner/run_multiple_test.js index 672397c43..c4bba16e6 100644 --- a/test/runner/run_multiple_test.js +++ b/test/runner/run_multiple_test.js @@ -180,9 +180,9 @@ describe('CodeceptJS Multiple Runner', function () { it('should exit with non-zero code for failures during init process', (done) => { process.chdir(codecept_dir); exec(`${runner} run-multiple --config codecept.multiple.initFailure.js default --all`, (err, stdout) => { - expect(err).not.toBeFalsy(); + expect(err).not.to.false; expect(err.code).toBe(1); - expect(stdout).toContain('Failed on FailureHelper'); + expect(stdout).contain('Failed on FailureHelper'); done(); }); }); @@ -190,10 +190,10 @@ describe('CodeceptJS Multiple Runner', function () { it('should exit code 1 when error in config', (done) => { process.chdir(codecept_dir); exec(`${runner} run-multiple --config configs/codecept-invalid.config.js default --all`, (err, stdout, stderr) => { - expect(stdout).not.toContain('UnhandledPromiseRejectionWarning'); - expect(stderr).not.toContain('UnhandledPromiseRejectionWarning'); - expect(stdout).toContain('badFn is not defined'); - expect(err).not.toBe(null); + expect(stdout).not.contain('UnhandledPromiseRejectionWarning'); + expect(stderr).not.contain('UnhandledPromiseRejectionWarning'); + expect(stdout).contain('badFn is not defined'); + expect(err).not.null; done(); }); }); diff --git a/test/runner/run_rerun_test.js b/test/runner/run_rerun_test.js index ebd2fb140..54d88a495 100644 --- a/test/runner/run_rerun_test.js +++ b/test/runner/run_rerun_test.js @@ -1,22 +1,24 @@ import { expect } from 'expect'; import { describe } from 'mocha'; -import path from 'path'; +import path, { dirname } from 'path'; import { exec } from 'child_process'; -import semver from 'semver'; +import { fileURLToPath } from 'url'; -const __dirname = path.resolve('.'); -const runner = path.join(__dirname, 'bin/codecept.js'); -const codecept_dir = path.join(__dirname, 'test/data/sandbox/configs/run-rerun/'); +const __dirname = dirname(fileURLToPath(import.meta.url)); +const runner = path.join(__dirname, '../../bin/codecept.js'); +const codecept_dir = path.join(__dirname, '../../test/data/sandbox/configs/run-rerun'); const codecept_run = `${runner} run-rerun`; const codecept_run_config = (config, grep) => `${codecept_run} --config ${codecept_dir}/${config} --grep "${grep || ''}"`; describe('run-rerun command', () => { before(() => { + global.codecept_dir = codecept_dir; process.chdir(codecept_dir); }); - it('should display count of attemps', (done) => { + it('should display count of attempts', (done) => { exec(`${codecept_run_config('codecept.conf.js')} --debug`, (err, stdout) => { + console.log(err, stdout) const runs = stdout.split('Run Rerun - Command --'); // check first run diff --git a/test/runner/session_test.js b/test/runner/session_test.js index aa0a0393b..7b6940c7e 100644 --- a/test/runner/session_test.js +++ b/test/runner/session_test.js @@ -1,6 +1,7 @@ -import path, {dirname} from 'path'; +import path, { dirname } from 'path'; import { exec } from 'child_process'; -import {fileURLToPath} from "url"; +import { fileURLToPath } from 'url'; +import { grepLines } from '../../lib/utils.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); const runner = path.join(__dirname, '../../bin/codecept.js'); diff --git a/test/runner/within_test.js b/test/runner/within_test.js index 4a706d5ae..4bfaf90bb 100644 --- a/test/runner/within_test.js +++ b/test/runner/within_test.js @@ -1,8 +1,8 @@ -import path, {dirname} from 'path'; +import path, { dirname } from 'path'; import { exec } from 'child_process'; import { expect } from 'chai'; +import { fileURLToPath } from 'url'; import { grepLines } from '../../lib/utils.js'; -import {fileURLToPath} from "url"; const __dirname = dirname(fileURLToPath(import.meta.url)); const runner = path.join(__dirname, '../../bin/codecept.js'); @@ -19,20 +19,16 @@ describe('CodeceptJS within', function () { }); it('should execute if no generators', (done) => { - exec(`${codecept_run} --verbose`, (_err, stdout) => { - console.log(`${codecept_run} --debug`) + exec(`${codecept_run} --steps`, (_err, stdout) => { const lines = stdout.match(/\S.+/g); const withoutGeneratorList = grepLines(lines, 'Check within without generator', 'Check within with generator. Yield is first in order'); - testStatus = withoutGeneratorList.pop(); - expect(testStatus).to.include('OK'); - withoutGeneratorList.should.eql([ - 'I small promise ', - 'I small promise was finished ', - 'I hey! i am within begin. i get blabla ', + expect(lines.join(' ')).to.include('OK'); + expect(lines).to.include.members([ + 'I small promise', + 'I hey! i am within begin. i get blabla', 'Within "blabla" ""', 'I small promise ', - 'I small promise was finished ', 'I oh! i am within end( ', ], 'check steps execution order'); done();