Home Reference Source
import {Tester} from './api/Tester.js'
public class | source

Tester

Very simple unit testing utility.

At some point we may switch to using Mocha or some other test framework, but for now we want to be able to invoke the tests from the Sketch side or from a plugin command, so it's simpler to use a simple test framework of our own devising.

Constructor Summary

Public Constructor
public

constructor(application: *)

Make a new tester.

Member Summary

Public Members
public get

The application instance that we're running the tests for. This is the instance associated with the script context that launched the tests.

Method Summary

Public Methods
public

assert(condition: bool, description: string)

Assert that a condition is true.

public

assertEqual(v1: *, v2: *)

Assert that two values are equal.

public

assertFalse(v: *)

Assert that a value is false.

public

assertTrue(v: *)

Assert that a value is true.

public

Returns a new document to use in tests.

public

runUnitTests(specification: dictionary, suiteName: string): dictionary

Run a collection of tests.

Public Constructors

public constructor(application: *) source

Make a new tester.

Params:

NameTypeAttributeDescription
application *

Public Members

public get application: Application: * source

The application instance that we're running the tests for. This is the instance associated with the script context that launched the tests.

Return:

Application

The application object.

Public Methods

public assert(condition: bool, description: string) source

Assert that a condition is true. If the assertion fails, the failure is recorded for later reporting by the tester.

Params:

NameTypeAttributeDescription
condition bool

The condition we're asserting.

description string

A description of the test.

public assertEqual(v1: *, v2: *) source

Assert that two values are equal. If the assertion fails, the failure is recorded for later reporting by the tester.

Params:

NameTypeAttributeDescription
v1 *

The first value to compare.

v2 *

The second value to compare.

public assertFalse(v: *) source

Assert that a value is false. If the assertion fails, the failure is recorded for later reporting by the tester.

Params:

NameTypeAttributeDescription
v *

The value to check.

public assertTrue(v: *) source

Assert that a value is true. If the assertion fails, the failure is recorded for later reporting by the tester.

Params:

NameTypeAttributeDescription
v *

The value to check.

public newTestDocument(): Document source

Returns a new document to use in tests.

Return:

Document

Test document.

public runUnitTests(specification: dictionary, suiteName: string): dictionary source

Run a collection of tests.

The method takes a dictionary describing the tests to run. The dictionary can contain two keys:

  • suites: this is a dictionary of sub-collections, each of which is recursively run by calling this method again.
  • tests: this is a dictionary of test functions, each of which is executed.

The test functions are passed this tester object when they are executed, and should use the assertion methods on it to perform tests.

Params:

NameTypeAttributeDescription
specification dictionary

A dictionary describing the tests to run. See discussion.

suiteName string

The name of the suite, if we're running a sub-collection. This will be null for the top level tests.

Return:

dictionary

Returns a dictionary indicating how many tests ran, and a list of the passed, failed, and crashed tests.

See something wrong or incomplete? Improve this page.