Added exclusivity support as well as only/timeout/skip support in front of test

This commit is contained in:
Jonatan Nilsson 2020-04-01 16:42:13 +00:00
parent f9d6ca20d7
commit efa90c1a59
3 changed files with 151 additions and 13 deletions

View file

@ -155,9 +155,29 @@ will output:
#someFunction() should always return true #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() ### 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 ```node
t.test('Skip due to something being broken', function() { t.test('Skip due to something being broken', function() {
@ -165,12 +185,24 @@ t.test('Skip due to something being broken', function() {
}).skip() }).skip()
``` ```
or like so:
```node
t.skip().test('Skip this', function() { ... })
```
### t.test(...).timeout(dur) ### 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 ```node
t.test('This is a really long test', async function() { t.test('This is a really long test', async function() {
await DoSomethingForReallyLongTime() await DoSomethingForReallyLongTime()
}).timeout(5000) // 5 seconds }).timeout(5000) // 5 seconds
``` ```
or like so:
```node
t.timeout(5000).test('A long test', async function() { ... })
```

View file

@ -2,15 +2,18 @@ import { printError } from './cli.mjs'
function Group(name) { function Group(name) {
this.name = name this.name = name
this.hasExclusive = false
this.tests = [] this.tests = []
} }
function Test(name, func) { function Test(e, group, name, func) {
this.e = e
this.group = group
this.skipTest = false this.skipTest = false
this.isExclusive = false
this.customTimeout = null this.customTimeout = null
this.name = name this.name = name
this.func = func this.func = func
this.group = null
this.error = null this.error = null
} }
@ -22,8 +25,13 @@ Test.prototype.skip = function() {
this.skipTest = true this.skipTest = true
} }
Test.prototype.only = function() {
this.e.hasExclusive = this.group.hasExclusive = this.isExclusive = true
}
function Eltro() { function Eltro() {
this.__timeout = 2000 this.__timeout = 2000
this.hasExclusive = false
this.reporter = 'list' this.reporter = 'list'
this.Eltro = Eltro this.Eltro = Eltro
this.groups = new Map() this.groups = new Map()
@ -34,6 +42,11 @@ function Eltro() {
this.starting = false this.starting = false
this.filename = '' this.filename = ''
this.prefix = '' this.prefix = ''
this.temporary = {
timeout: 0,
skip: false,
only: false
}
} }
Eltro.prototype.begin = function() { Eltro.prototype.begin = function() {
@ -179,6 +192,7 @@ Eltro.prototype.run = async function() {
for (let i = 0; i < this.groupsFlat.length; i++) { for (let i = 0; i < this.groupsFlat.length; i++) {
let g = this.groupsFlat[i]; let g = this.groupsFlat[i];
if (g.hasExclusive === this.hasExclusive) {
if (this.reporter === 'list') { if (this.reporter === 'list') {
console.log(' ' + g.name) console.log(' ' + g.name)
} }
@ -186,10 +200,13 @@ Eltro.prototype.run = async function() {
await this.__runTest(stats, g.tests[x]) await this.__runTest(stats, g.tests[x])
} }
} }
}
for (let x = 0; x < this.tests.length; x++) { for (let x = 0; x < this.tests.length; x++) {
if (this.tests[x].isExclusive === this.hasExclusive) {
await this.__runTest(stats, this.tests[x]) await this.__runTest(stats, this.tests[x])
} }
}
let end = process.hrtime(start) let end = process.hrtime(start)
if (this.reporter) { if (this.reporter) {
@ -238,6 +255,33 @@ Eltro.prototype.describe = function(name, func) {
this.prefix = before 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) { Eltro.prototype.test = function(name, func) {
let targetName = name let targetName = name
if (this.prefix) { if (this.prefix) {
@ -254,9 +298,21 @@ Eltro.prototype.test = function(name, func) {
} }
group = this.groups.get(this.filename) group = this.groups.get(this.filename)
} }
let test = new Test(targetName, func) let test = new Test(this, group, targetName, func)
test.group = group
group.tests.push(test) 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 return test
} }

View file

@ -180,6 +180,56 @@ e.test('Eltro should support skipped tests', async function() {
assert.strictEqual(t.failedTests.length, 0) 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 // Extra testing to make sure tests were run at all
process.on('exit', function(e) { process.on('exit', function(e) {
try { try {