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'
}
}
Configure your
Gruntfile.js
similar to the example shown in the docs.cmd
tonpm
.run
andtest-jest
in theargs
Array.Gruntfile.js
Running
Running
$ grunt
via your CLI using the configuration shown above will invoke thenpm run test-jest
command.Note: Adding
--silent
(or it's shorthand equivalent-s
) to theargs
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 viacmd.exe
. The following error was thrown: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
Notes
grunt-shell
requires load-grunt-tasks for loading the Task instead of the typicalgrunt.loadNpmTasks(...)
, so you'll need to install that too:npm i -D load-grunt-tasks
grunt-shell
, namely version1.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 theexec
key instead of thecmd
andargs
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:Gruntfile.js