cli: Add ability to override default timeout as well as ignore only tests
Some checks failed
continuous-integration/appveyor/branch AppVeyor build failed

This commit is contained in:
Jonatan Nilsson 2022-03-02 14:25:56 +00:00
parent 72d5e8d124
commit be700ab012
7 changed files with 119 additions and 4 deletions

View file

@ -24,3 +24,24 @@ environment:
test_script:
- sh: |
npm test
on_success:
- sh: |
apk add curl jq
CURR_VER=$(cat package.json | jq -r .version)
if curl -s -X GET -H "Authorization: token $deploytoken" https://git.nfp.is/api/v1/repos/$APPVEYOR_REPO_NAME/releases | grep -o "\"name\"\:\"v${CURR_VER}\"" > /dev/null; then
echo "Release already exists, nothing to do.";
else
echo "Creating release on gitea"
RELEASE_RESULT=$(curl \
-X POST \
-H "Authorization: token $deploytoken" \
-H "Content-Type: application/json" \
https://git.nfp.is/api/v1/repos/$APPVEYOR_REPO_NAME/releases \
-d "{\"tag_name\":\"v${CURR_VER}\",\"name\":\"v${CURR_VER}\",\"body\":\"Automatic release from Appveyor from ${APPVEYOR_REPO_COMMIT} :\n\n${APPVEYOR_REPO_COMMIT_MESSAGE}\"}")
RELEASE_ID=$(echo $RELEASE_RESULT | jq -r .id)
echo "Created release ${RELEASE_ID}"
echo '//registry.npmjs.org/:_authToken=${npmtoken}' > ~/.npmrc
echo "Publishing new version to npm"
npm publish
fi

View file

@ -23,6 +23,9 @@ function PrintHelp() {
console.log('where <options> can be any of the following:')
console.log(' -r, --reporter - Specify the reporter to use.')
console.log(' Supported reporters: list, dot')
console.log(' -t, --timeout - Specify the timeout for tests in ms.')
console.log(' Default value is 2000ms')
console.log(' --ignore-only - Specify to ignore any .only() tests found')
console.log('')
console.log('eltro test/mytest.mjs')
console.log('eltro dot test/*.mjs')
@ -40,6 +43,8 @@ cli.processTargets().then(function() {
return cli.loadFiles()
.then(function() {
e.reporter = cli.reporter
e.ignoreOnly = cli.ignoreOnly
e.__timeout = cli.timeout
return e.run()
.catch(function(err) {

View file

@ -4,6 +4,8 @@ import fs from 'fs'
export function CLI(e) {
this.e = e
this.reporter = 'list'
this.ignoreOnly = false
this.timeout = 2000
this.targets = ['test/**']
this.files = []
this.errored = false
@ -28,6 +30,15 @@ CLI.prototype.parseOptions = function(args) {
}
this.reporter = args[i + 1]
i++
} else if (args[i] === '-t' || args[i] === '--timeout') {
if (!args[i + 1] || isNaN(Number(args[i + 1]))) {
this.errored = true
return
}
this.timeout = Number(args[i + 1])
i++
} else if (args[i] === '--ignore-only') {
this.ignoreOnly = true
} else if (args[i][0] === '-') {
this.errored = true
return

View file

@ -73,8 +73,10 @@ Test.prototype.skip = function() {
}
Test.prototype.only = function() {
this.isExclusive = true
this.group.__hasonly(true)
if (!this.e.ignoreOnly) {
this.isExclusive = true
this.group.__hasonly(true)
}
}
Test.prototype.clone = function(prefix = '') {
@ -97,6 +99,7 @@ function Eltro() {
this.failedTests = []
this.hasTests = false
this.starting = false
this.ignoreOnly = false
this.logger = null
this.filename = ''
this.prefix = ''
@ -436,7 +439,9 @@ Eltro.prototype.skip = function() {
}
Eltro.prototype.only = function() {
this.temporary.only = true
if (!this.ignoreOnly) {
this.temporary.only = true
}
return this
}

View file

@ -4,7 +4,18 @@
"description": "Eltro is a tiny no-dependancy test framework for node",
"main": "index.mjs",
"scripts": {
"test": "node cli.mjs test/**/*.test.mjs"
"test": "node cli.mjs test/**/*.test.mjs",
"test:watch": "npm-watch test"
},
"watch": {
"test": {
"patterns": [
"*"
],
"extensions": "js,mjs",
"quiet": true,
"inherit": true
}
},
"repository": {
"type": "git",

View file

@ -8,6 +8,8 @@ t.describe('CLI', function() {
t.test('#constructor() give default options', function() {
let cliTest = new CLI()
assert.strictEqual(cliTest.reporter, 'list')
assert.strictEqual(cliTest.ignoreOnly, false)
assert.strictEqual(cliTest.timeout, 2000)
assert.deepEqual(cliTest.targets, ['test/**'])
assert.deepEqual(cliTest.files, [])
assert.notOk(cliTest.errored)
@ -38,6 +40,13 @@ t.describe('CLI', function() {
assert.strictEqual(cli.reporter, 'dot')
assert.notOk(cli.errored)
})
t.test('should support enabling ignore-only long option', function() {
cli.ignoreOnly = false
cli.parseOptions(['--ignore-only', '-r', 'dot'])
assert.strictEqual(cli.ignoreOnly, true)
assert.notOk(cli.errored)
})
t.test('should support reporter list', function() {
cli.reporter = 'list'
@ -56,6 +65,30 @@ t.describe('CLI', function() {
assert.ok(cli.errored)
})
t.test('should support overriding timeout with shorthand option', function() {
cli.timeout = 2000
cli.parseOptions(['-t', '1000'])
assert.strictEqual(cli.timeout, 1000)
assert.notOk(cli.errored)
})
t.test('should support overriding timeout with long option', function() {
cli.timeout = 2000
cli.parseOptions(['--timeout', '250'])
assert.strictEqual(cli.timeout, 250)
assert.notOk(cli.errored)
})
t.test('should mark errored if missing timeout', function() {
cli.parseOptions(['--timeout'])
assert.ok(cli.errored)
})
t.test('should mark errored if invalid timeout', function() {
cli.parseOptions(['--timeout', 'test'])
assert.ok(cli.errored)
})
t.test('should add file to targets', function() {
cli.parseOptions(['test'])
assert.deepEqual(cli.targets, ['test'])

View file

@ -268,6 +268,35 @@ e.test('Eltro should support only tests', async function() {
assert.strictEqual(assertIsTrue, true)
})
e.test('Eltro should ignore only if ignoreOnly is specified', async function() {
testsWereRun = true
let assertIsRun = 0
const t = CreateT()
t.ignoreOnly = true
t.begin()
t.describe('', function() {
t.test('a', function() { assertIsRun++ })
t.test('b', function() { assertIsRun++ }).only()
t.describe('sub', function() {
t.test('d', function() { assertIsRun++ })
t.only().test('c', function() { assertIsRun++ })
})
t.only().describe('sub2', function() {
t.test('d', function() { assertIsRun++ })
t.test('c', function() { assertIsRun++ })
})
t.describe('sub3', function() {
t.test('d', function() { assertIsRun++ })
t.test('c', function() { assertIsRun++ })
})
})
await t.run()
assert.strictEqual(t.failedTests.length, 0)
assert.strictEqual(assertIsRun, 8)
})
e.test('Eltro should support skipped tests in front of the test', async function() {
testsWereRun = true
let assertIsTrue = false