javascript 如何调试使用 Testacular (Karma) 运行的 Jasmine 规范?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14412437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I debug a Jasmine spec run with Testacular (Karma)?
提问by Buzzy
I have a small project with Jasmine specs and I am using Testacular as my test runner. I don't understand how I can debug, both the app code or the spec code. When I try to set a breakpoint in Chrome Dev Tools it isn't hit next time the specs run because it loads the files every time with a new query string.
我有一个 Jasmine 规格的小项目,我使用 Testacular 作为我的测试运行器。我不明白如何调试应用程序代码或规范代码。当我尝试在 Chrome Dev Tools 中设置断点时,它不会在下次运行规范时命中,因为它每次都使用新的查询字符串加载文件。
The best thing I found so far is to use console.log() but I would rather use Chrome Dev Tools breakpoints.
到目前为止,我发现最好的方法是使用 console.log() 但我更愿意使用 Chrome Dev Tools 断点。
(I am using Visual Studio 2012 for development.)
(我使用 Visual Studio 2012 进行开发。)
Thanks
谢谢
回答by Joseph Eames
Testacular is not the best tool to use for debugging. It's power lies in the fact that it will run your tests in multiple browsers, and do it EXTREMELY quickly, and can do it every time you change a file, so therefore it will tell you if you have broken a test. But if you need to debug, it's not the best tool.
Testacular 不是用于调试的最佳工具。它的强大之处在于它可以在多个浏览器中运行您的测试,而且运行速度非常快,并且可以在每次更改文件时进行,因此它会告诉您是否破坏了测试。但是如果你需要调试,它不是最好的工具。
You can indeed put a "debugger" statement in your code to cause it to break, but you may end up hitting that same breakpoint dozens or more times in your tests if that is a common line of code that is hit in multiple tests. Where perhaps it's only breaking in one given scenario, so you have to skip all the breakpoints except the one occurrence where you are seeing a problem. If you are using mocha or jasmine there is a way to run only a single test in your entire test suite. With jasmine that's changing that one test from it() to iit(), with mocha it's it.only(). But even so, testacular still is the wrong tool for this job.
您确实可以在代码中放置一个“调试器”语句以使其中断,但是如果这是在多个测试中命中的公共代码行,您最终可能会在测试中多次或多次命中相同的断点。也许它只在一个给定的场景中中断,所以你必须跳过所有断点,除了你看到问题的那一次。如果您使用 mocha 或 jasmine,有一种方法可以在整个测试套件中只运行一个测试。jasmine 将测试从 it() 更改为 iit(),而 mocha 则是 it.only()。但即便如此,睾丸仍然是这项工作的错误工具。
A far better solution is to use a different test "setup" and just run the single test that's breaking. This is easily accomplished using jasmine or mocha or whatever your test framework of choice is. You will already be writing your testacular tests in one of those frameworks since testacular is an automation tool and not a test framework. So just create a test runner file and using that, load the file up, and if you're using chrome, go into the dev tools, hit Command-O on MAC or Control-O on windows, and select the file you wish to put a breakpoint in, and set your breakpoint, and you're cooking with gas.
一个更好的解决方案是使用不同的测试“设置”,然后只运行一个有问题的测试。这可以使用 jasmine 或 mocha 或您选择的任何测试框架轻松完成。您将已经在其中一个框架中编写您的 testacular 测试,因为 testacular 是一种自动化工具而不是测试框架。因此,只需创建一个测试运行程序文件并使用它,加载文件,如果您使用的是 chrome,请进入开发工具,在 MAC 上点击 Command-O 或在 Windows 上点击 Control-O,然后选择您想要的文件设置一个断点,并设置断点,然后你就可以用煤气做饭了。
Using the traditional "test runner" with your test framework won't clash with using testacular at all. The two will run in concert happily.
在您的测试框架中使用传统的“测试运行器”不会与使用 testacular 发生冲突。两人将在音乐会上愉快地奔跑。
Here's links to my preferred articles for doing this in the major 3 test frameworks:
以下是我在主要 3 个测试框架中执行此操作的首选文章的链接:
jasmine: http://net.tutsplus.com/tutorials/javascript-ajax/testing-your-javascript-with-jasmine/
茉莉花:http: //net.tutsplus.com/tutorials/javascript-ajax/testing-your-javascript-with-jasmine/
QUnit: http://www.testdrivenjs.com/getting-started/qunit-setup/
QUnit:http: //www.testdrivenjs.com/getting-started/qunit-setup/
Mocha: I don't have a link to a good article for this. By the middle of February 2013 my PluralSight,com course on testing clientside JavaScript will be published and you can find it there, along with detailed directions on setting up QUnit and Jasmine. They have a short free trial that you can use to view the content without paying. This URL will link to that course when it gets published. http://pluralsight.com/training/Authors/Details/joe-eames
摩卡:我没有一个好文章的链接。到 2013 年 2 月中旬,我的 PluralSight,com 测试客户端 JavaScript 课程将发布,您可以在那里找到它,以及有关设置 QUnit 和 Jasmine 的详细说明。他们有一个简短的免费试用期,您可以使用它来查看内容而无需付费。该 URL 将在发布时链接到该课程。 http://pluralsight.com/training/Authors/Details/joe-eames
回答by Mark Rushakoff
You can use the statement debugger;
by itself, and Chrome will break on that statement as long as the developer tools panel is open and breakpoints are enabled.
您可以单独使用该语句debugger;
,只要开发人员工具面板打开并启用断点,Chrome 就会中断该语句。
回答by Andy Armstrong
My favorite approach to debugging tests with Karma (was Testacular) is to use this plugin:
我最喜欢使用 Karma(是 Testacular)调试测试的方法是使用这个插件:
I start up Karma like this:
我像这样启动 Karma:
karma start karma.conf.js --browsers=Chrome --single-run=false --reporters=kjhtml
This opens Karma in debug mode with a "Debug" button. When you click that, it runs all the tests in a Chrome window and shows the results as they go by. At this point you can use Developer Tools, set breakpoints, step into code etc as normal.
这将使用“调试”按钮在调试模式下打开 Karma。当您单击它时,它会在 Chrome 窗口中运行所有测试并显示结果。此时,您可以正常使用开发人员工具、设置断点、单步执行代码等。
Even better, you can click on a failing test and it will switch to a mode where it only runs that one test. Now you can quickly test and debug that one test without having to wait for all the others to run.
更好的是,您可以单击失败的测试,它会切换到仅运行该测试的模式。现在,您可以快速测试和调试该测试,而无需等待所有其他测试运行。
To see this in action, here's a GitHub pull request where I added this library to our project:
要查看它的实际效果,这里是一个 GitHub 拉取请求,我将这个库添加到我们的项目中: