From efa90c1a595b770b8910811456e17aaf4d13ce3c Mon Sep 17 00:00:00 2001 From: Jonatan Nilsson Date: Wed, 1 Apr 2020 16:42:13 +0000 Subject: [PATCH] Added exclusivity support as well as only/timeout/skip support in front of test --- README.md | 36 +++++++++++++++++++-- lib/eltro.mjs | 78 ++++++++++++++++++++++++++++++++++++++------- test/eltro.test.mjs | 50 +++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 6079217..0dfa956 100644 --- a/README.md +++ b/README.md @@ -155,9 +155,29 @@ will output: √ #someFunction() should always return true ``` +### t.test(...).only() + +Eltro supports exclusivity when running tests. When specified, only tests marked with only will be run. + +You can do exclusivity on tests by adding `.only()` after or before the test like so: + +```node +t.test('Only run this test', function() { + assert.strictEqual(true, true) +}).only() +``` + +or like so: + +```node +t.only().test('Only run this test', function() { + assert.strictEqual(true, true) +}) +``` + ### t.test(...).skip() -You can skip tests easily by adding `.skip()` after the test like so: +You can skip tests easily by adding `.skip()` before or after the test like so: ```node t.test('Skip due to something being broken', function() { @@ -165,12 +185,24 @@ t.test('Skip due to something being broken', function() { }).skip() ``` +or like so: + +```node +t.skip().test('Skip this', function() { ... }) +``` + ### t.test(...).timeout(dur) -Tests can take a long timt. By default, eltro will cancel a test if it takes longer than 2 seconds. You can however override this by calling the timeout function after the test with the specified duration in milliseconds like so: +Tests can take a long time. By default, eltro will cancel a test if it takes longer than 2 seconds. You can however override this by calling the timeout function after or before the test with the specified duration in milliseconds like so: ```node t.test('This is a really long test', async function() { await DoSomethingForReallyLongTime() }).timeout(5000) // 5 seconds ``` + +or like so: + +```node +t.timeout(5000).test('A long test', async function() { ... }) +``` diff --git a/lib/eltro.mjs b/lib/eltro.mjs index 5b652ee..53ba0a8 100644 --- a/lib/eltro.mjs +++ b/lib/eltro.mjs @@ -2,15 +2,18 @@ import { printError } from './cli.mjs' function Group(name) { this.name = name + this.hasExclusive = false this.tests = [] } -function Test(name, func) { +function Test(e, group, name, func) { + this.e = e + this.group = group this.skipTest = false + this.isExclusive = false this.customTimeout = null this.name = name this.func = func - this.group = null this.error = null } @@ -22,8 +25,13 @@ Test.prototype.skip = function() { this.skipTest = true } +Test.prototype.only = function() { + this.e.hasExclusive = this.group.hasExclusive = this.isExclusive = true +} + function Eltro() { this.__timeout = 2000 + this.hasExclusive = false this.reporter = 'list' this.Eltro = Eltro this.groups = new Map() @@ -34,6 +42,11 @@ function Eltro() { this.starting = false this.filename = '' this.prefix = '' + this.temporary = { + timeout: 0, + skip: false, + only: false + } } Eltro.prototype.begin = function() { @@ -178,17 +191,21 @@ Eltro.prototype.run = async function() { let start = process.hrtime() for (let i = 0; i < this.groupsFlat.length; i++) { let g = this.groupsFlat[i]; - - if (this.reporter === 'list') { - console.log(' ' + g.name) - } - for (let x = 0; x < g.tests.length; x++) { - await this.__runTest(stats, g.tests[x]) + + if (g.hasExclusive === this.hasExclusive) { + if (this.reporter === 'list') { + console.log(' ' + g.name) + } + for (let x = 0; x < g.tests.length; x++) { + await this.__runTest(stats, g.tests[x]) + } } } for (let x = 0; x < this.tests.length; x++) { - await this.__runTest(stats, this.tests[x]) + if (this.tests[x].isExclusive === this.hasExclusive) { + await this.__runTest(stats, this.tests[x]) + } } let end = process.hrtime(start) @@ -238,6 +255,33 @@ Eltro.prototype.describe = function(name, func) { this.prefix = before } +/*Test.prototype.timeout = function(time) { + this.customTimeout = time +} + +Test.prototype.skip = function() { + this.skipTest = true +} + +Test.prototype.only = function() { + this.e.hasExclusive = this.group.hasExclusive = this.isExclusive = true +}*/ + +Eltro.prototype.timeout = function(time) { + this.temporary.timeout = time + return this +} + +Eltro.prototype.skip = function() { + this.temporary.skip = true + return this +} + +Eltro.prototype.only = function() { + this.temporary.only = true + return this +} + Eltro.prototype.test = function(name, func) { let targetName = name if (this.prefix) { @@ -254,9 +298,21 @@ Eltro.prototype.test = function(name, func) { } group = this.groups.get(this.filename) } - let test = new Test(targetName, func) - test.group = group + let test = new Test(this, group, targetName, func) group.tests.push(test) + + if (this.temporary.only) { + test.only() + this.temporary.only = false + } + if (this.temporary.skip) { + test.skip() + this.temporary.skip = false + } + if (this.temporary.timeout) { + test.timeout(this.temporary.timeout) + this.temporary.timeout = 0 + } return test } diff --git a/test/eltro.test.mjs b/test/eltro.test.mjs index c38a473..97d90fa 100644 --- a/test/eltro.test.mjs +++ b/test/eltro.test.mjs @@ -180,6 +180,56 @@ e.test('Eltro should support skipped tests', async function() { assert.strictEqual(t.failedTests.length, 0) }) +e.test('Eltro should support only tests', async function() { + testsWereRun = true + let assertIsTrue = false + const t = CreateT() + t.begin() + t.test('a', function() { throw new Error('Should not be called') }) + t.test('b', function() { throw new Error('Should not be called') }) + t.test('c', function() { assertIsTrue = true }).only() + await t.run() + assert.strictEqual(t.failedTests.length, 0) + assert.strictEqual(assertIsTrue, true) +}) + +e.test('Eltro should support timed out tests in front', async function() { + testsWereRun = true + const t = CreateT() + t.begin() + t.timeout(25).test('', function(cb) { setTimeout(cb, 50) }) + t.test('', function(cb) { setTimeout(cb, 50) }) + await t.run() + assert.strictEqual(t.failedTests.length, 1) + assert.ok(t.failedTests[0].error) + assert.match(t.failedTests[0].error.message, /25ms/) +}) + +e.test('Eltro should support skipped tests in front of the test', async function() { + testsWereRun = true + let assertIsTrue = false + const t = CreateT() + t.begin() + t.skip().test('', function() { throw new Error('Should not be called') }) + t.test('', function() { assertIsTrue = true }) + await t.run() + assert.strictEqual(t.failedTests.length, 0) + assert.strictEqual(assertIsTrue, true) +}) + +e.test('Eltro should support only tests in front of the test', async function() { + testsWereRun = true + let assertIsTrue = false + const t = CreateT() + t.begin() + t.test('a', function() { throw new Error('Should not be called') }) + t.only().test('b', function() { assertIsTrue = true }) + t.test('c', function() { throw new Error('Should not be called') }) + await t.run() + assert.strictEqual(t.failedTests.length, 0) + assert.strictEqual(assertIsTrue, true) +}) + // Extra testing to make sure tests were run at all process.on('exit', function(e) { try {