首页 > 代码库 > [Node.js] Configuring npm package.json scripts

[Node.js] Configuring npm package.json scripts

With a node package manager‘s (npm) package.json script property, you can preconfigure common tasks like running unit tests with npm $SCRIPT_NAME.

 

package.json:

{  "name": "commonJSBroswerfiy",  "version": "0.0.0",  "description": "",  "main": "index.js",  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1",    "checkVersion": "mocha --version",    "fnTest": "mocha app"  },  "author": "",  "license": "MIT",  "dependencies": {    "underscore": "^1.7.0"  },  "devDependencies": {    "bower": "^1.3.12",    "chai": "^1.10.0",    "mocha": "^2.0.1"  }}

 

If you run ‘npm test‘, it will go to the scripts tag to run test script.

$ npm test

echo "Error: no test specified" && exit 1

 

You can set up you own test:

"checkVersion": "mocha --version","fnTest": "mocha app"

run:

npm run checkVersionnpm run fnTest

 

See more: 

mocha,

chai

 

app.js:

var up = require(‘./dep‘),    chai = require(‘chai‘),    expect = chai.expect,    assert = chai.assert;var should = require(‘chai‘).should();describe(‘my file‘, function () {    xit(‘should convert strings to upper case‘, function () {        expect(up(‘hello‘)).to.equal(‘HELLO‘);    });    xit(‘should be a string‘, function() {        var one = up(‘one‘);        //assert.typeOf(one, ‘Array‘); //false        assert.typeOf(one, ‘String‘); //true    });    it(‘string string length should be 10‘, function() {        var loveMessage = up(‘I love you‘);        loveMessage.should.have.length(10);    });});

dep.js:

module.exports = function (str) {    return str.toUpperCase();}

 

[Node.js] Configuring npm package.json scripts