Skip to content

Commit

Permalink
Added FAQs and started work on Async tests
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorhakes committed Feb 7, 2016
1 parent a2b8065 commit c800dc1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,44 @@ Simple test structure that is easy to learn and use. All tests are really fast b
- 100% Test Coverage
- Coverage support with Istanbul

## Compared to other frameworks
## Compared to other frameworks <a name="comparison"></a>
#### Mocha
Painless Advantages
- Has everything you need included (assertions, mocking, etc)
- Supports Async/Await, Promises, Generators, Observables, Streams, Processes
- Doesn't need a separate executable to run (just run `node test.js`)
- Is easier to debug (single process)
- Has no globals
- Much faster
- Much faster to run tests
Mocha Advantages
- Supports browser and node (Painless is only node. It will support browsers soon.)
- More widely used (more battle tested)
- Integrated coverage

#### Ava
Painless Advantages
- Easier to debug (single process)
- Has everything you need included (assertions, mocking, etc)
- Doesn't need a separate executable to run (just run `node test.js`)
- Faster for most types of tests (even IO tests with painless --async flag)
- Doesn't require babel (write your tests in ES5, typescript, coffeescript, etc)
- Built-in support for Node Streams and processes
- Built-in support for Node streams and processes
Ava Advantages
- Test groups are isolated (Less likely for tests to affect each other)

#### Jasmine
Painless Advantages
- Supports Async/Await, Promises, Generators, Observables, Streams, Processes
- Doesn't need a separate executable to run (just run `node test.js`)
- Easier to debug (single process)
- Has no globals
- Much faster
Jasmine Advantages
- Supports browser and node (Painless is only node. It will support browsers soon.)
- More widely used (more battle tested)

#### Tape
Painless Advantages
- Supports Async/Await, Promises, Generators, Observables, Streams, Processes
- Has everything you need included (assertions, mocking, etc)
- Simpler test syntax
Expand Down Expand Up @@ -209,6 +222,12 @@ var sinon = painless.sinon;
var xhr = sinon.useFakeXMLHttpRequest();
```

### Command Line Options
--async,-a Run tests async. This will speed up tests that use IO or network. It is not recommended while debugging. It will make tests tough to reason about.
--bunch,-b Bunch size. If using the async flag, it determines the number of tests to run at the same time. By default this is 10.
--tap,-t Use tap output reporter instead of the default spec reported


### Code Coverage
Code coverage is really easy to use. Just install istanbul
```
Expand All @@ -218,3 +237,10 @@ Then you can run coverage by
```
istanbul cover ./node_modules/bin/painless -- test/**/*.js
```

## FAQS
1. Why should I use painless over other test libraries?
Go [here](#comparison) for comparisons to other test libraries. Painless is not groundbreaking in any way. If you like it, use it. It is more important to test your javascript code. The test framework is a minor detail.
2. Why bundle other libraries (Chai, Sinon, etc)? Why not let the user decide?
Painless tries to achieve a test library that just works. The goal is to be able to start writing tests as soon as possible. It is possible to switch out assertion and mocking libraries, but defaults are available.

2 changes: 1 addition & 1 deletion examples/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ test('simple sync pass', function sync1() {
});

test('simple sync pass 2', function sync2() {
assert.equal(5, 3);
assert.equal(5, 5);
});
49 changes: 39 additions & 10 deletions lib/execute-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
var through = require('through');
var sequentialTests = require('./sequential-tests');
var setAsap = require('setasap');
var bunchSize = 10;

function executeGroups(groups) {
function executeGroups(groups, async) {
var output = through();

function endStream() {
output.queue(null);
}

function createStream(index) {
var stream = executeGroup(groups[index]);
var stream = executeGroup(groups[index], async);
stream.on('data', output.queue);
stream.on('end', index < groups.length - 1 ? createStream.bind(null, index + 1) : endStream);
}
Expand All @@ -21,10 +22,11 @@ function executeGroups(groups) {
return output;
}

function executeGroup(group) {
var responses = {};
function executeGroup(group, async) {
var testIndex = 0;
var resultIndex = 0;
var len = group.tests.length;
var responses = Array(len);
var output = through();

function createOnSuccess(index) {
Expand All @@ -42,16 +44,17 @@ function executeGroup(group) {
}

function outputResults() {
while (responses[testIndex]) {
output.queue(responses[testIndex]);
delete responses[testIndex];
testIndex++;
while (responses[resultIndex]) {
output.queue(responses[resultIndex]);
delete responses[resultIndex];
resultIndex++;
}
if (testIndex === len) {
if (resultIndex === len) {
output.queue(null);
}
}


function executeNext() {
if (!len) {
setAsap(function next() {
Expand All @@ -66,13 +69,39 @@ function executeGroup(group) {
.catch(createOnError(testIndex))
.then(outputResults)
.then(function onNext() {
testIndex++;
if (testIndex < len) {
executeNext();
}
});
}

executeNext();
function executeBunch(size) {
var promArr = [];
var prom;
for (var i = 0; i < size && testIndex < len; i++, testIndex++) {
var allTests = [].concat(group.beforeEach, [group.tests[testIndex]], group.afterEach);
prom = sequentialTests(allTests)
.then(createOnSuccess(testIndex))
.catch(createOnError(testIndex))
.then(outputResults);

promArr.push(prom);
}

Promise.all(promArr)
.then(function onDone() {
if (testIndex < len) {
executeBunch(size);
}
});
}

if (async) {
executeBunch(bunchSize);
} else {
executeNext();
}

return output;
}
Expand Down

0 comments on commit c800dc1

Please sign in to comment.