1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-17 21:03:01 -05:00
denoland-deno/testing
2019-03-12 01:51:51 -04:00
..
asserts.ts fix: eslint errors (denoland/deno_std#265) 2019-03-12 01:51:51 -04:00
asserts_test.ts Use AssertionError instead of Error in testing (denoland/deno_std#254) 2019-03-08 16:04:43 -05:00
bench.ts Move benching into testing. (denoland/deno_std#258) 2019-03-11 14:21:13 -04:00
bench_example.ts Move benching into testing. (denoland/deno_std#258) 2019-03-11 14:21:13 -04:00
bench_test.ts Move benching into testing. (denoland/deno_std#258) 2019-03-11 14:21:13 -04:00
diff.ts Refactor asserts in testing (denoland/deno_std#227) 2019-03-05 14:58:28 -05:00
diff_test.ts Rename assertEq to assertEquals (denoland/deno_std#242) 2019-03-06 19:42:24 -05:00
format.ts Add eslint for linting (denoland/deno_std#235) 2019-03-04 19:53:35 -05:00
format_test.ts Rename assertEq to assertEquals (denoland/deno_std#242) 2019-03-06 19:42:24 -05:00
main.ts Fixed non-standard prefix on importing (denoland/deno_std#216) 2019-02-23 11:13:53 -05:00
mod.ts Testing refactor (denoland/deno_std#240) 2019-03-06 16:39:50 -05:00
pretty.ts Rename assertEq to assertEquals (denoland/deno_std#242) 2019-03-06 19:42:24 -05:00
pretty_test.ts Rename assertEq to assertEquals (denoland/deno_std#242) 2019-03-06 19:42:24 -05:00
README.md Move benching into testing. (denoland/deno_std#258) 2019-03-11 14:21:13 -04:00
test.ts Move benching into testing. (denoland/deno_std#258) 2019-03-11 14:21:13 -04:00
testing_bench.ts Move benching into testing. (denoland/deno_std#258) 2019-03-11 14:21:13 -04:00

Testing

This module provides a few basic utilities to make testing easier and consistent in Deno.

Usage

The module exports a test function which is the test harness in Deno. It accepts either a function (including async functions) or an object which contains a name property and a fn property. When running tests and outputting the results, the name of the past function is used, or if the object is passed, the name property is used to identify the test.

Asserts are exposed in testing/asserts.ts module.

  • equal - Deep comparision function, where actual and expected are compared deeply, and if they vary, equal returns false.
  • assert() - Expects a boolean value, throws if the value is false.
  • assertEquals() - Uses the equal comparison and throws if the actual and expected are not equal.
  • assertStrictEq() - Compares actual and expected strictly, therefore for non-primitives the values must reference the same instance.
  • assertThrows() - Expects the passed fn to throw. If fn does not throw, this function does. Also compares any errors thrown to an optional expected Error class and checks that the error .message includes an optional string.
  • assertThrowsAsync() - Expects the passed fn to be async and throw (or return a Promise that rejects). If the fn does not throw or reject, this function will throw asynchronously. Also compares any errors thrown to an optional expected Error class and checks that the error .message includes an optional string.

runTests() executes the declared tests.

Basic usage:

import { runTests, test } from "https://deno.land/std/testing/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

test({
  name: "testing example",
  fn() {
    assertEquals("world", "world"));
    assertEquals({ hello: "world" }, { hello: "world" }));
  }
});

runTests();

Short syntax (named function instead of object):

test(function example() {
    assertEquals("world", "world"));
    assertEquals({ hello: "world" }, { hello: "world" }));
});

Using assertStrictEq():

test(function isStrictlyEqual() {
  const a = {};
  const b = a;
  assertStrictEq(a, b);
});

// This test fails
test(function isNotStrictlyEqual() {
  const a = {};
  const b = {};
  assertStrictEq(a, b);
});

Using assertThrows():

test(function doesThrow() {
  assertThrows(() => {
    throw new TypeError("hello world!");
  });
  assertThrows(() => {
    throw new TypeError("hello world!");
  }, TypeError);
  assertThrows(
    () => {
      throw new TypeError("hello world!");
    },
    TypeError,
    "hello"
  );
});

// This test will not pass
test(function fails() {
  assertThrows(() => {
    console.log("Hello world");
  });
});

Using assertThrowsAsync():

test(async function doesThrow() {
  assertThrowsAsync(async () => {
    throw new TypeError("hello world!");
  });
  assertThrowsAsync(async () => {
    throw new TypeError("hello world!");
  }, TypeError);
  assertThrowsAsync(
    async () => {
      throw new TypeError("hello world!");
    },
    TypeError,
    "hello"
  );
  assertThrowsAsync(async () => {
    return Promise.reject(new Error());
  });
});

// This test will not pass
test(async function fails() {
  assertThrowsAsync(async () => {
    console.log("Hello world");
  });
});

Benching Usage

Basic usage:

import { runBenchmarks, bench } from "https://deno.land/std/testing/bench.ts";

bench(function forIncrementX1e9(b) {
  b.start();
  for (let i = 0; i < 1e9; i++);
  b.stop();
});

runBenchmarks();

Averaging execution time over multiple runs:

bench({
  name: "runs100ForIncrementX1e6",
  runs: 100,
  func(b) {
    b.start();
    for (let i = 0; i < 1e6; i++);
    b.stop();
  }
});

Benching API

bench(benchmark: BenchmarkDefinition | BenchmarkFunction): void

Registers a benchmark that will be run once runBenchmarks is called.

runBenchmarks(opts?: BenchmarkRunOptions): Promise<void>

Runs all registered benchmarks serially. Filtering can be applied by setting BenchmarkRunOptions.only and/or BenchmarkRunOptions.skip to regular expressions matching benchmark names.

runIfMain(meta: ImportMeta, opts?: BenchmarkRunOptions): Promise<void>

Runs specified benchmarks if the enclosing script is main.

Other exports
/** Provides methods for starting and stopping a benchmark clock. */
export interface BenchmarkTimer {
  start: () => void;
  stop: () => void;
}

/** Defines a benchmark through a named function. */
export interface BenchmarkFunction {
  (b: BenchmarkTimer): void | Promise<void>;
  name: string;
}

/** Defines a benchmark definition with configurable runs. */
export interface BenchmarkDefinition {
  func: BenchmarkFunction;
  name: string;
  runs?: number;
}

/** Defines runBenchmark's run constraints by matching benchmark names. */
export interface BenchmarkRunOptions {
  only?: RegExp;
  skip?: RegExp;
}