简体   繁体   中英

How can I pipe code and unit test to npm test?

I would like to unit test on the fly on my server without writing temporary files. Is there a way to pass both the code to be tested and the unit test to npm test ? The npm test documentation is kind of bare ( https://docs.npmjs.com/cli/test ). Also, it doesn't have to be npm test if there is an alternative solution.

Thank you, Jens

npm test is simply a convenient method to execute a npm package's test suite without having to worry about the details of how the suite should be run. If you run it on a project that has no scripts entry for test , you get Error: no test specified . If you setup a package.json with this:

{
  "scripts": {
    "test": "echo This is a test!"
   }
}

Then npm test will echo to the console This is a test! . What you do in a real project is set a script that launches the real testing tool. For instance, mocha is a test runner, so once it is installed, you could have:

  "scripts": {
    "test": "mocha"
   }

Or you could have Karma, or Jest, or tap, or what-have-you.

You wrote in the bounty notice:

Some websites offer online testing for JavaScript code, are they really writing out temporary files with user code to test against unit tests with npm test?

The websites that offer online testing offer very different services. So depending on which online testing service you are referring to, sometimes the answer is yes, sometimes no.

  • Travis-ci offers a service that runs your entire test on their server. To do so, it checks out the branch under test from a git repository so yes they are getting a copy of the user code. Note that this is quite independent of which test runner you've set npm test to run: whether you run Mocha or Karma, or some other thing, Travis checks out the branch under test and then runs npm test .

  • SauceLabs and BrowserStack on the other hand don't run the suite themselves but provide virtual machines that launch browsers to test against. Your test suite still runs on a machine of yours. So they don't get a copy of the code being tested. Again, which runner you use does not matter. I've run suites using these services with Mocha, Karma, Behave, etc.

Focusing on your title now:

How can I pipe code and unit test to npm test?

No testing setup I know of pipes the code under test. The most common method of providing the code under test, and the tests themselves is to checkout code from a repository like Travis does. (Note that this does not require you to make your code public. Testing services can work with private repositories.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM