Comparing version 1.0.3 to 1.1.0
44
index.js
@@ -16,3 +16,4 @@ /*------------------------------------- | ||
currentStage = '', | ||
errorOccured = false, | ||
fails = [], | ||
totalErrors = 0, | ||
stageError = false, | ||
@@ -33,3 +34,3 @@ runningTest = false, | ||
async function starTest(testFunction) { | ||
async function start(testFunction) { | ||
if (runningTest) | ||
@@ -49,4 +50,6 @@ return testQueue.push(testFunction) | ||
if (errorOccured) | ||
console.log(chalk.bold.yellow(`An error occured during the test ${name}${sad()}\n`)) | ||
if (fails.length == 1) | ||
console.log(chalk.bold.yellow(`One error occured during the test ${name}${sad()}\n`)) | ||
else if (fails.length > 1) | ||
console.log(chalk.bold.yellow(`${fails.length} errors occured during the test ${name}${sad()}\n`)) | ||
else | ||
@@ -74,3 +77,4 @@ console.log(chalk.bold.green(`The test ${name}has been successfully passed ${happy()}\n`)) | ||
currentStage = '' | ||
errorOccured = false | ||
fails.length = 0 | ||
totalErrors = 0 | ||
stageError = false | ||
@@ -80,3 +84,3 @@ runningTest = false | ||
if (next) | ||
starTest(next) | ||
start(next) | ||
} | ||
@@ -92,5 +96,7 @@ | ||
return | ||
if (stageError) { | ||
if (fails.length) { | ||
console.log(chalk.bold.red("✗ "+currentStage)) | ||
stageError = false | ||
for (const fail of fails) | ||
console.log(chalk.gray.italic(" Error at : " + chalk.reset.bold.italic.red(fail))) | ||
fails.length = 0 | ||
} | ||
@@ -101,18 +107,5 @@ else | ||
function test(conditionA, conditionB) { | ||
switch (arguments.length) { | ||
case 1: | ||
if (conditionA) | ||
return | ||
break | ||
case 2: | ||
if (conditionA === conditionB) | ||
return | ||
break | ||
default: | ||
throw "Bad use of function FarTest.test(). One or two arguments are expected." | ||
} | ||
stageError = true | ||
errorOccured = true | ||
function test(conditionA, testDescription='') { | ||
if (conditionA) return | ||
fails.push(testDescription) | ||
} | ||
@@ -122,5 +115,6 @@ | ||
module.exports = { | ||
starTest, | ||
start, | ||
starTest: start, // for retro-compatibility | ||
stage, | ||
test, | ||
} |
{ | ||
"name": "fartest", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "A minimal, colorful and enjoyable test library for small applications", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,8 +0,9 @@ | ||
## FarTest | ||
*FAst and smaRT TESTing* | ||
FarTest is a minimal, colorful and enjoyable test library for small applications. | ||
FarTest is an obvious, colorful and enjoyable test library for small applications. | ||
## Installation | ||
Your terminal need to accept UTF-8 characters and ANSI colors for a better experience. | ||
# Installation | ||
``` | ||
@@ -16,6 +17,6 @@ npm install --save-dev fartest | ||
## Basic usage | ||
# Usage | ||
FarTest export three functions : | ||
- *starTest* - start a new test, | ||
- *start* - start a new test, | ||
- *stage* - define the current stage inside a test, | ||
@@ -26,7 +27,7 @@ - *test* - check an assertion inside a test. | ||
```javascript | ||
const {starTest, stage, test} = require('fartest') | ||
const { start, stage, test } = require('fartest') | ||
``` | ||
With ES modules : | ||
```javascript | ||
import {starTest, stage, test} from 'fartest') | ||
import { start, stage, test } from 'fartest') | ||
``` | ||
@@ -38,17 +39,25 @@ | ||
// and is optional | ||
starTest(async function MyAwesomeTest() { | ||
start(async function MyAwesomeTest() { | ||
// we define the current stage of our test | ||
stage('Some succesful tests') | ||
test(1 == 1) // simple assertion | ||
test(21, 21) // same as test(21 === 21) | ||
// simple assertion | ||
test(1 == "1") | ||
// the test description will be displayed in case of error | ||
test(21 == "21", "Test description") | ||
stage('A simple test which will not succeed') | ||
test(21, "21") // will fail because types don't match | ||
test(21 === "21", "Test description") // will fail because types don't match | ||
stage('Crash test') | ||
undefined.coco = 321321 // any invalid code will be caught | ||
undefined.coco = 321321 // any invalid code will be caught | ||
}) | ||
``` | ||
## Test asynchronous functions | ||
Declare your main test functions as `async` and just use `await` anywhere an asynchronous function is used. | ||
## Running multiple tests | ||
@@ -58,14 +67,14 @@ You can run multiple tests at once, in which case they will be executed one after another : | ||
// test 1 | ||
starTest(async function CoolTest() { | ||
start(async function CoolTest() { | ||
stage('1 == 1') | ||
test(1 == 1) | ||
test(1 == 1) | ||
stage('2 == "2"') | ||
test(2 == "2") | ||
test(2 == "2") | ||
}) | ||
// test 2 | ||
starTest(async function SuperCoolTest() { | ||
start(async function SuperCoolTest() { | ||
stage('3 == 3') | ||
test(3 == 3) | ||
test(3 == 3) | ||
}) | ||
@@ -72,0 +81,0 @@ ``` |
5021
4
104
82