Skip to content

Commit

Permalink
added way to supress error stack
Browse files Browse the repository at this point in the history
  • Loading branch information
n2geoff committed Dec 2, 2018
1 parent c4cf2ed commit c1d8f38
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
**Test.it** is a small testing library for people that want to live in code, not in tests. No over engineering here. Inspired by the simplicity of libraries like [Tape](https://github.com/substack/tape),but the implementation ideas of things like [Expect](https://github.com/Automattic/expect.js) and [TinyTest](https://github.com/joewalnes/jstinytest)

This is probally not a *cure-all* testing solution, if you want something more robust checkout [Jasmine](), [Tape]() or [Mocha]() -- this is to...
This is probally not a *cure-all* testing solution, if you want something more robust checkout [Jasmine](https://jasmine.github.io/), [Tape](https://github.com/substack/tape) or [Mocha](https://mochajs.org/) -- this is to...

**Test small things, with small things**

### Features

- Works in the Browser
- Works with CommonJS (aka NodeJS)
- Barely over a 100 lines
- *Barely* over a 100 lines
- Single File
- No Dependicies
- 2kb footprint (*before gzip*)
Expand All @@ -21,7 +21,8 @@ This is probally not a *cure-all* testing solution, if you want something more r

**No Bloat Here!**

- [Download Now Available](https://raw.githubusercontent.com/n2geoff/testit/master/src/testit.min.js)
- [Download Here](https://raw.githubusercontent.com/n2geoff/testit/master/src/testit.js)
- [Or Minified Version Here](https://raw.githubusercontent.com/n2geoff/testit/master/src/testit.min.js)

## Usage

Expand Down Expand Up @@ -52,7 +53,7 @@ Error: just wanted to fail fast
# tests 2 pass 1 fail 1
```

A `+OK` will proceed test lines that *pass* and a `-ERR` for those that *fail*, the error stack is included after the failing test wrapped in `---`
A `+OK` will proceed test lines that *pass* and a `-ERR` for those that *fail*, An error stack is included by default after the failing test wrapped in `---`. You can suppress outputing the error stack by passing `false` as an argument to `run()`, ie `run(false)`.

You can, however, write your own custom test runner...

Expand Down Expand Up @@ -89,6 +90,8 @@ If using the optional `next` param will return results as JSON

From this object you can easily find the number of tests ran `pass.length`, number of failed tests `fail.length` or the total test count by adding the two. Simple.

> REMEMBER: you can bypass error output too
A sample test runner is provided for both **HTML** and **NODE** in the `test/` directory; `run.html` and `run.js` respectfully.

## Methods
Expand Down
14 changes: 12 additions & 2 deletions dist/testit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

const test = {
"_tests": {},
"run": function run(next) {
"run": function run(errors, next) {
if(typeof errors !== "boolean") {
next = errors;
errors = true;
}

let tests = this._tests;
let failed = [];
Expand All @@ -24,7 +28,13 @@
test();
passed.push(`\n+OK ${name}`);
} catch (err) {
failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`);
if (errors) {
console.log('ERRORS: YES');
failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`);
} else {
console.log('ERRORS: NO');
failed.push(`\n-ERR ${name}`);
}
}
});

Expand Down
3 changes: 2 additions & 1 deletion dist/testit.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions src/testit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

const test = {
"_tests": {},
"run": function run(next) {
"run": function run(errors, next) {
// rewrite to allow a show errors flag (optional)
if(typeof errors !== "boolean") {
next = errors;
errors = true;
}

let tests = this._tests;
// capture results
Expand All @@ -28,7 +33,13 @@
test();
passed.push(`\n+OK ${name}`);
} catch (err) {
failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`);
if (errors) {
console.log('ERRORS: YES');
failed.push(`\n-ERR ${name} \n --- \n ${err.stack} \n ---`);
} else {
console.log('ERRORS: NO');
failed.push(`\n-ERR ${name}`);
}
}
});

Expand Down
2 changes: 1 addition & 1 deletion test/run.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</head>
<body>
<script>
test.run(function(r) {
test.run(false, function(r) {
document.body.style.backgroundColor = (
r.fail.length ? "#ff9999" : "#99ff99"
);
Expand Down
2 changes: 1 addition & 1 deletion test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fs.readdir(__dirname, function(err, files) {
}

var tests = files.filter(function(item) {
return item.indexOf("spec.js") !== -1;
return item.indexOf("spec.js") !== -1;
});

tests.forEach(function(file) {
Expand Down

0 comments on commit c1d8f38

Please sign in to comment.