Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Bug: Watch mode (-w) does not re-run after a run did not find any tests #3851

Closed
4 tasks done
stigok opened this issue Mar 28, 2019 · 8 comments
Closed
4 tasks done
Labels
type: bug a defect, confirmed by a maintainer

Comments

@stigok
Copy link

stigok commented Mar 28, 2019

Prerequisites

  • Checked that your issue hasn't already been filed by cross-referencing issues with the faq label
  • Checked next-gen ES issues and syntax problems by using the same environment and/or transpiler configuration without Mocha to ensure it isn't just a feature that actually isn't supported in the environment in question or a bug in your code.
  • 'Smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, your usage of Mocha, or Mocha itself
  • Ensured that there is no discrepancy between the locally and globally installed versions of Mocha. You can find them with: node node_modules/.bin/mocha --version(Local) and mocha --version(Global). We recommend that you not install Mocha globally.

Description

Starting mocha with -w watch argument does not make mocha re-run on file save when no tests were executed in the previous run.

Steps to Reproduce

  1. Create a file test-test.js with contents describe('hello');
  2. Run mocha -w test-test.js
  3. Edit file and see mocha not re-running the test suite

Expected behavior: mocha -w re-runs the test suite when file is edited again

Actual behavior: mocha -w process hangs on the stats from the last run, failing to re-run after file has been edited

Reproduces how often: 100%

Versions

  • The output of mocha --version and node node_modules/.bin/mocha --version: 6.0.2 and 6.0.2
  • The output of node --version: v11.10.1
  • Your operating system
    • name and version: Arch Linux 5.0.3-arch1-1-ARCH
    • architecture (32 or 64-bit): 64-bit
  • Your shell (e.g., bash, zsh, PowerShell, cmd): bash
  • Your browser and version (if running browser tests):
  • Any third-party Mocha-related modules (and their versions):
  • Any code transpiler (e.g., TypeScript, CoffeeScript, Babel) being used (and its version):
@plroebuck
Copy link
Contributor

plroebuck commented Mar 29, 2019

Your reproduction steps are too imprecise to be usable.

  1. File "test-test.js" with just those contents contains no tests. Not sure what you were expecting since there's nothing for Mocha to run to begin with...
  2. Edit file how? What change(s) did you make?

@stigok
Copy link
Author

stigok commented Mar 30, 2019

I'll try to be more precise. When I say update here, open up your editor and change the file contents.

  1. Create a file bug-test.js with contents describe('hello world', () => it('works'));
  2. Run mocha -w bug-test.js
  3. Update the contents of bug-test.js with: describe('hello world', () => {});
  4. Update the contents of bug-test.js with: describe('hello world', () => it('does not trigger mocha'));

@plroebuck
Copy link
Contributor

Automated MCVE (UN*X-only)

$ cd /var/tmp
$ mkdir testwatch
$ cd testwatch
$ mkdir test bin
$ cat < EOF > "package.json"
{
  "name": "testwatch",
  "version": "1.0.0",
  "description": "Issue 3851",
  "main": "index.js",
  "devDependencies": {
    "mocha": "latest"
  },
  "directories": {
    "bin": "./bin",
    "test": "./test"
  },
  "scripts": {
    "test": "mocha",
    "test:watch": "mocha --watch"
  },
  "keywords": [
    "mocha"
  ],
  "author": "P. Roebuck <plroebuck@users.noreply.github.com>",
  "license": "ISC"
}
EOF
$ npm install
$ cat < EOF > "bin/run_test.sh"
#! /bin/sh
  
PROJROOT="/var/tmp/testwatch"
SPECFILE="${PROJROOT}/test/bug-test.spec.js"
#RUNDIR="/var/run"                   # user acct has no permission to write
RUNDIR="/tmp"
PIDFILE="${RUNDIR}/testwatch.pid"

## Modify test specification
function init_test_script {
  echo "describe('hello world', () => it('works'));" > ${SPECFILE}
}

function use_unmodified_test_script {
  echo "**** unmodified" >&2; \
  cat ${SPECFILE} >&2
}

function _use_empty_test_script {
  echo "describe('hello world', () => {});" > ${SPECFILE}
}

function use_empty_test_script {
  echo "**** replace with empty describe" >&2; \
  _use_empty_test_script; \
  cat ${SPECFILE} >&2
}

function _use_fixed_test_script {
  echo "describe('hello world', () => it('does not trigger mocha'));" > ${SPECFILE}
}

function use_fixed_test_script {
  echo "**** replace with valid describe" >&2; \
  _use_fixed_test_script; \
  cat ${SPECFILE} >&2
}

## Utility
function create_pidfile {
  echo "$(ps -ef | grep _mocha | grep -v grep | awk '{print $2}')" > ${PIDFILE}
}

function kill_mocha {
  pid=$(cat ${PIDFILE})
  kill ${pid}
}

function pause {
  sleep 10
}

function terminate {
  echo "**** done" >&2; \
  kill_mocha
}

## Cleanup
function cleanup {
  err=$?
  init_test_script
  trap '' EXIT INT QUIT TERM
  tput init
  exit $err
}

function sig_cleanup {
    trap '' EXIT # some shells will call EXIT after the INT handler
    false # sets $?
    cleanup
}
trap cleanup EXIT
trap sig_cleanup INT QUIT TERM

##
## Main
##
init_test_script

use_unmodified_test_script
(env DEBUG=* npm run test:watch &) && \
  sleep 5; create_pidfile && \
  pause; use_empty_test_script && \
  pause; use_fixed_test_script && \
  pause; terminate

EOF
$ chmod 755 bin/run_test.sh

Run test

$ cd /var/tmp/testwatch
$ bin/run_test.sh

@plroebuck
Copy link
Contributor

I can reproduce your results, but I can't tell you whether it's by design or not at this point (don't use --watch myself).

@juergba
Copy link
Member

juergba commented Apr 11, 2019

Mocha's watch option is far from working perfectly, there are a few open issues about that.

I guess this bug has something to do with that Mocha loads its test files with require(), which means the files are loaded only ones and then cached. And the cached files don't change when the original file in the directory is modified.

@juergba
Copy link
Member

juergba commented May 13, 2019

@stigok do you have some time left for me?
Could you patch "lib\cli\run-helpers.js" aprox. ln 234:

  • old line: const watchFiles = utils.files(cwd, extension);
  • new line: const watchFiles = files.length ? files : utils.files(cwd, extension);

and give me some feedback, please?

EDIT: it's not working, yet ...

@juergba juergba added type: bug a defect, confirmed by a maintainer and removed unconfirmed-bug labels May 13, 2019
@juergba juergba self-assigned this May 13, 2019
@stigok
Copy link
Author

stigok commented May 21, 2019

No, it made no difference.

@JoshuaKGoldberg JoshuaKGoldberg changed the title -w does not re-run after a run did not find any tests 馃悰 Bug: Watch mode (-w) does not re-run after a run did not find any tests Dec 27, 2023
@JoshuaKGoldberg
Copy link
Member

This doesn't reproduce for me on Mocha 10.2.0. Hooray 馃コ! Maybe #3912 happened to fix this?

If anybody is still seeing this, please file a new issue with one of our new issue templates. They'll prompt for the info needed to help us triage the new issue. Cheers all! 馃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

No branches or pull requests

4 participants