What are some JavaScript Unit Testing and Mocking

2019-01-29 22:10发布

My main JavaScript framework is jQuery so I would like my unit test and mocking frameworks to be compatible with that. I'd rather not have to introduce another JavaScript framework.

I am currently using QUnit for unit testing and Jack for mocking, but I am pretty new to the whole unit testing of JavaScript.

Does anyone else have a better tool to suggest? What has worked for you?

14条回答
Explosion°爆炸
2楼-- · 2019-01-29 22:27

JsUnit is run from either the browser, through its Eclipse plug-in, or automatically through an ANT task. You create an HTML page with a bunch of test functions, which must be named with the prefix ‘test’, include the JS file you are testing. When any assert within a function fails, the entire function fails and stops executing. There is no guaranteed order in which these tests are run. You can create setup() and teardown() functions.

License: GPL, GLPL, MPL

Pros

  • Automation is relatively easy to implement
  • A lot of functionality
  • Syntax is similar to JUnit

Cons

  • Not great for DOM testing since it runs tests inside an iFrame.
  • No guarantee that tests will be run in the order they are written.
  • Can’t use Firebug on testrunner page. Need to have another tab open with the actual test code.
查看更多
乱世女痞
3楼-- · 2019-01-29 22:29

I know you are asking for JQuery-compatible frameworks, but I want to throw script.aculo.us into the mix for completeness. They have a unit test suite that isn't bad.

查看更多
Fickle 薄情
4楼-- · 2019-01-29 22:30

I think that Jack is the best mocking framework for JavaScript as of the time of this writing. The main reason is that what's right for JavaScript is not likely what is right for a strongly typed language such as Java.

Many JavaScript mocking frameworks are inspired by Java mock frameworks (such as the excellent JsMockito, for example). But the problem with these is that they require dependency injection, because that's about the only reasonable way to use mocking in Java. But in JavaScript, there are MANY ways to use mocking, and you are not forced into using dependency injection everywhere.

For example, with JsMockito, you have to make mocks and then pass those mocks into your software-under-test (SUT). The SUT has to directly call the mocks. Therefore, you're forced to code the SUT as a constructor or function that takes in all its dependencies as parameters. (Sometimes, that's a fine way to implement it, but not in every case. The tail is wagging the dog if your mocking framework's design forces your implementation approach).

In JavaScript, it's very easy to "hijack" any function. Therefore, there are tons of ways to build something such that you can mock parts of it without explicitly injecting its dependencies into it. For example, Jack lets you mock any function, whether it is public or on a local object. From there you can spy on it, stub it, or express expectations on it. The key point is this: once you've mocked a function, ANY calls to that original function will instead be directed to your mock. In other words, your mocks will still get used even though the original, un-mocked function was called. As a result, you are not forced to inject dependencies, although you certainly can do so in those cases which call for it.

JavaScript is a different language than Java (and C#, etc.) It allows for different implementation idioms. Dependency injection is still one valuable tool in the toolbox in JavaScript, but it is not the only game in town anymore. Your mocking framework needs to know and respect that fact. Jack and a couple of others do, but of the ones that do, Jack appears to be the most mature and feature-rich.

查看更多
萌系小妹纸
5楼-- · 2019-01-29 22:33

For mocking in JavaScript take a look at qMock, a framework a colleague and I wrote to complement our use of QUnit. Although the latter is great for unit tests, it doesn't allow for very effective async/business logic testing. We havn't 'tagged' any release as stable, but there's some decent docs on there, and if you checkout the svn you'll see qmock itself has unit tests behind it which are fairly self-explanatory.

Oh, and to automate testing as part of the build we used a simple selenium script to navigate through our testsuite (one testing page per JS file), and 'listened' for a pass or fail CSS class (added by QUnit). This works headless as well for IE/FF2 AFAIK

查看更多
▲ chillily
6楼-- · 2019-01-29 22:36

We've been using jsspec jsspec. Its very nice if you like rspec and BDD. Just saw an article by Justin Gehtland on using it "headless" as well.

查看更多
地球回转人心会变
7楼-- · 2019-01-29 22:42

QUnit
jQUnit
Writing JS tests with QUnit and jQUnit

QUnit is the unit testing framework for the jQuery JavaScript framework. The testing framework itself uses the jQuery library, but the tests can be written for any JavaScript and do not require the code to use jQuery. JQUnit is a modified version of QUnit that adds in the setup, teardown, and assert functions that are more typical of an xUnit framework, and encapsulates everything in one global variable.

The visual interface of the testrunner page is nice, allowing you to drill down and see each assert in every test method. Writing tests is fairly easy, and you can run the test code directly on the testRunner page [8]. This allows for easy and visible DOM testing.

QUnit: MIT or GPL (choose) jQUnit: MIT License

Pros
- Asynchronous support
- Good for DOM testing
- Tests always run sequentially in the order they are added to a suite
- Debug on test page using firebug
- Syntax is similar to JUnit if using JQUnit, but simple to learn if using QUnit
Cons
- Automation would be difficult to implement

查看更多
登录 后发表回答