Skip to content

fixed pause finishing, added suggest for empty run, included fuse.js … #4713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 32 additions & 17 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,42 @@ on:
- '**'

jobs:
build:
unit-tests:
name: Unit tests
runs-on: ubuntu-22.04

strategy:
matrix:
node-version: [20.x, 22.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm i
env:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: true
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
- run: npm run test:unit

runner-tests:
name: Runner tests
runs-on: ubuntu-22.04

strategy:
matrix:
node-version: [20.x]
node-version: [20.x, 22.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm i --force
env:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: true
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
- uses: nick-fields/retry@v3
with:
timeout_minutes: 6
max_attempts: 3
retry_on: error
command: npm test
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm i
env:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: true
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
- run: npm run test:runner
1 change: 1 addition & 0 deletions lib/codecept.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class Codecept {
runHook(require('./listener/globalTimeout'))
runHook(require('./listener/globalRetry'))
runHook(require('./listener/exit'))
runHook(require('./listener/emptyRun'))

// custom hooks (previous iteration of plugins)
this.config.hooks.forEach(hook => runHook(hook))
Expand Down
58 changes: 58 additions & 0 deletions lib/listener/emptyRun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const figures = require('figures')
const Container = require('../container')
const event = require('../event')
const output = require('../output')

module.exports = function () {
let isEmptyRun = true

event.dispatcher.on(event.test.before, test => {
isEmptyRun = false
})

event.dispatcher.on(event.all.result, () => {
if (isEmptyRun) {
const mocha = Container.mocha()

if (mocha.options.grep) {
const Fuse = require('fuse.js')

output.print()
output.print('No tests found by pattern: ' + mocha.options.grep)

const allTests = []
mocha.suite.suites.forEach(suite => {
suite.tests.forEach(test => {
allTests.push(test.fullTitle())
})
})

const fuse = new Fuse(allTests, {
includeScore: true,
threshold: 0.6,
caseSensitive: false,
})

const results = fuse.search(mocha.options.grep.toString())

if (results.length > 0) {
output.print()
output.print('Maybe you wanted to run one of these tests?')
results.forEach(result => {
output.print(figures.checkboxOff, output.styles.log(result.item))
})

output.print()
output.print(output.styles.debug('To run the first test use the following command:'))
output.print(output.styles.bold('npx codeceptjs run --debug --grep "' + results[0].item + '"'))
}
}
if (process.env.CI && !process.env.DONT_FAIL_ON_EMPTY_RUN) {
output.print()
output.error('No tests were executed. Failing on CI to avoid false positives')
output.error('To disable this check, set `DONT_FAIL_ON_EMPTY_RUN` environment variable to true in CI config')
process.exitCode = 1
}
}
})
}
3 changes: 0 additions & 3 deletions lib/mocha/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const fs = require('fs')
const reporter = require('./cli')
const gherkinParser = require('./gherkin')
const output = require('../output')
const { genTestId } = require('../utils')
const ConnectionRefused = require('../helper/errors/ConnectionRefused')

const scenarioUi = fsPath.join(__dirname, './ui.js')
Expand Down Expand Up @@ -45,8 +44,6 @@ class MochaFactory {
let missingFeatureInFile = []
const seenTests = []
mocha.suite.eachTest(test => {
test.uid = genTestId(test)

const name = test.fullTitle()
if (seenTests.includes(test.uid)) {
dupes.push(name)
Expand Down
2 changes: 2 additions & 0 deletions lib/mocha/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Test = require('mocha/lib/test')
const { test: testWrapper } = require('./asyncWrapper')
const { enhanceMochaSuite } = require('./suite')
const { genTestId } = require('../utils')

/**
* Factory function to create enhanced tests
Expand Down Expand Up @@ -40,6 +41,7 @@ function enhanceMochaTest(test) {
suite.addTest(testWrapper(this))
test.tags = [...(test.tags || []), ...(suite.tags || [])]
test.fullTitle = () => `${suite.title}: ${test.title}`
test.uid = genTestId(test)
}

test.applyOptions = function (opts) {
Expand Down
1 change: 1 addition & 0 deletions lib/mocha/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Test as MochaTest, Suite as MochaSuite } from 'mocha'
declare global {
namespace CodeceptJS {
interface Test extends MochaTest {
uid: string
title: string
tags: string[]
steps: string[]
Expand Down
38 changes: 31 additions & 7 deletions lib/pause.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const colors = require('chalk')
const readline = require('readline')
const ora = require('ora-classic')
const debug = require('debug')('codeceptjs:pause')
const Fuse = require('fuse.js')

const container = require('./container')
const history = require('./history')
Expand Down Expand Up @@ -29,10 +30,20 @@ const pause = function (passedObject = {}) {
// add listener to all next steps to provide next() functionality
event.dispatcher.on(event.step.after, () => {
recorder.add('Start next pause session', () => {
// test already finished, nothing to pause
if (!store.currentTest) return
if (!next) return
return pauseSession()
})
})

event.dispatcher.on(event.test.finished, () => {
finish()
recorder.session.restore('pause')
rl.close()
history.save()
})

recorder.add('Start new session', () => pauseSession(passedObject))
}

Expand Down Expand Up @@ -72,18 +83,20 @@ function pauseSession(passedObject = {}) {
})
return new Promise(resolve => {
finish = resolve
// eslint-disable-next-line
return askForStep()
})
}

/* eslint-disable */
async function parseInput(cmd) {
rl.pause()
next = false
recorder.session.start('pause')
if (cmd === '') next = true
if (!cmd || cmd === 'resume' || cmd === 'exit') {
finish()
recorder.session.restore()
recorder.session.restore('pause')
rl.close()
history.save()
return nextStep()
Expand Down Expand Up @@ -184,6 +197,7 @@ async function parseInput(cmd) {
recorder.add('ask for next step', askForStep)
nextStep()
}
/* eslint-enable */

function askForStep() {
return new Promise(resolve => {
Expand All @@ -197,13 +211,23 @@ function askForStep() {
function completer(line) {
const I = container.support('I')
const completions = methodsOfObject(I)
const hits = completions.filter(c => {
if (c.indexOf(line) === 0) {
return c
}
return null
// If no input, return all completions
if (!line) {
return [completions, line]
}

// Initialize Fuse with completions
const fuse = new Fuse(completions, {
threshold: 0.3,
distance: 100,
minMatchCharLength: 1,
})
return [hits && hits.length ? hits : completions, line]

// Search using Fuse.js
const searchResults = fuse.search(line)
const hits = searchResults.map(result => result.item)

return [hits, line]
}

function registerVariable(name, value) {
Expand Down
Loading
Loading