简体   繁体   English

如何将代码和单元测试传递给npm测试?

[英]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 ? 有没有办法将要测试的代码和单元测试都传递给npm test The npm test documentation is kind of bare ( https://docs.npmjs.com/cli/test ). npm测试文档几乎没有内容( https://docs.npmjs.com/cli/test )。 Also, it doesn't have to be npm test if there is an alternative solution. 另外,如果有替代解决方案,则不必进行npm test

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. npm test只是执行npm软件包的测试套件的一种简便方法,而不必担心套件如何运行的细节。 If you run it on a project that has no scripts entry for test , you get Error: no test specified . 如果您在没有要test scripts条目的项目上运行它,则会出现Error: no test specified If you setup a package.json with this: 如果您使用以下命令设置package.json

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

Then npm test will echo to the console This is a test! 然后npm test将回显到控制台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: 例如, mocha是一个测试运行器,因此,一旦安装,您就可以拥有:

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

Or you could have Karma, or Jest, or tap, or what-have-you. 或者,您可能拥有Karma,Jest或水龙头,或者您拥有什么。

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? 一些网站提供了JavaScript代码的在线测试功能,是否真的使用用户代码写出临时文件来通过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. Travis-ci提供一项服务,可在其服务器上运行您的整个测试。 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. 为此,它从git存储库中检出了受测分支,因此,是的,他们正在获取用户代码的副本。 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 . 请注意,这与您设置要运行npm test运行npm test完全无关:无论运行Mocha还是Karma,还是其他原因,Travis都会检出被测分支,然后运行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. 另一方面,SauceLabs和BrowserStack本身不会运行套件,而是提供启动浏览器进行测试的虚拟机。 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. 我使用Mocha,Karma,Behave等使用这些服务运行套件。

Focusing on your title now: 现在关注您的标题:

How can I pipe code and unit test to npm test? 如何将代码和单元测试传递给npm测试?

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. 提供被测代码的最常见方法以及测试本身就是像Travis一样从存储库中检出代码 (Note that this does not require you to make your code public. Testing services can work with private repositories.) (请注意,这并不要求您公开代码。测试服务可以与私有存储库一起使用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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