mocha icon indicating copy to clipboard operation
mocha copied to clipboard

🐛 Bug: mocha fails with `spawn EINVAL` on windows with node 22

Open maxonfjvipon opened this issue 1 year ago • 2 comments
trafficstars

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 faq label, 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

maxonfjvipon avatar Jun 27 '24 09:06 maxonfjvipon

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

voxpelli avatar Aug 05 '24 14:08 voxpelli

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

voxpelli avatar Aug 05 '24 15:08 voxpelli

👋 friendly ping @maxonfjvipon, what do you think?

JoshuaKGoldberg avatar Oct 30 '24 16:10 JoshuaKGoldberg

Closing as this doesn't seem to be a Mocha thing. Cheers! 🤎

JoshuaKGoldberg avatar Nov 06 '24 21:11 JoshuaKGoldberg

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:

  1. add "patch-package" as dev dependency to you package.json file
  2. after npm install go to node_modules/grunt-mocha-cli/lib/index.js and modify it. I added the next code at the line 74:
if (os.platform() === 'win32') {
  spawnOptions.opts.shell = true
}
  1. run npx patch-package grunt-mocha-cli. This will create a patch file inside patches/ folder in your root directory. Mine looks like this: Image

  2. add the next post-install script to you package.json:

"scripts": {
  "postinstall": "patch-package"
}

That's it. Hope it'll help

maxonfjvipon avatar Jan 07 '25 15:01 maxonfjvipon