1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-27 16:10:57 -05:00

testing: turn off exitOnFail by default (denoland/deno_std#307)

Original: d9e8953110
This commit is contained in:
Vincent LE GOFF 2019-03-26 16:29:12 +01:00 committed by Ryan Dahl
parent 630d0f213d
commit b7022848f6
2 changed files with 25 additions and 17 deletions

View file

@ -13,7 +13,7 @@ object is passed, the `name` property is used to identify the test. If the asser
Asserts are exposed in `testing/asserts.ts` module. Asserts are exposed in `testing/asserts.ts` module.
- `equal` - Deep comparision function, where `actual` and `expected` are - `equal()` - Deep comparision function, where `actual` and `expected` are
compared deeply, and if they vary, `equal` returns `false`. compared deeply, and if they vary, `equal` returns `false`.
- `assert()` - Expects a boolean value, throws if the value is `false`. - `assert()` - Expects a boolean value, throws if the value is `false`.
- `assertEquals()` - Uses the `equal` comparison and throws if the `actual` and - `assertEquals()` - Uses the `equal` comparison and throws if the `actual` and
@ -37,7 +37,10 @@ Asserts are exposed in `testing/asserts.ts` module.
- `unimplemented()` - Use this to stub out methods that will throw when invoked - `unimplemented()` - Use this to stub out methods that will throw when invoked
- `unreachable()` - Used to assert unreachable code - `unreachable()` - Used to assert unreachable code
`runTests()` executes the declared tests. `runTests()` executes the declared tests. It accepts a `RunOptions` parameter:
- parallel : Execute tests in a parallel way.
- exitOnFail : if one test fails, test will throw an error and stop the tests. If not all tests will be processed.
Basic usage: Basic usage:

View file

@ -9,8 +9,6 @@ export interface TestDefinition {
name: string; name: string;
} }
export const exitOnFail = true;
let filterRegExp: RegExp | null; let filterRegExp: RegExp | null;
const tests: TestDefinition[] = []; const tests: TestDefinition[] = [];
@ -93,7 +91,8 @@ function report(result: TestResult): void {
function printResults( function printResults(
stats: TestStats, stats: TestStats,
results: TestResults, results: TestResults,
flush: boolean flush: boolean,
exitOnFail: boolean
): void { ): void {
if (flush) { if (flush) {
for (const result of results.cases.values()) { for (const result of results.cases.values()) {
@ -125,7 +124,8 @@ function previousPrinted(name: string, results: TestResults): boolean {
async function createTestCase( async function createTestCase(
stats: TestStats, stats: TestStats,
results: TestResults, results: TestResults,
{ fn, name }: TestDefinition { fn, name }: TestDefinition,
exitOnFail: boolean
): Promise<void> { ): Promise<void> {
const result: TestResult = results.cases.get(results.keys.get(name)); const result: TestResult = results.cases.get(results.keys.get(name));
try { try {
@ -147,18 +147,20 @@ async function createTestCase(
function initTestCases( function initTestCases(
stats: TestStats, stats: TestStats,
results: TestResults, results: TestResults,
tests: TestDefinition[] tests: TestDefinition[],
exitOnFail: boolean
): Array<Promise<void>> { ): Array<Promise<void>> {
return tests.map(createTestCase.bind(null, stats, results)); return tests.map(createTestCase.bind(null, stats, results, exitOnFail));
} }
async function runTestsParallel( async function runTestsParallel(
stats: TestStats, stats: TestStats,
results: TestResults, results: TestResults,
tests: TestDefinition[] tests: TestDefinition[],
exitOnFail: boolean
): Promise<void> { ): Promise<void> {
try { try {
await Promise.all(initTestCases(stats, results, tests)); await Promise.all(initTestCases(stats, results, tests, exitOnFail));
} catch (_) { } catch (_) {
// The error was thrown to stop awaiting all promises if exitOnFail === true // The error was thrown to stop awaiting all promises if exitOnFail === true
// stats.failed has been incremented and the error stored in results // stats.failed has been incremented and the error stored in results
@ -167,7 +169,8 @@ async function runTestsParallel(
async function runTestsSerial( async function runTestsSerial(
stats: TestStats, stats: TestStats,
tests: TestDefinition[] tests: TestDefinition[],
exitOnFail: boolean
): Promise<void> { ): Promise<void> {
for (const { fn, name } of tests) { for (const { fn, name } of tests) {
// See https://github.com/denoland/deno/pull/1452 // See https://github.com/denoland/deno/pull/1452
@ -193,15 +196,17 @@ async function runTestsSerial(
/** Defines options for controlling execution details of a test suite. */ /** Defines options for controlling execution details of a test suite. */
export interface RunOptions { export interface RunOptions {
parallel?: boolean; parallel?: boolean;
exitOnFail?: boolean;
} }
/** /**
* Runs specified test cases. * Runs specified test cases.
* Parallel execution can be enabled via the boolean option; default: serial. * Parallel execution can be enabled via the boolean option; default: serial.
*/ */
export async function runTests({ parallel = false }: RunOptions = {}): Promise< export async function runTests({
void parallel = false,
> { exitOnFail = false
}: RunOptions = {}): Promise<void> {
const stats: TestStats = { const stats: TestStats = {
measured: 0, measured: 0,
ignored: 0, ignored: 0,
@ -212,11 +217,11 @@ export async function runTests({ parallel = false }: RunOptions = {}): Promise<
const results: TestResults = createTestResults(tests); const results: TestResults = createTestResults(tests);
console.log(`running ${tests.length} tests`); console.log(`running ${tests.length} tests`);
if (parallel) { if (parallel) {
await runTestsParallel(stats, results, tests); await runTestsParallel(stats, results, tests, exitOnFail);
} else { } else {
await runTestsSerial(stats, tests); await runTestsSerial(stats, tests, exitOnFail);
} }
printResults(stats, results, parallel); printResults(stats, results, parallel, exitOnFail);
if (stats.failed) { if (stats.failed) {
// Use setTimeout to avoid the error being ignored due to unhandled // Use setTimeout to avoid the error being ignored due to unhandled
// promise rejections being swallowed. // promise rejections being swallowed.