Skip to content

Commit

Permalink
implements #55
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurent RENARD authored and Laurent RENARD committed Dec 13, 2019
1 parent 9b2ef19 commit fc442de
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ If you have the following test.
import {test} from 'path/to/zora';

test('should result to the answer', t => {
const answer = 42
const answer = 42;
t.equal(answer, 42, 'answer should be 42');
});
```
Expand Down Expand Up @@ -367,7 +367,7 @@ You can ask zora to indent sub tests with configuration flag:
2. setting a global variable on the window object if you use the browser to run the test program
```markup
<script>INDENT=true;</script>
<script src="path/to/test/program></script>
<script src="path/to/test/program"></script>
```

```Javascript
Expand Down Expand Up @@ -594,13 +594,14 @@ aliases: falsy

### Create manually a test harness

You can discard the default test harness and create your own. This has various effect:
You can discard the default test harness and create your own. This has various effects:
- the reporting won't start automatically, you will have to trigger it yourself but it also lets you know when the reporting is over
- you can pass a custom reporter. Zora produces a stream of messages which are then transformed into a TAP stream. If you create the test harness yourself
you can directly pass your custom reporter to transform the raw messages stream.

```Javascript
const {createHarness, mochaTapLike} = require('zora');
const {createHarness} = require('zora');
const {indentedTapReporter} = require('zora-tap-reporter');

const harness = createHarness();
const {test} = harness;
Expand All @@ -622,7 +623,7 @@ test('a first sub test', t => {
});

harness
.report(mochaTapLike) // we have passed the mochaTapLike (with indention but here you can pass whatever you want
.report(indentedTapReporter())
.then(() => {
// reporting is over: we can release some pending resources
console.log('DONE !');
Expand Down
15 changes: 14 additions & 1 deletion src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@ import {defaultTestOptions, noop, testerFactory} from './commons';
export const tester = (description, spec, {offset = 0, skip = false, runOnly = false} = defaultTestOptions): Test => {
let executionTime = 0;
let error = null;
let done = false;
const assertions = [];
const collect = item => assertions.push(item);
const collect = item => {
if (done) {
throw new Error(`test "${description}"
tried to collect an assertion after it has run to its completion.
You might have forgotten to wait for an asynchronous task to complete
------
${spec.toString()}
`);
}
assertions.push(item);
};
const specFunction = skip === true ? noop : function zora_spec_fn() {
return spec(assert(collect, offset, runOnly));
};
Expand All @@ -18,6 +29,8 @@ export const tester = (description, spec, {offset = 0, skip = false, runOnly = f
return result;
} catch (e) {
error = e;
} finally {
done = true;
}
})();

Expand Down
9 changes: 9 additions & 0 deletions test/samples/cases/late_collect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {test} from '../../../dist/bundle/module.js';

test(`late collection`, async t => {
t.ok(true);

setTimeout(() => {
t.ok(true);
}, 50);
});
27 changes: 24 additions & 3 deletions test/samples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const {readdirSync, readFileSync} = require('fs');
const node = process.execPath;

const sampleRoot = path.resolve(process.cwd(), './test/samples/cases/');
const files = readdirSync(sampleRoot).filter(f => f.split('.').reverse()[0] === 'js');
const files = readdirSync(sampleRoot)
.filter(f => f.split('.').reverse()[0] === 'js' && f !== 'late_collect.js'); // late collect will be checked separately

for (const f of files) {
test(`sample output: ${f}`, t => {
Expand All @@ -25,7 +26,7 @@ for (const f of files) {
test(`sample output indented: ${f}`, t => {
const cp = spawnSync(node, ['-r', 'esm', f], {
cwd: sampleRoot,
stdio: ['pipe', 'pipe', 'ignore'],
stdio: ['pipe', 'pipe', 'pipe'],
env: {
RUN_ONLY: f.startsWith('only'),
INDENT: true
Expand All @@ -39,4 +40,24 @@ for (const f of files) {
t.equal(actualOutput, expectedOutput);
t.end();
});
}
}

test(`late collect should report an error on stderr`, t => {
const cp = spawnSync(node, ['-r', 'esm', 'late_collect.js'], {
cwd: sampleRoot,
stdio: ['pipe', 'pipe', 'pipe']
});
const actualOutput = cp.stderr.toString();
t.ok(actualOutput.startsWith(`Error: test "late collection"
tried to collect an assertion after it has run to its completion.
You might have forgotten to wait for an asynchronous task to complete
------
async t => {
t.ok(true);
setTimeout(() => {
t.ok(true);
}, 50);
}`));
t.end();
});
2 changes: 1 addition & 1 deletion test/unit/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ test('test harness with nested sub tests', async (t) => {
t.end();
});

test('test haness with nested async sub test: should stream in the declared order', async (t) => {
test('test harness with nested async sub test: should stream in the declared order', async (t) => {
const harness = createHarness();
const {test} = harness;
test('keep parallel', async (t) => {
Expand Down

0 comments on commit fc442de

Please sign in to comment.