-
Notifications
You must be signed in to change notification settings - Fork 1.4k
after.always
doesn't run if there are uncaught exceptions
#917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
New features always come with docs. That's a requirement. |
Hm. I'll recreate my issue, give me some seconds. |
OK, this is actually a bug. This code will reproduce: var test = require('ava');
var r = require('request-promise');
test.before(() => console.log('before'));
test('failing test', (t) =>
r('http://httpbin.org/status/400')
.catch(err => t.true(err.statusCode === 200)));
test('another failing test', (t) =>
r({
uri: 'http://httpbin.org/status/400',
method: 'POST',
body: { should: 'be string' } // this will throw
}).catch(err => t.true(err.statusCode === 200)));
test.after.always(() => console.log('after')); Seems to be a race condition. |
Throws from a place ava is unable to catch? Might not really an issue, as this is misuse of |
after.always
doesn't run if request-promise is misused (throws)
Simpler test case: var test = require('ava');
test.before(() => console.log('before'));
test.cb('another failing test', (t) => {
setTimeout(() => {
t.true(true);
throw new Error('catch me');
}, 1);
});
test.after.always(() => console.log('after')); Output
Is this expected? |
We shut the process down immediately if an uncaught exception happens, so I guess we could trigger |
Nice when using |
after.always
doesn't run if request-promise is misused (throws)after.always
doesn't run if there are uncaught exceptions
You will always be able to create situations where your cleanup code fails to do what you want, even with import test from 'ava';
import mkdirp from 'mkdirp';
import rimraf from 'rimraf';
test.cb(t => {
setTimeout(() => {
mkdirp('some/deeply/nested/directory');
}, 20);
setTimeout(() => {
cb();
throw new Error('woops');
}, 10);
});
test.after.always.cb(t => {
rimraf('some/deeply', t.end); // <= this is likely executing before the `mkdirp` line.
}); Instead of banking on the system being left in a clean state by a previous run, I recommend using function cleanup(t) {
if (fs.exists('some/deeply')) {
rimraf('some/deeply', t.end);
}
}
test.before.cb(cleanup);
// if leaving that directory hanging around bothers you:
test.after.cb(cleanup); |
Adding context, the before/after is starting/stopping the rethink database, which is quite expensive (~2-3 seconds). |
Went for checking if database and server are alive: let db, server;
test.cb.before((t) => {
let timeout = 0;
if (child.spawnSync('pgrep', ['-lf', 'rethinkdb']).status === 1) {
console.log('---- STARTING DATABASE ----');
db = child.spawn('rethinkdb', [], { cwd: __dirname, stdio: 'inherit' });
timeout += 3000;
}
if (child.spawnSync('pgrep', ['-lf', '(node|nodemon) index.js']).status === 1) {
console.log('---- STARTING SERVER ----');
server = child.fork('index.js', [], { cwd: __dirname, stdio: 'inherit' });
timeout += 100;
}
// wait for server to start
function wait () {
request(HOST + 'captcha')
.then(() => t.end())
.catch(wait);
}
setTimeout(wait, timeout);
}); Drawback: not cross platform. |
Uncaught exceptions crash the worker process. We can't really make any guarantees about whether |
Thanks for the help and clarifications 👍 |
Hi, as far as I understand from this PR, |
Hey @Githubber2021, I think this comment still stands:
|
Does anybody know of a workaround for this? In my case, a process in the |
@fcastilloec you could use |
Hi! This is the second time I read the documentation at README.md and find that the feature I'm using is not yet released (after some debugging). I'm not sure what it was last time, but this time it is
after.always
found in commits since release.Any thoughts about stabilizing/syncing the docs with the release cycle?
The text was updated successfully, but these errors were encountered: