64

I'm trying to learn grunt. When I run npm init, I get a prompt in the process of creating a package.json file that asks for "test command" - I'm not sure how to utilize this, or what it's expecting. It doesn't seem to be well documented. If I leave it blank, I get this in the resulting package.json file:

"scripts": {
    //"test": "echo \"Error: no test specified\" && exit 1"
  },

Can anybody shed some light on how to set up a test script?

mheavers
  • 29,530
  • 58
  • 194
  • 315

2 Answers2

44

at first, the scripts-property in your package.json has nothing to do with grunt itself. its just a cli-command from npm, wich will be run if you run

$ npm test

read more about that here: https://npmjs.org/doc/scripts.html

e.g. if you test your application with the grunt & nodeunit you could just add that to the scripts-block

"scripts": {
  "test": "grunt nodeunit"
}

and your nodeunit-task is run if you run 'npm test'

this basically makes it easier for continuous integration and so on, if you change your underlying testframework.

of course you could add an alias-task if you need more to be done before and after your tests are run (e.g. concatenation before, cleanup after)

hereandnow78
  • 14,094
  • 8
  • 42
  • 48
  • In extension to this you can do package.json with mutiple commands in npm test, like if you wanna grunt and compile your sass you could just do - "test": "grunt && compass compile" – Simon Dragsbæk Jun 12 '14 at 08:57
  • @SimonPertersen: i would not do it like this. there is a grunt-compass plugin out there. if you want to run tests and afterwards compile compass, you should use this plugin, register an alias task which runs this chain, and let npm test run your alias task! – hereandnow78 Jun 12 '14 at 12:19
  • It was just an example you could do tons of commands or use it for other stuff it was just because i had this on my hand - but alias tasks is only for the grunt, can i run terminal commands aswell? – Simon Dragsbæk Jun 12 '14 at 12:49
  • of course you can, but for almost everything you want to do there, will be a grunt plugin available. if not, write one for the task you want to do! – hereandnow78 Jun 14 '14 at 12:07
0

As mentioned in the answer above you can run your test command when you specify it during creation of package json from cmd or by editing the json file manually. Basically as per the npm docs it is used to run the provided package's test script.

npm docs test

In my case , Iam using it to test an angular application using Jasmine(spec.js files) a sample usage can be found in this article :-

Getting Started with Node.js and Jasmine

Vibhu
  • 536
  • 1
  • 3
  • 11