I've been trying to run a mock API server that I wrote and use it with tape, but it seems that when the mock API server is running, tape cannot exit. Although I get the usual message,
1..39
# tests 39
# pass 39
# ok
the suite never really ends.
Is there a way to know when all tests are done? I've read the docs and couldn't find anything relevant.
I have the same problem. My use case: I have a web page which runs tape in the browser, and I have a phantomjs-script that loads that page. When the tests are done (and all results have been written), I want to exit the phantomjs process.
A workaround seems be:
var test = require('tape');
test('timing test', function (t) {
...
});
test.createStream().on('end', function () { ... }); # <---
But +1 for a more official API.
:+1:
This is my quick&dirty solution as the one @webthusiast mentioned did not work for me.
https://github.com/Pajk/tape/commit/a4355d8466a0894855c470164f9f7c2e5d227af5
It's called only when all tests pass but that's enough for my case. I use it to disconnect node-postgres connection pool.
Also interested in this feature. I tried @webthusiast's method, but the 'end' event never fires.
Or perhaps my problem is different - there is no summary printed at the end of the tests... It looks like the tests are simply not ending.
+1
I've decided this is now a useful feature >_>;
@Raynos How does tape know when to emit the end event if there are async tests? Do all calls to test need to occur synchronously?
At some point the program exits and prints the number of oks. Thats a good place to hook into.
Have to read more code.
I think it's simply reference counting.
My asyc test just hangs indefinitely.
I was initially encountering this issue only to discover the real culprit was my MySQL connection not closing (remained hanging) after the tests. I have yet to encounter this problem with only static or local files and processes. Figured that insight may or may not be of use to others.
I think different people might be looking at this issue from different angles. I think the OP describes a problem which could be investigated/solved if a new feature is added (@tsironis please correct me if I'm wrong). The name of the issue also suggests it's a feature request. I think related bugs might best be put in their own issues.
@Raynos: I'd like to start with writing a test, so it's probably a good start to agree on an interface. I'm thinking of making tape a promise, so that the following is possible:
test = require('tape');
test(...).then(onEnd);
Or:
test = require('tape');
Promise.all([
test(...),
test(...),
...
]).then(onEnd);
Maybe an additional event/callback-based implementation would be desired later, but I don't immediately see an obvious implementation for that. Agree? Or suggestions?
@webthusiast no promises.
The cleanest thing to do is:
var test = require('tape');
test.on('end', onEnd);
+1 on
test.on('end', onEnd)`
Having this hook available would probably be useful for cuke-tap, or any other module that wraps around tape.
The problem with that is that tape is a function. So it can't also inherit from EventEmitter. It could maybe use it as a mixin, but that seems a bit hacky. What about tape.tearDown(cb)?
Tear down is reasonable.
tearDown() sounds like a command that will tear apart the test, not registering a listener that fires when all the tests are finished
Let's rename to
tape.onFinish(cb);
no :bike: :house: here, onFinish SGTM
I hope this workflow is workable: both b09c92e and b01c08b should be pulled.
@webthusiast do you want to open a pull request ? :)
+1
Hi @Raynos @substack, is this pull request still on your radar?
I was initially encountering this issue only to discover the real culprit was my MySQL connection not closing (remained hanging) after the tests.
This is ended up being my problem also, I think. I wrapped certain test files in a promise handler and cleaned up my DB connections in the finally handler.
+1: I'm writing a custom test runner that seems to be working perfectly, except it exits before any tests are run because the tests are asynchronous. I'm writing the custom running so I can configure and maintain output formatter and output stream (and other resources) inside JS code.
@Raynos I'm not sure this feature entirely works. Since globbing happens asynchronously, you can't guarantee the onFinish hook is correct (E.g. it'll call onFinish before any tests even run). I think switching to synchronous globs would correct this.
I'm agree with @blakeembrey, I was trying to setup an onFinish hook in a preloaded module without any success because of asynchronous glob in tape cli.
Most helpful comment
@Raynos I'm not sure this feature entirely works. Since globbing happens asynchronously, you can't guarantee the
onFinishhook is correct (E.g. it'll callonFinishbefore any tests even run). I think switching to synchronous globs would correct this.