Run JavaScript unit tests inside of Visual Studio
I have been searching for a good way to run JavaScript unit tests inside of the Visual Studio IDE. I currently use TestDriven.net to run my C# units tests and it is very convenient to be able to quickly get the result of my tests in the output pane. I would love to find a similar experience for JavaScript (ideally working with TestDriven.net).
I have read about different solutions that let you execute JavaScrpt unit tests. Some have their own JS engine while others like JS-Test-Driver are able to send the code to the browsers and fetch the results. But I have yet to see something that is i开发者_运维技巧ntegrated into VS.
Does anyone know of an extension that might do this?
After nine months there are now a couple answers to this question.
I created an open source project called Chutzpah - A JavaScript Test Runner. Chutzpah enables you to run JavaScript unit tests from the command line and from inside of Visual Studio. It also supports running in the TeamCity continuous integration server.
Another solution is part of the next version of Resharper. In Resharper 6 there is an integrated QUnit test runner.
It is possible to use JsTestDriver to be a test-runner in Visual Studio. Once a server has been started, with browsers attached, one can run tests directly from within Visual Studio.
The Console-window will then give the output of the test results. I won't go to implementation details here, but the following how-to should be enough to get you started on the actual setup of Visual Studio / JsTestRunner.
Console output from chrome and internet explorer (ignore my bad test-names):
JsTestDriver is mainly a test-running tool to verify multiple browsers. To get good unit-tests on the javascript itself, one can plug in other test-specific tools like JasmineBDD (jasmine to jstestdriver adapter).
JsTestDriver also opens up for the possibility to test against multiple browsers as a build step on your continuous integration server ie: Hudson (Continuous Integration with Hudson and jstestdriver). This then allows a dev to test against a certain browser or two while developing locally, but then verify the result against any range of OS / browser combinations on the build server.
Just found this article (and this question) when looking for the same thing.
Integrating JavaScript Unit Tests with Visual Studio - Steven Walther
It does have a lot of work onto it, but it seems that it really brings a nice interaction. For sure worth the hassle if you're working on a JavaScript heavy application.
Microsoft has a link recommending the use a NodeJS app to run our JavaScript Unit Tests. Visual Studio's test window can now see tests we write in that project even when the overall project is in C#:
- https://learn.microsoft.com/en-us/visualstudio/javascript/unit-testing-javascript-with-visual-studio?view=vs-2019&tabs=mocha
One of the testing library combos (unit testing / mocking / assertions) recommended is using Mocha / Sinon / Chai. For this flavor there's a useful tutorial located here:
- https://scotch.io/tutorials/how-to-test-nodejs-apps-using-mocha-chai-and-sinonjs
And another link for getting vanilla JS to work in the node environment:
- https://dev.to/thawkin3/how-to-unit-test-html-and-vanilla-javascript-without-a-ui-framework-4io
精彩评论