Add skip feature

master v1.3.0
Frédéric Matte 2022-12-30 13:42:42 -05:00
parent 5e6e43930c
commit 90b20029c9
5 changed files with 31 additions and 3 deletions

View File

@ -21,6 +21,7 @@ const stats = await test.run();
title: 'Metaltest', title: 'Metaltest',
success: 2, success: 2,
fail: 0, fail: 0,
skip: 0,
total: 2, total: 2,
testSuccess: [], testSuccess: [],
testFail: [] testFail: []
@ -34,6 +35,18 @@ const stats = await test.run();
const stats = await test.run(consolereporter()); const stats = await test.run(consolereporter());
``` ```
### How to run only specific test or how to skip some test
```js
import metaltest from "metaltest";
const test = metaltest("Simple test");
test.only("only this test will run", () => {
assert.equal(1, 1);
});
test("this test will not run", () => {});
test.skip("this test will be skipped, even if there is no only", () => {});
```
## How to create a suite of test ## How to create a suite of test
test.js test.js

View File

@ -1,12 +1,14 @@
const metaltest = (title) => { const metaltest = (title) => {
const suite = [] const suite = []
const only = [] const only = []
const skipped = []
const before = [] const before = []
const after = [] const after = []
const end = [] const end = []
let success = 0 let success = 0
let fail = 0 let fail = 0
let skip = 0
const testSuccess = [] const testSuccess = []
const testFail = [] const testFail = []
@ -22,7 +24,7 @@ const metaltest = (title) => {
break break
} }
return { title, success, fail, total: success + fail, testSuccess, testFail } return { title, success, fail, skip, total: success + fail, testSuccess, testFail }
} }
const runner = (name, fn) => { const runner = (name, fn) => {
@ -30,6 +32,7 @@ const metaltest = (title) => {
} }
runner.only = (name, fn) => { only.push({ name, fn }) } runner.only = (name, fn) => { only.push({ name, fn }) }
runner.skip = (name, fn) => { skipped.push({ name, fn }) }
runner.before = (fn) => { before.push(fn) } runner.before = (fn) => { before.push(fn) }
runner.after = (fn) => { after.push(fn) } runner.after = (fn) => { after.push(fn) }
runner.end = (name, fn) => { end.push({ name, fn }) } runner.end = (name, fn) => { end.push({ name, fn }) }
@ -46,6 +49,14 @@ const metaltest = (title) => {
await notify(reporters, 'start', title) await notify(reporters, 'start', title)
const tests = only.length ? only : suite const tests = only.length ? only : suite
if (only.length) {
for (const test of suite) await notify(reporters, 'skip', test)
}
for (const test of skipped) await notify(reporters, 'skip', test)
skip = only.length ? suite.length : skip
skip += skipped.length
for (const test of tests) { for (const test of tests) {
try { try {
for (const fn of before) await fn() for (const fn of before) await fn()

View File

@ -12,10 +12,12 @@ test('throw', () => {
throw new Error('message') throw new Error('message')
}, { name: 'Error', message: 'message' }) }, { name: 'Error', message: 'message' })
}) })
test.skip('skip', () => { })
test.end('verify', async (stats) => { test.end('verify', async (stats) => {
assert.equal(stats.success, 4) assert.equal(stats.success, 4)
assert.equal(stats.fail, 0) assert.equal(stats.fail, 0)
assert.equal(stats.skip, 1)
assert.equal(stats.total, 4) assert.equal(stats.total, 4)
assert.equal(stats.title, 'Metaltest') assert.equal(stats.title, 'Metaltest')
assert.equal(stats.testSuccess.length, 4) assert.equal(stats.testSuccess.length, 4)

View File

@ -1,6 +1,6 @@
{ {
"name": "metaltest", "name": "metaltest",
"version": "1.2.0", "version": "1.3.0",
"description": "Clone of baretest with reporting", "description": "Clone of baretest with reporting",
"type": "module", "type": "module",
"exports": "./index.js", "exports": "./index.js",

View File

@ -1,13 +1,15 @@
import chalk from 'chalk' import chalk from 'chalk'
const totalreporter = () => { const totalreporter = () => {
let success = 0, fail = 0 let success = 0, fail = 0, skip = 0
return { return {
success: (test) => { success++ }, success: (test) => { success++ },
fail: (test, e) => { fail++ }, fail: (test, e) => { fail++ },
skip: (test) => { skip++ },
msg: () => `${chalk.redBright(`Fail: ${fail}`)} msg: () => `${chalk.redBright(`Fail: ${fail}`)}
${chalk.greenBright(`Sucess: ${success}`)} ${chalk.greenBright(`Sucess: ${success}`)}
${chalk.blueBright(`Skip: ${skip}`)}
${chalk.yellowBright(`Ratio ${(success / (success + fail) * 100).toFixed(0)}%`)}` ${chalk.yellowBright(`Ratio ${(success / (success + fail) * 100).toFixed(0)}%`)}`
} }
} }