diff --git a/pages/guide/advanced_tests.md b/pages/guide/advanced_tests.md new file mode 100644 index 0000000..2d28080 --- /dev/null +++ b/pages/guide/advanced_tests.md @@ -0,0 +1,263 @@ +--- +layout: guide +section: guide +role: page +order: 60 +toc: true +title: Advanced Tests +label: Advanced Tests +description: | + Advanced Tests +--- + +## Advanced Tests + +{:.toc} +## Test Execution Options + +{% highlight TypeScript linenos %} +import { suite, test, retries, slow, timeout } from '@testdeck/mocha'; + +@suite +class Suite { + + @test(retries(5)) + @timeout(1500) + retried(done) { + + setTimeout(done, 100); + } + + @test(slow(1000), timeout(2000)) + @retries(2) + slow(done) { + + setTimeout(done, 2500); + } +} +{% endhighlight %} + + +{:.toc} +### retries {% include support-mocha.html %} + +`retries` can be used as either a decorator or passed as a parameter to the `@test` decorator. + +{% highlight TypeScript linenos %} +import { suite, test, retries } from '@testdeck/mocha'; + +@suite +class Suite { + + @test(retries(2)) + @retries(2) + retried() { + + throw new Error('failed'); + } +} +{% endhighlight %} + + +{:.toc} +### slow {% include support-mocha.html %} + +`slow` can be used as either a decorator or passed as a parameter to the `@test` decorator. + +{% highlight TypeScript linenos %} +import { suite, test, slow } from '@testdeck/mocha'; + +@suite(slow(2000)) +@slow(2000) +class Suite { + + // ... +} +{% endhighlight %} + + +{:.toc} +### timeout {% include support-all.html %} + +`timeout` can be used as either a decorator or passed as a parameter to the `@test` decorator. + +{% highlight TypeScript linenos %} +import { suite, test, timeout } from '@testdeck/mocha'; + +@suite(timeout(2000)) +@timeout(2000) +class Suite { + + // ... +} +{% endhighlight %} + + +{:.toc} +## Suite Naming {% include support-all.html %} + +{% highlight TypeScript linenos %} +import { suite, test } from '@testdeck/mocha'; + +@suite("a custom name for a suite") +class Suite { + + @test + test() { + } +} +{% endhighlight %} + + +{:.toc} +## Pending Suites {% include support-all.html %} + +{% highlight TypeScript linenos %} +import { suite } from '@testdeck/mocha'; + +@suite.pending("a pending suite") +class Suite { +} +{% endhighlight %} + + +{:.toc} +## Skipping Suites {% include support-all.html %} + +{% highlight TypeScript linenos %} +import { suite, test } from '@testdeck/mocha'; + +@suite.skip("a skipped suite") +class Suite { + + @test + test() { + } +} +{% endhighlight %} + + +{:.toc} +## Focused Suites {% include support-all.html %} + +{% highlight TypeScript linenos %} +import { suite, test } from '@testdeck/mocha'; + +@suite.only("a focused suite") +class Suite { + + @test + test() { + } +} +{% endhighlight %} + + +{:.toc} +## Static Lifecycle Hooks {% include support-all.html %} + +`Testdeck` supports static test lifecycle hooks, namely `before` and `after`, which are equivalent to the `beforeAll` +and `afterAll` test lifecycle hooks found in either of the supported test frameworks. + +{% highlight TypeScript linenos %} +import { suite, test } from '@testdeck/mocha'; +import * as k8s from '@kubernetes/client-node'; + +@suite +class Suite { + + private static k8sApi; + + static before(): Promise { + + const kc = new k8s.KubeConfig(); + kc.loadFromDefault(); + + Suite.k8sApi = kc.makeApiClient(k8s.Core_v1Api); + + const namespace = { + metadata: { + name: 'test' + } + }; + + return Suite.k8sApi.createNamespace(namespace).then( + (response) => { + + // ... fire up some pods (kafka/zookeeper, microservice consumer/producer) + }, (err) => { + + return Promise.reject(err); + } + ); + } + + @test + integrationTest() { + + // send some message via kafka + + // consume and test expected events from kafka topic + } + + static after(): Promise { + + const k8sApi = Suite.k8sApi; + Suite.k8sApi = null; + + // tear down pods and namespace + + const namespace = { + metadata: { + name: 'test' + } + }; + + return k8sApi.deleteNamespace(namespace).then( + (response) => { + + // ... + }, (err) => { + + return Promise.reject(err); + } + ); + } +} +{% endhighlight %} + + +{:.toc} +## Instance Lifecycle Hooks {% include support-all.html %} + +`Testdeck` also supports instance level test lifecycle hooks, which also go by the names `before` and `after`. These +hooks are the equivalent to the `beforeEach` and `afterEach` test lifecycle hooks found in either of the supported test +frameworks. + +{% highlight TypeScript linenos %} +import { suite, test } from '@testdeck/mocha'; + +import { NgLangParser } from '../src/NgLangParser'; + +@suite +class Suite { + + private cut: NgLangParser; + + before() { + + this.cut = new NgLangParser(); + } + + @test + mustParseEmptyString() { + + this.cut.parse(""); + } + + after() { + + this.cut = null; + } +} +{% endhighlight %}