Skip to content

Commit

Permalink
Implement basic cancelation
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Aug 15, 2017
1 parent 9becc5a commit 75cf6e5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
22 changes: 11 additions & 11 deletions packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ module.exports = function(j$) {
};

const clearResourcesForRunnable = function(id) {
console.log('clear resource', id)
spyRegistry.clearSpies();
delete runnableResources[id];
};
Expand Down Expand Up @@ -162,7 +161,7 @@ module.exports = function(j$) {
return seed;
};

async function queueRunnerFactory(options) {
function queueRunnerFactory(options) {
options.clearTimeout = realClearTimeout;
options.fail = self.fail;
options.setTimeout = realSetTimeout;
Expand All @@ -189,22 +188,23 @@ module.exports = function(j$) {
}

const uncaught = (err) => {
let name
let current

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

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

reporter.jasmineStarted({
totalSpecsDefined,
Expand Down
12 changes: 10 additions & 2 deletions packages/jest-jasmine2/src/jasmine/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ Spec.prototype.execute = function(onComplete, enabled) {
const fns = this.beforeAndAfterFns();
const allFns = fns.befores.concat(this.queueableFn).concat(fns.afters);

this.queueRunnerFactory({
this.currentRun = this.queueRunnerFactory({
queueableFns: allFns,
onException() {
self.onException.apply(self, arguments);
},
userContext: this.userContext(),
}).then(() => complete(true));
});

this.currentRun.then(() => complete(true));

function complete(enabledAgain) {
self.result.status = self.status(enabledAgain);
Expand All @@ -114,6 +116,12 @@ Spec.prototype.execute = function(onComplete, enabled) {
}
};

Spec.prototype.cancel = function cancel() {
if (this.currentRun) {
this.currentRun.cancel();
}
};

Spec.prototype.onException = function onException(error) {
if (Spec.isPendingSpecException(error)) {
this.pend(extractCustomPendingMessage(error));
Expand Down
27 changes: 22 additions & 5 deletions packages/jest-jasmine2/src/queue_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ type QueueableFn = {
timeout?: () => number,
};

async function queueRunner(options: Options) {
function createCancelToken () {
let res
const token = new Promise(resolve => {
res = resolve
})

token.cancel = res
return token
}

function queueRunner(options: Options) {
const mapper = ({fn, timeout}) => {
const promise = new Promise(resolve => {
const next = function(err) {
Expand Down Expand Up @@ -62,11 +72,18 @@ async function queueRunner(options: Options) {
},
);
};
const token = createCancelToken();
const returnPromise = Promise.race([
options.queueableFns.reduce(
(promise, fn) => promise.then(() => mapper(fn)),
Promise.resolve(),
),
token,
]);

returnPromise.cancel = token.cancel;

return options.queueableFns.reduce(
(promise, fn) => promise.then(() => mapper(fn)),
Promise.resolve(),
);
return returnPromise;
}

module.exports = queueRunner;

0 comments on commit 75cf6e5

Please sign in to comment.