Testing with Polymerize

Unit testing is nowaday a fundamental part of any sound and well driven software project. Until version 0.9.4 of polymerize there weren’t any framework that could support the developer in writing unit test for a polymerize dart project.

Since version 0.9.4 things are changed and you can write tests using the package:test you are used to but with some little changes. In this post we will see what this differerence are and how to write unit tests for polymerize.

Writing test for polymerize

Writing a test for polymerize is not so different from writing test for any other dart packages using package:test. the main difference is how you should write the html wrapper file and in the pubspec.yaml.

Infact we have to provide a different javascript boot script than the usual one. Let’s see an example.

The pubspec.yaml

In order to run the test environment pub requires the pub_serve transformer to be specified in the pubspec.yaml. This is not needed by polymerize because the normal polymerize transformer will automatically detect the presence of unit tests in your code and apply the needed adaptation required to make it run in the test runner.

The pub tool though will make a sanity check of your pubspec.yaml and will complain if it doesn’t see any pub_serve transformer. So we have to fool it by specifying an pub_serve transformer with a fake $exclude pattern, for example :

1
2
3
4
5
6
7
8
9
10
11
12
13
    name: my_project
    version: 1.0.0
    dependencies:
      ...
      test: ^0.12.23
      ...
    transformers:
     - polymerize:
        entry-points: 
          - web/*.dart
          - test/*.dart
     - test/pub_serve:
        $include: xtest/*.dart

At line 8 of example above the polymerize transformer is specified and an entry point pattern (line 11) is added for the test folder too.

At line 12 the pub_serve transformer is specified a pattern that will actually disable the transformer excluding every dart test files (do not use an $exclude filter because there’s a issue with pub that will fail compiling).

As we have already noted the pub_serve transformer is specified only to make pub stop complaining about its absence, the real one is the polymerize transformer.

The html wrapper

You can avoid writing a custom html wrapper if you don’t need it in your test. In that case a standard one will be automatically generated for you.

If you have to write one then you have to follow the general direction documented here but with a different javascript boot script, here’s an example:

    <!doctype html>
    <html>
      <head>
        <title>Custom HTML Test</title>
        <link rel="x-dart-test" href="todo_test.dart">
        <script src="polymerize_require/dart_test.js"></script>
        <!-- NOTE YOU MUST LEAVE COMMENTED THIS OTHERWISE PUB TEST WILL REFUSE TO EXECUTE THE TEST
         <script src="packages/test/dart.js"></script>
        -->
      </head>
      <body>
        // ...
      </body>
    </html>

So the boot script will be polymerize_require/dart_test.js instead of packages/test/dart.js; but again, to make pub stop complaining about the “missing” boot script, we have to fool it by leaving the original test script tag inside a comment, like in the example above. Luckily enough pub is not that clever to understand that the tag is commented.

The unit test

Finally you can write the test. The only difference here is that you have to add the @entryPoint on any top level directive or definition (like for example the main function).

You can examine and try a working example in the usual demo project:

running_test

Running the test

A final note is about how to run the test. You can’t use dartium because we are using dartdevc (that is supposed to replace dartium sometime in the future) and you have to run the transformer so you have to run pub serve, in the end to make a long story short, this is the two command line you will have to run:

# You run this once and leave it running in the background
pub serve test &   
# Once pub serve is running launch this command to execute the test 
pub run test --pub-serve=8080 -p chrome