siesta-lite
Version:
Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers
188 lines (125 loc) • 6.37 kB
Markdown
Extending test class
==================
As your test suite grows you often notice that you are repeating certain pieces of code in your tests. You should then
consider extending the Siesta test class with your own utility and assertion methods.
It can be just a simple helper method returning some pre-populated data store, or a new assertion method which reports
results to the project like any other.
Extending the Siesta test class
---------
All Siesta assertions are methods, belonging to the {@link Siesta.Test} class. To create a new assertion method you
will need to subclass the test class. When creating assertions purposed for testing JavaScript code in browsers -
subclass the {@link Siesta.Test.Browser}. For testing NodeJS code - use {@link Siesta.Test.NodeJS} as your base class.
Siesta is written using the [Joose](http://joose.it) class system. The following example will show you the typical scenario
for subclassing the test class. For more information, please refer to [Joose docs](http://joose.github.io/Joose/doc/html/Joose/Manual.html).
Let's create 2 special assertions, which will be checking the odd parity of a passed number. Usually, an assertion
needs to check its statement and report the result with either {@link Siesta.Test#pass} or {@link Siesta.Test#fail} methods.
Class('MyProject.MyTestClass', {
isa : Siesta.Test.Browser,
methods : {
isOdd : (number, description) => {
if (number % 2) {
this.pass(description);
} else {
this.fail(description, {
assertionName : 'isOdd',
got : number,
annotation : 'Need odd number'
});
}
},
isEven : (number, description) => {
if (!(number % 2)) {
this.pass(description);
} else {
this.fail(description, {
assertionName : 'isEven',
got : number,
annotation : 'Need even number'
});
}
}
}
})
When failing, try to provide as much information about the failure as possible and format the failure message in a readable form.
Please refer to {@link Siesta.Test#fail} method documentation for additional options.
To make the project use your new test class, you have to specify the test class to use, by setting the
{@link Siesta.Project#testClass testClass} configuration option:
project.configure({
title : 'Awesome Test Suite',
testClass : MyProject.MyTestClass,
preload : [
...
]
})
The test class should be loaded right after the `siesta-all.js` file in the browser project wrapper file:
<!DOCTYPE html>
<html>
<head>
...
<link rel="stylesheet" type="text/css" href="__path_to_siesta__/resources/css/siesta-all.css">
<script type="text/javascript" src="__path_to_siesta__/siesta-all.js"></script>
<!-- The file with new test class -->
<script type="text/javascript" src="lib/MyTestClass.js"></script>
<script type="text/javascript" src="siesta.js"></script>
</head>
<body>
</body>
</html>
Now you can use your custom assertion or utility methods in all your tests:
StartTest('My test', t => {
var nbr = 1;
t.isEven(nbr); // Will fail
})
Things to note
---------
- `this` inside of assertion methods corresponds to the test instance (often seen as `t` in the examples)
- The code of the test class is executed in the context of the project file. So, in browsers, the value of `window` inside of an
assertion method is different from the `window` inside of the file with `StartTest`.
- To access the context of the test, use "this.global". So, to get access to `window` object of the test file inside of the assertion
method, use `this.global`
- When performing asynchronous operations inside of a helper method - always wrap them with
{@link Siesta.Test#beginAsync beginAsync}/{@link Siesta.Test#endAsync endAsync} calls.
Custom setup/tearDown code
------------
Sometimes, you need to execute some code before every test start. For this purpose you can use the {@link Siesta.Test#setup setup} method,
please refer to its documentation for details. There's also {@link Siesta.Test#isReady} method, which is slightly lower-level.
After the end of test, a {@link Siesta.Test#tearDown} method can be used for cleanup.
Calling the superclass method
-------------
If needed, you can create several test classes for your project. They can inherit from each other. Here's how you call a
superclass method in Joose class:
Class('Test.Class1', {
isa : Siesta.Test.Browser,
methods : {
doSomething : (arg1, arg2) => {
}
}
})
Class('Test.Class2', {
isa : Test.Class1,
methods : {
doSomething : (arg1, arg2) => {
this.SUPER(arg1, args)
// or, to pass args as array
this.SUPERARG(arguments)
}
}
})
Make sure you call the parent implementation if you override some important methods, like `initialize`, `finalize`, etc.
See more info in [Joose.Manual](http://joose.github.io/Joose/doc/html/Joose/Manual.html)
Buy this product
---------
Visit our store: <https://bryntum.com/store/siesta>
Support
---------
Ask questions in our community forum: <https://www.bryntum.com/forum/viewforum.php?f=20>
Subscribers can post expedited questions in Premium Forum: <https://www.bryntum.com/forum/viewforum.php?f=21>
Please report any bugs through the web interface at <https://www.assembla.com/spaces/bryntum/support/tickets>
See also
---------
Siesta web-page: <https://bryntum.com/products/siesta>
Other Bryntum products: <https://bryntum.com/products>
COPYRIGHT AND LICENSE
---------
Copyright (c) 2009-2022, Bryntum AB & Nickolay Platonov
All rights reserved.