0

I would like to create a function that can return the result by npm test.

(by call this function npm.commands.test(packages, callback) https://docs.npmjs.com/api/test)

I tried to use that like this:

var npm = require("npm");
npm.load('', function(err, npm) {
  npm.commands.test('package.json', function(data) {
  });
});

It could run such as command npm test, but I don't know how to get the result? and what is the callback function like?

0

1 Answer 1

1

npm.commands.test executes the test command defined in the package.json file of the current project. Additionally, npm.commands.test redirects the first parameter to the test command.

Example

package.json:
{
    ...
    "scripts": {
        "test": "mocha"
    }
    ...
}
code:
var npm = require('npm');
...
npm.commands.test('myTestDirectory', function(err){
    // if one of more tests failed, err will be set to the string:
    // 'Test failed.  See above for more details.'
    // Else, err will be undefined.
});
calls:
/path/to/project/root> mocha "myTestDirectory"

In some situations, especially if you want to execute tests of another project or package, it probably makes more sense to use the child_process module to spawn a new process of npm test and access the output over the process' stderr and stdout streams or call the test runner of the particular project directly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.