From 90b20029c9dde21218e2464a1e8958435fd42286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Matte?= Date: Fri, 30 Dec 2022 13:42:42 -0500 Subject: [PATCH] Add skip feature --- README.md | 13 +++++++++++++ lib/metaltest.js | 13 ++++++++++++- lib/metaltest.test.js | 2 ++ package.json | 2 +- reporter/totalreporter.js | 4 +++- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ccbec0e..f724b57 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ const stats = await test.run(); title: 'Metaltest', success: 2, fail: 0, + skip: 0, total: 2, testSuccess: [], testFail: [] @@ -34,6 +35,18 @@ const stats = await test.run(); 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 test.js diff --git a/lib/metaltest.js b/lib/metaltest.js index 8c3dbbc..372afd7 100644 --- a/lib/metaltest.js +++ b/lib/metaltest.js @@ -1,12 +1,14 @@ const metaltest = (title) => { const suite = [] const only = [] + const skipped = [] const before = [] const after = [] const end = [] let success = 0 let fail = 0 + let skip = 0 const testSuccess = [] const testFail = [] @@ -22,7 +24,7 @@ const metaltest = (title) => { break } - return { title, success, fail, total: success + fail, testSuccess, testFail } + return { title, success, fail, skip, total: success + fail, testSuccess, testFail } } const runner = (name, fn) => { @@ -30,6 +32,7 @@ const metaltest = (title) => { } runner.only = (name, fn) => { only.push({ name, fn }) } + runner.skip = (name, fn) => { skipped.push({ name, fn }) } runner.before = (fn) => { before.push(fn) } runner.after = (fn) => { after.push(fn) } runner.end = (name, fn) => { end.push({ name, fn }) } @@ -46,6 +49,14 @@ const metaltest = (title) => { await notify(reporters, 'start', title) 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) { try { for (const fn of before) await fn() diff --git a/lib/metaltest.test.js b/lib/metaltest.test.js index 33b7529..e3ac51f 100644 --- a/lib/metaltest.test.js +++ b/lib/metaltest.test.js @@ -12,10 +12,12 @@ test('throw', () => { throw new Error('message') }, { name: 'Error', message: 'message' }) }) +test.skip('skip', () => { }) test.end('verify', async (stats) => { assert.equal(stats.success, 4) assert.equal(stats.fail, 0) + assert.equal(stats.skip, 1) assert.equal(stats.total, 4) assert.equal(stats.title, 'Metaltest') assert.equal(stats.testSuccess.length, 4) diff --git a/package.json b/package.json index 96fa633..cbacbac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "metaltest", - "version": "1.2.0", + "version": "1.3.0", "description": "Clone of baretest with reporting", "type": "module", "exports": "./index.js", diff --git a/reporter/totalreporter.js b/reporter/totalreporter.js index 09d2604..5cd9923 100644 --- a/reporter/totalreporter.js +++ b/reporter/totalreporter.js @@ -1,13 +1,15 @@ import chalk from 'chalk' const totalreporter = () => { - let success = 0, fail = 0 + let success = 0, fail = 0, skip = 0 return { success: (test) => { success++ }, fail: (test, e) => { fail++ }, + skip: (test) => { skip++ }, msg: () => `${chalk.redBright(`Fail: ${fail}`)} ${chalk.greenBright(`Sucess: ${success}`)} +${chalk.blueBright(`Skip: ${skip}`)} ${chalk.yellowBright(`Ratio ${(success / (success + fail) * 100).toFixed(0)}%`)}` } }