2020-03-09 20:06:47 -04:00
|
|
|
# Deno runtime tests
|
|
|
|
|
|
|
|
Files in this directory are unit tests for Deno runtime.
|
|
|
|
|
|
|
|
Testing Deno runtime code requires checking API under different runtime
|
|
|
|
permissions (ie. running with different `--allow-*` flags). To accomplish this
|
|
|
|
all tests exercised are created using `unitTest()` function.
|
|
|
|
|
|
|
|
```
|
|
|
|
import { unitTest } from "./test_util.ts";
|
|
|
|
|
|
|
|
unitTest(function simpleTestFn(): void {
|
|
|
|
// test code here
|
|
|
|
});
|
|
|
|
|
|
|
|
unitTest({
|
2020-04-28 12:35:23 -04:00
|
|
|
ignore: Deno.build.os === "windows",
|
2020-03-09 20:06:47 -04:00
|
|
|
perms: { read: true, write: true },
|
|
|
|
},
|
|
|
|
function complexTestFn(): void {
|
|
|
|
// test code here
|
|
|
|
}
|
|
|
|
);
|
|
|
|
```
|
|
|
|
|
|
|
|
`unitTest` is is a wrapper function that enhances `Deno.test()` API in several
|
|
|
|
ways:
|
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
- ability to conditionally skip tests using `UnitTestOptions.skip`.
|
2020-03-09 20:06:47 -04:00
|
|
|
- ability to register required set of permissions for given test case using
|
2020-10-03 16:19:11 -04:00
|
|
|
`UnitTestOptions.perms`.
|
2020-03-09 20:06:47 -04:00
|
|
|
- sanitization of resources - ensuring that tests close all opened resources
|
2020-10-03 16:19:11 -04:00
|
|
|
preventing interference between tests.
|
2020-03-09 20:06:47 -04:00
|
|
|
- sanitization of async ops - ensuring that tests don't leak async ops by
|
2020-10-03 16:19:11 -04:00
|
|
|
ensuring that all started async ops are done before test finishes.
|
2020-03-09 20:06:47 -04:00
|
|
|
|
2020-03-14 06:53:20 -04:00
|
|
|
## Running tests
|
|
|
|
|
|
|
|
`unit_test_runner.ts` is the main script used to run unit tests.
|
2020-03-09 20:06:47 -04:00
|
|
|
|
2020-05-14 00:38:42 -04:00
|
|
|
Runner discovers required permissions combinations by loading
|
2020-05-20 17:52:51 -04:00
|
|
|
`cli/tests/unit/unit_tests.ts` and going through all registered instances of
|
2020-03-14 06:53:20 -04:00
|
|
|
`unitTest`.
|
|
|
|
|
|
|
|
There are three ways to run `unit_test_runner.ts`:
|
|
|
|
|
|
|
|
```
|
2020-03-15 12:58:59 -04:00
|
|
|
# Run all tests. Spawns worker processes for each discovered permission
|
|
|
|
# combination:
|
2020-05-20 17:52:51 -04:00
|
|
|
target/debug/deno run -A cli/tests/unit/unit_test_runner.ts --master
|
2020-03-14 06:53:20 -04:00
|
|
|
|
2020-03-15 12:58:59 -04:00
|
|
|
# By default all output of worker processes is discarded; for debug purposes
|
2020-10-03 16:19:11 -04:00
|
|
|
# the --verbose flag preserves output from the worker.
|
2020-05-20 17:52:51 -04:00
|
|
|
target/debug/deno run -A cli/tests/unit/unit_test_runner.ts --master --verbose
|
2020-03-14 06:53:20 -04:00
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
# Run subset of tests that don't require any permissions.
|
2020-05-20 17:52:51 -04:00
|
|
|
target/debug/deno run --unstable cli/tests/unit/unit_test_runner.ts
|
2020-03-14 06:53:20 -04:00
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
# Run subset tests that require "net" and "read" permissions.
|
2020-05-20 17:52:51 -04:00
|
|
|
target/debug/deno run --unstable --allow-net --allow-read cli/tests/unit/unit_test_runner.ts
|
2020-03-14 06:53:20 -04:00
|
|
|
|
2020-03-15 12:58:59 -04:00
|
|
|
# "worker" mode communicates with parent using TCP socket on provided address;
|
|
|
|
# after initial setup drops permissions to specified set. It shouldn't be used
|
|
|
|
# directly, only be "master" process.
|
2020-05-20 17:52:51 -04:00
|
|
|
target/debug/deno run -A cli/tests/unit/unit_test_runner.ts --worker --addr=127.0.0.1:4500 --perms=net,write,run
|
2020-03-14 06:53:20 -04:00
|
|
|
|
2020-10-03 16:19:11 -04:00
|
|
|
# Run specific tests.
|
2020-05-20 17:52:51 -04:00
|
|
|
target/debug/deno run --unstable --allow-net cli/tests/unit/unit_test_runner.ts -- netTcpListenClose
|
2020-04-21 09:48:44 -04:00
|
|
|
|
2020-05-20 17:52:51 -04:00
|
|
|
RUST_BACKTRACE=1 cargo run -- run --unstable --allow-read --allow-write cli/tests/unit/unit_test_runner.ts -- netUnixDialListen
|
2020-03-14 06:53:20 -04:00
|
|
|
```
|
|
|
|
|
2020-03-15 12:58:59 -04:00
|
|
|
### Http server
|
2020-03-14 06:53:20 -04:00
|
|
|
|
2020-07-04 13:05:01 -04:00
|
|
|
`target/debug/test_server` is required to run when one's running unit tests.
|
|
|
|
During CI it's spawned automatically, but if you want to run tests manually make
|
|
|
|
sure that server is spawned otherwise there'll be cascade of test failures.
|