9

I have a npm task in my package.json file as follows to execute jest testing:

  "scripts": {
    "test-jest": "jest",
    "jest-coverage": "jest --coverage"
  },
  "jest": {
    "testEnvironment": "jsdom"
  },

I want to execute this task npm run test-jest using grunt. I installed grunt-run for the same and added the run task, but how do I invoke this npm task there?

    run: {
        options: {
          // Task-specific options go here.
        },
        your_target: {
          cmd: 'node'
        }
      }
Canta
  • 1,480
  • 1
  • 13
  • 26
Peter
  • 10,492
  • 21
  • 82
  • 132

1 Answers1

18

Configure your Gruntfile.js similar to the example shown in the docs.

  1. Set the value for the cmd to npm.
  2. Set run and test-jest in the args Array.

Gruntfile.js

module.exports = function (grunt) {

  grunt.loadNpmTasks('grunt-run');

  grunt.initConfig({
    run: {
      options: {
        // ...
      },
      npm_test_jest: {
        cmd: 'npm',
        args: [
          'run',
          'test-jest',
          '--silent'
        ]
      }
    }
  });

  grunt.registerTask('default', [ 'run:npm_test_jest' ]);

};

Running

Running $ grunt via your CLI using the configuration shown above will invoke the npm run test-jest command.

Note: Adding --silent (or it's shorthand equivalent -s) to the args Array simply helps avoids the additional npm log to the console.


EDIT:

Cross Platform

Using the grunt-run solution shown above failed on Windows OS when running via cmd.exe. The following error was thrown:

Error: spawn npm ENOENT Warning: non-zero exit code -4058 Use --force to continue.

For a cross-platform solution consider installing and utlizing grunt-shell to invoke the npm run test-jest instead.

npm i -D grunt-shell

Gruntfile.js

module.exports = function (grunt) {

  require('load-grunt-tasks')(grunt); // <-- uses `load-grunt-tasks`

  grunt.initConfig({
    shell: {
      npm_test_jest: {
        command: 'npm run test-jest --silent',
      }
    }
  });

  grunt.registerTask('default', [ 'shell:npm_test_jest' ]);

};

Notes

  1. grunt-shell requires load-grunt-tasks for loading the Task instead of the typical grunt.loadNpmTasks(...), so you'll need to install that too:

npm i -D load-grunt-tasks

  1. For older version of Windows I had to install an older version of grunt-shell, namely version 1.3.0, so I recommend installing an earlier version.

npm i -D grunt-shell@1.3.0


EDIT 2

grunt-run does seem to work on Windows if you use the exec key instead of the cmd and args keys...

For cross platform purposes... I found it necessary to specify the command as a single string using the exec key as per the documentation that reads:

If you would like to specify your command as a single string, useful for specifying multiple commands in one task, use the exec: key

Gruntfile.js

module.exports = function (grunt) {

  grunt.loadNpmTasks('grunt-run');

  grunt.initConfig({
    run: {
      options: {
        // ...
      },
      npm_test_jest: {
        exec: 'npm run test-jest --silent' // <-- use the exec key.
      }
    }
  });

  grunt.registerTask('default', [ 'run:npm_test_jest' ]);

};
RobC
  • 22,977
  • 20
  • 73
  • 80
  • Running the task via CLI threw me this error: Running "run:npm_test_jest" (run) task >> Error: spawn npm ENOENT Warning: non-zero exit code -4058 Use --force to continue. Aborted due to warnings. – Peter Nov 16 '17 at 04:37
  • Yes, using `grunt-run` I also got the same error when running on Windows - yet it works successfully on Mac. Did you try using [grunt-shell](https://github.com/sindresorhus/grunt-shell) instead of `grunt-run` - as explained in the _Cross Platform_ section of my answer? Using `grunt-shell` was successfully for me on both Windows and Mac. – RobC Nov 16 '17 at 08:33
  • Thanks Manu - I've actually just found a way to use `grunt-run` cross platform using the `exec` key instead. So If your preference is to use `grunt-run` instead of `grunt-shell` then see my updated answer under the _EDIT 2_ section. – RobC Nov 16 '17 at 09:11
  • That works like a champ! Plus I was able to use the shell task independently like grunt shell:npm_test_jest and it worked, but if I registered it with another task and ran that one then it tries to run endlessly with no execution. Switched to grunt-run! – Peter Nov 16 '17 at 09:21
  • @RobC - while this is helpful, still not having success with edit2 version. My grunt.js is: `module.exports = function (grunt) { // Activate on subgrunt grunt.initConfig({ run: { build: { exec: 'npm', args: [ 'run', 'build' ] } } }); grunt.loadNpmTasks('grunt-run'); grunt.registerTask('default', ['run']); grunt.registerTask('build', ['run']); };` – Mark Sep 23 '18 at 03:45
  • 1
    @Mark - Have you tried completely omitting the `args` key/value, (as pèr my recommendation in edit2), and placing the `npm run build` command as a value of the `exec` key. For instance: `grunt.initConfig({ run: { build: { exec: 'npm run build' } } });` – RobC Sep 23 '18 at 17:13
  • @RobC - thanks, tried that, but still issue. Problem here is the dev team that spun this up and still uses it are on Macs and I prefer Windows. That said, my initial question [in the thread](https://stackoverflow.com/questions/52453809/running-grunt-with-node-in-windows-fails) with the error referencing error 1 and this: `mkdir -p static/build` still exists. Any further recommendations please? – Mark Sep 23 '18 at 21:03
  • 1
    @Mark - Try running `npm run build` directly in your command-line and if the errors still persist, then it would be indicative that the error has nothing to do with `grunt-run`. It's most probably related to a nodejs script which you are running. In your `app.js` script you definitely need to change what's suggested in [this comment](https://stackoverflow.com/questions/52453809/running-grunt-with-node-in-windows-fails#comment-91850612). Once running `npm run build` directly in your command-line is successful, then utilizing `grunt-run` should be ok. – RobC Sep 24 '18 at 08:22