0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/std/testing
2020-03-10 12:08:58 -04:00
..
testdata Move everything into std subdir 2019-10-09 17:10:09 -04:00
asserts.ts test: add actual error class to fail message (#4305) 2020-03-09 17:46:55 -04:00
asserts_test.ts test: add actual error class to fail message (#4305) 2020-03-09 17:46:55 -04:00
bench.ts refactor: Cleanup options object parameters (#4296) 2020-03-10 12:08:58 -04:00
bench_example.ts Move everything into std subdir 2019-10-09 17:10:09 -04:00
bench_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00
diff.ts Happy new year! (#3578) 2020-01-02 15:13:47 -05:00
diff_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00
format.ts remove non-null assertion operator from std (part1) (#3900) 2020-02-07 02:23:38 -05:00
format_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00
README.md update references to testing/mod.ts in manual (#3973) 2020-02-11 21:50:20 +01:00
runner.ts rename Deno.Err -> Deno.errors (#4093) 2020-02-24 15:48:35 -05:00
runner_test.ts refactor: rewrite tests in std/ to use Deno.test (#3930) 2020-02-11 17:24:27 +01:00

Testing

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

Usage

testing/asserts.ts module provides range of assertion helpers. If the assertion is false an AssertionError will be thrown which will result in pretty-printed diff of failing assertion.

  • equal() - Deep comparison 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.
  • assertNotEquals() - Uses the equal comparison and throws if the actual and expected are equal.
  • assertStrictEq() - Compares actual and expected strictly, therefore for non-primitives the values must reference the same instance.
  • assertStrContains() - Make an assertion that actual contains expected.
  • assertMatch() - Make an assertion that actual match RegExp expected.
  • assertArrayContains() - Make an assertion that actual array contains the expected values.
  • 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.
  • unimplemented() - Use this to stub out methods that will throw when invoked
  • unreachable() - Used to assert unreachable code

Basic usage:

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

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

await Deno.runTests();

Short syntax (named function instead of object):

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

Using assertStrictEq():

Deno.test(function isStrictlyEqual(): void {
  const a = {};
  const b = a;
  assertStrictEq(a, b);
});

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

Using assertThrows():

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

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

Using assertThrowsAsync():

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

// This test will not pass
Deno.test(async function fails(): Promise<void> {
  await assertThrowsAsync(
    async (): Promise<void> => {
      console.log("Hello world");
    }
  );
});

Benching Usage

Basic usage:

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

bench(function forIncrementX1e9(b): void {
  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): void {
    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;
}