Screw.Unit 0.3

May 4, 2008 Pivotal Labs

Screw.Unit 0.3 is now available. I’m skipping 0.2 since there have been two milestones worth of additions to the new version of Screw.Unit. New features:

  • test suite functionality
  • afters
  • global befores and afters
  • enhanced error messages
  • new matchers

Here’s how to do a global before:

Screw.Unit(function() {
  before(function() {
    $("#screw_unit_content").html("");
    ActiveAjaxRequests.length = 0;
    PainPoint.instances = [];
    delete window._token;
  });
});

Put this, for instance, in your spec_helper.js file.

Here is how to manage a suite:

Have a suite.html file that requires the Screw.Unit source, your spec helper, and your various test files.

<html>
  <head>
    <script src="../lib/jquery-1.2.3.js"></script>
    <script src="../lib/jquery.fn.js"></script>
    <script src="../lib/jquery.print.js"></script>
    <script src="../lib/screw.builder.js"></script>
    <script src="../lib/screw.matchers.js"></script>
    <script src="../lib/screw.events.js"></script>
    <script src="../lib/screw.behaviors.js"></script>
    <script src="spec_helper.js"></script> <!-- SPEC HELPER -->
    <script src="behaviors_spec.js"></script><!-- A SPEC -->
    <script src="matchers_spec.js"></script> <!-- ANOTHER SPEC -->
    <link rel="stylesheet" href="../lib/screw.css">
    </head>
    <body></body>
  </html>

Have a test file (for example, matchers_spec.js):

Screw.Unit(function() {
  var global_before_invoked = false;
  before(function() { global_before_invoked = true });

  describe('Behaviors', function() {
    describe('#run', function() {
      describe("a simple [describe]", function() {
        it("invokes the global [before] before an [it]", function() {
          expect(global_before_invoked).to(equal, true);
        });
  ...
});

Extra Powerful Matchers

// hash equality
expect({a: 'b', c: 'd'}).to(equal, {a: 'b', c: 'd'});

// recursive array equality
expect([{a: 'b'}, {c: 'd'}]).to(equal, [{a: 'b'}, {c: 'd'}]);

// regular expressions and string inclusion
expect("The wheels of the bus").to(match, /bus/);
expect("The wheels of the bus").to(match, "wheels");

// array emptiness:
expect([]).to(be_empty);
expect([1]).to_not(be_empty);

Thanks To

  • Brian Takita for the ideas and much of the implementation.

About the Author

Biography

Previous
Better Javascript testing through ScrewUnit
Better Javascript testing through ScrewUnit

The following is an excerpt of ScrewUnit's new copious documentation: Writing Good Tests A great test max...

Next
use photobooth to exchange ideas quickly
use photobooth to exchange ideas quickly

Draw something on an index card Run photobooth Edit => Auto Flip New Photos Take a photo of your index card...

SpringOne Platform 2019 Presentations

Watch Now