UNPKG

siesta-lite

Version:

Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers

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