mocha
mocha copied to clipboard
🐛 Bug: mocha fails with `spawn EINVAL` on windows with node 22
Bug Report Checklist
- [X] I have read and agree to Mocha's Code of Conduct and Contributing Guidelines
- [X] I have searched for related issues and issues with the
faqlabel, but none matched my issue. - [X] I have '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, my usage of Mocha, or Mocha itself.
- [ ] I want to provide a PR to resolve this
Expected
Tests with mochajs successfully start to execute without errors on GHA CI on windows
Actual
CI fails with:
Running "mochacli:test" (mochacli) task
Warning: spawn EINVAL Use --force to continue.
Aborted due to warnings.
Tests are not even started to execute.
Example of failed pipeline: https://github.com/objectionary/eo2js/actions/runs/9693490408/job/26749043096
Minimal, Reproducible Example
I run just regular js tests via GHA on windows using Gruntfile.js:
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: ['temp'],
mochacli: {
test: {
options: {
timeout: '1200000',
files: ['test/**/*.test.js', '!test/resources/**'],
},
},
}
})
grunt.loadNpmTasks('grunt-mocha-cli')
grunt.registerTask('default', ['mochacli'])
}
regular.test.js:
const assert = require('assert')
it('', function() {
assert.ok(true)
})
Versions
- Tests run via GHA
- OS: windows-latest
- node_modules/.bin/mocha --version: 10.5.2
- node --version: 22
Full CI script is here
Additional Info
My little research tells me that issue is related to April node security release and can be resolved by passing { shell: true } option to spawn function. But here there's no such option. I think the problem may be here
Our windows tests passes? See eg. #5185. Are you sure this issue is with mocha and not with grunt? Since its grunt that's invoked initially:
cd eo2js-runtime && grunt
And the code path that calls https://github.com/mochajs/mocha/blob/b2a6358f860dae732cfcbe5ed86693eadb199fac/bin/mocha.js#L105 is triggered in the tests eg here, so if mocha is failing it should show in these tests I think https://github.com/mochajs/mocha/blob/819b17292dbd4cc3528c2554de33c1b8bd7c3278/test/integration/esm.spec.js#L75
Maybe its this spawn that you are hitting instead? https://github.com/Rowno/grunt-mocha-cli/blob/be6eeb40cac537b8b016c6693956574ab153a59c/lib/index.js#L127
👋 friendly ping @maxonfjvipon, what do you think?
Closing as this doesn't seem to be a Mocha thing. Cheers! 🤎
Sorry for being away for a long time. You all were absolutely right, the problem is in grunt-mocha-cli. The repository seems to be kind of abandoned so it's worthless to make a ticket and wait until it's fixed. So I decided to fix it locally:
- add
"patch-package"as dev dependency to youpackage.jsonfile - after
npm installgo tonode_modules/grunt-mocha-cli/lib/index.jsand modify it. I added the next code at the line 74:
if (os.platform() === 'win32') {
spawnOptions.opts.shell = true
}
-
run
npx patch-package grunt-mocha-cli. This will create a patch file insidepatches/folder in your root directory. Mine looks like this: -
add the next post-install script to you
package.json:
"scripts": {
"postinstall": "patch-package"
}
That's it. Hope it'll help