纽约市伊斯坦布尔的仪器是什么? [英] What is instrumentation in nyc istanbul?

查看:115
本文介绍了纽约市伊斯坦布尔的仪器是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

纽约市使用什么仪器?

What is instumentation used in nyc?

nyc的instrument命令可用于在单元测试的上下文之外检测源文件:

nyc's instrument command can be used to instrument source files outside of the context of your unit-tests:

我认为它将在单元测试之外进行覆盖.我尝试过

I assume it will do coverage outside unit-testing. I tried it with

nyc instrument src coverage/instrument

然后运行应用程序并尝试命中端点

then run the application and tries hitting an endpoint

npm start

但是当我做以上操作时,它不会在nyc_output中生成文件,因此无法报告任何内容.

but when I do above, it doesn't generate a file in nyc_output thus can't report anything.

我必须完成nyc instrument命令吗?怎么做呢?

Do I have to finish the nyc instrument command? how to do so?

推荐答案

nyc instrument用于检测代码.它产生的输出在运行时将收集覆盖率数据.除非您实际上对该数据进行了某些操作,例如报告或以某种方式使用它,否则这将毫无用处.当您运行已检测到的文件时,我相信它将在global.__coverage__中存储coverage数据.然后,您可以使用该数据执行所需的操作.因此,您可以创建一个将运行检测到的文件的报告程序,然后查看global.__coverage__以查看覆盖范围.只需运行检测文件就不会产生任何输出

nyc instrument is used to instrument your code. It produces output that, when ran, will gather coverage data. This is useless unless you actually do something with that data... like report it or use it somehow. When you run a file that has been instrumented, it will store coverage data in global.__coverage__ I believe. You can then do what you want with that data. So, you could create a reporter that will run the instrumented file, then take a look at global.__coverage__ to see what the coverage is like. Simply running an instrumented file won't generate any output

要查看已检测文件的覆盖范围,您可以在已检测文件的位置创建自己的报告程序,然后查看global.__coverage__或运行nyc命令生成与正常情况一样的覆盖率数据.

To see what the coverage is of a file that has been instrumented, you can either create your own reporter where you require the instrumented file, then take a look at global.__coverage__ or you could run the nyc command to generate coverage data like normal.

以下是一些示例:

假设您有一个文件file.js要检查其覆盖范围,并运行了以下命令:

Let's say you have a file file.js that you want to check the coverage of and you've run the command:

nyc instrument file.js > file_instrumented.js

现在,您将拥有一个名为file_instrumented.js的文件,其中包含生成代码覆盖率所需的所有代码.

Now, you'll have a file named file_instrumented.js that has all the code necessary to generate code coverage.

如果我使用node file_instumented.js运行该文件,则没有任何反应...除了文件的执行方式与file.js

If I run that file with node file_instumented.js nothing happens... other than the file executes same as file.js

但是,如果我使用以下代码创建名为coverage.js的文件:

But, if I create a file named coverage.js with this code:

require("./file_instrumented.js");
console.log(global.__coverage__)

然后,我运行node coverage.js,您将能够看到覆盖率数据.然后,您可以输出所需的任何数据.这是对覆盖率数据的较低级别访问

Then, I run node coverage.js you'll be able to see coverage data. You can then output whatever data you want. It's sort of a lower level access to the coverage data

如果要在nyc_output中生成报告,则需要对已检测文件使用nyc命令.例如,如下所示:

If you want to generate a report in nyc_output you'll need to use the nyc command against the instrumented file. For example, something like this:

nyc --reporter=text --report-dir=./nyc_output node file_instrumented.js

如果将file_instrumented.js文件设为可执行文件,这样的命令也将起作用:

A command like this would work too if you made the file_instrumented.js file executable:

nyc --reporter=text --report-dir=./nyc_output file_instrumented.js

但是,如果我们尝试像这样对原始的file.js运行相同的命令:

However, if we try to run that same command against the original file.js like this:

nyc --reporter=text --report-dir=./nyc_output node file.js

您会看到我们收到了一份报告,其中没有涵盖任何内容.这是因为未插入file.js文件,因此没有向nyc报告者提供要报告的任何数据

You'll see that we get a report that shows no coverage. And this is because the file.js file isn't instrumented and therefore doesn't give the nyc reporter any data to report

您是正确的,如上文所述,使用nyc instrument可以在单元测试框架之外进行覆盖.这有点令人困惑,因为文档不够清晰.关于如何在测试框架之外获取文件的覆盖范围,没有很好的例子,因此我通过查看nyc的源代码以及一些测试框架来弄清了所有这些内容.

You are correct that using nyc instrument will do coverage outside unit-testing frameworks as I've demonstrated above. It's a bit confusing because the docs aren't as clear as they should be. There are no good examples that I could find on how to get coverage of files outside of test frameworks so I figured this all out by looking at the source code for nyc as well as some of the testing frameworks.

问题是测试框架会为您检测文件,因此当您使用Mocha测试框架运行这样的命令时,例如:

The thing is that the testing frameworks instrument the file for you so when you run a command like this using the Mocha testing framework for example:

nyc --reporter=text mocha --ui bdd test.js

正在发生的事情是:
-nyc正在执行mocha ...
-然后mocha正在幕后为您检测代码
-然后mocha正在运行该检测代码
-在收集覆盖率数据时运行测试
-为nyc提供生成报告所需的global.__coverage__
-最后,nyc使用该数据在您的nyc_output文件夹中输出报告

What's happening is:
- nyc is executing mocha...
- then mocha is instrumenting your code for you behind the scenes
- then mocha is running that instrumented code
- which runs tests while collecting coverage data
- which gives nyc the global.__coverage__ that it needs to generate a report
- finally, nyc uses that data to output a report in your nyc_output folder

希望这一切都有道理...

Hope this all makes sense...

这篇关于纽约市伊斯坦布尔的仪器是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆