Skip to content

Commit

Permalink
wip: start handling async errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Aug 16, 2017
1 parent 417182b commit fcb4bb3
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 5 deletions.
40 changes: 40 additions & 0 deletions integration_tests/jasmine_async/__tests__/promise_it.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe('promise it', () => {
done();
});

it('works with async done', done => {
setTimeout(done, 1);
});

it('is bound to context object', () => {
return new Promise(resolve => {
if (this.someContextValue !== 'value') {
Expand All @@ -46,6 +50,42 @@ describe('promise it', () => {
done.fail(new Error('done.fail was called'));
});

it('works with done(error)', done => {
done(new Error('done was called with error'));
})

it('fails if failed expectation with done', done => {
expect(true).toEqual(false);
done();
})

it('fails if failed expectation with done - async', done => {
setTimeout(() => {
expect(true).toEqual(false)
done()
}, 1)
})

it('fails with thrown error with done - sync', done => {
throw new Error('sync fail');
done();
})

it('fails with thrown error with done - async', done => {
setTimeout(() => {
throw new Error('async fail');
done();
}, 1);
})

// I wish it was possible to catch this but I do not see a way.
// Currently both jest and mocha will pass this test.
it.skip('fails with thrown error - async', () => {
setTimeout(() => {
throw new Error('async fail - no done');
}, 1);
})

it('fails a sync test', () => {
expect('sync').toBe('failed');
});
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-cli/src/reporters/coverage_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ function formatCoverageError(error, filename: Path): SerializableError {

// Make sure uncaught errors are logged before we exit.
process.on('uncaughtException', err => {
console.error(err.stack);
process.exit(1);
// console.error(err.stack);
// process.exit(1);
});

module.exports = (
Expand Down
26 changes: 26 additions & 0 deletions packages/jest-jasmine2/src/__tests__/queue_runner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,30 @@ describe('queueRunner', () => {

expect(options.fail).toHaveBeenCalledWith('miserably', 'failed');
});

it('calls `fail` when done(error) is invoked', async () => {
const error = new Error('I am an error')
const fail = jest.fn();
const fnOne = jest.fn(next => next(error));
const fnTwo = jest.fn(next => next());
const options = {
clearTimeout,
fail,
onException: () => {},
queueableFns: [
{
fn: fnOne,
},
{
fn: fnTwo,
},
],
setTimeout,
};
await queueRunner(options);
expect(fnOne).toHaveBeenCalled();
expect(fail).toHaveBeenCalledWith(error);
// Even if `fail` is called, the queue keeps running.
expect(fnTwo).toHaveBeenCalled();
});
});
22 changes: 22 additions & 0 deletions packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ module.exports = function(j$) {
};

const clearResourcesForRunnable = function(id) {
console.log('clear resource', id)
spyRegistry.clearSpies();
delete runnableResources[id];
};
Expand Down Expand Up @@ -187,6 +188,24 @@ module.exports = function(j$) {
}
}

const uncaught = (err) => {
let name
let current
if (currentSpec) {
current = currentSpec
name = current.getFullName()
current.onException(err)
// TODO: how to cancel the current running spec
} else {
// TODO: Handle top level failures
}
console.error('caught in ' + name)
}

console.log('START')
process.on('uncaughtException', uncaught)
process.on('unhandledRejection', uncaught)

reporter.jasmineStarted({
totalSpecsDefined,
});
Expand Down Expand Up @@ -215,6 +234,9 @@ module.exports = function(j$) {
reporter.jasmineDone({
failedExpectations: topSuite.result.failedExpectations,
});

process.removeListener('uncaughtException', uncaught)
process.removeListener('unhandledRejection', uncaught)
};

this.addReporter = function(reporterToAdd) {
Expand Down
8 changes: 7 additions & 1 deletion packages/jest-jasmine2/src/queue_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ type QueueableFn = {
async function queueRunner(options: Options) {
const mapper = ({fn, timeout}) => {
const promise = new Promise(resolve => {
const next = () => resolve();
const next = function(err) {
if (err) {
options.fail.apply(null, arguments);
}
resolve();
};

next.fail = function() {
options.fail.apply(null, arguments);
resolve();
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-runner/src/test_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import type {RawModuleMap} from 'types/HasteMap';

// Make sure uncaught errors are logged before we exit.
process.on('uncaughtException', err => {
console.error(err.stack);
process.exit(1);
// console.error(err.stack);
// process.exit(1);
});

import HasteMap from 'jest-haste-map';
Expand Down

0 comments on commit fcb4bb3

Please sign in to comment.