Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: stop using class extends as it breaks rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
mhevery committed Oct 1, 2016
1 parent 7da0c52 commit b52cf02
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
sudo: false
node_js:
- '4.2.1'
- '6.3.1'
env:
global:
- BROWSER_PROVIDER_READY_FILE=/tmp/sauce-connect-ready
Expand Down
57 changes: 32 additions & 25 deletions lib/jasmine/jasmine.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict';
(() => {
var __extends = function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
// Patch jasmine's describe/it/beforeEach/afterEach functions so test code always runs
// in a testZone (ProxyZone). (See: angular/zone.js#91 & angular/angular#10503)
if (!Zone) throw new Error("Missing: zone.js");
if (typeof jasmine == 'undefined') throw new Error("Missing: jasmine.js");
if (typeof jasmine == 'undefined') throw new Error("Missing: jasmine.js");
if (jasmine['__zone_patch__']) throw new Error("'jasmine' has already been patched with 'Zone'.");
jasmine['__zone_patch__'] = true;

Expand All @@ -13,19 +18,19 @@
if (!ProxyZoneSpec) throw new Error("Missing: ProxyZoneSpec");

const ambientZone = Zone.current;
// Create a synchronous-only zone in which to run `describe` blocks in order to raise an
// error if any asynchronous operations are attempted inside of a `describe` but outside of
// Create a synchronous-only zone in which to run `describe` blocks in order to raise an
// error if any asynchronous operations are attempted inside of a `describe` but outside of
// a `beforeEach` or `it`.
const syncZone = ambientZone.fork(new SyncTestZoneSpec('jasmine.describe'));

// This is the zone which will be used for running individual tests.
// It will be a proxy zone, so that the tests function can retroactively install
// different zones.
// different zones.
// Example:
// - In beforeEach() do childZone = Zone.current.fork(...);
// - In it() try to do fakeAsync(). The issue is that because the beforeEach forked the
// - In it() try to do fakeAsync(). The issue is that because the beforeEach forked the
// zone outside of fakeAsync it will be able to escope the fakeAsync rules.
// - Because ProxyZone is parent fo `childZone` fakeAsync can retroactively add
// - Because ProxyZone is parent fo `childZone` fakeAsync can retroactively add
// fakeAsync behavior to the childZone.
let testProxyZone: Zone = null;

Expand All @@ -35,43 +40,43 @@
let originalJasmineFn: Function = jasmineEnv[methodName];
jasmineEnv[methodName] = function(description: string, specDefinitions: Function) {
return originalJasmineFn.call(this, description, wrapDescribeInZone(specDefinitions));
}
}
});
['it', 'xit', 'fit'].forEach((methodName) => {
let originalJasmineFn: Function = jasmineEnv[methodName];
jasmineEnv[methodName] = function(description: string, specDefinitions: Function, timeout: number) {
arguments[1] = wrapTestInZone(specDefinitions);
return originalJasmineFn.apply(this, arguments);
}
}
});
['beforeEach', 'afterEach'].forEach((methodName) => {
let originalJasmineFn: Function = jasmineEnv[methodName];
jasmineEnv[methodName] = function(specDefinitions: Function, timeout: number) {
arguments[0] = wrapTestInZone(specDefinitions);
return originalJasmineFn.apply(this, arguments);
}
}
});

/**
* Gets a function wrapping the body of a Jasmine `describe` block to execute in a
* synchronous-only zone.
/**
* Gets a function wrapping the body of a Jasmine `describe` block to execute in a
* synchronous-only zone.
*/
function wrapDescribeInZone(describeBody: Function): Function {
return function() {
return syncZone.run(describeBody, this, arguments as any as any[]);
}
}

/**
* Gets a function wrapping the body of a Jasmine `it/beforeEach/afterEach` block to
* execute in a ProxyZone zone.
/**
* Gets a function wrapping the body of a Jasmine `it/beforeEach/afterEach` block to
* execute in a ProxyZone zone.
* This will run in `testProxyZone`. The `testProxyZone` will be reset by the `ZoneQueueRunner`
*/
function wrapTestInZone(testBody: Function): Function {
// The `done` callback is only passed through if the function expects at least one argument.
// Note we have to make a function with correct number of arguments, otherwise jasmine will
// think that all functions are sync or async.
return (testBody.length == 0)
return (testBody.length == 0)
? function() { return testProxyZone.run(testBody, this); }
: function(done) { return testProxyZone.run(testBody, this, [done]); };
}
Expand All @@ -90,17 +95,17 @@
}

const QueueRunner = (jasmine as any).QueueRunner as { new(attrs: QueueRunnerAttrs): QueueRunner };
(jasmine as any).QueueRunner = class ZoneQueueRunner extends QueueRunner {
constructor(attrs: QueueRunnerAttrs) {
(jasmine as any).QueueRunner = (function (_super) {
__extends(ZoneQueueRunner, _super);
function ZoneQueueRunner(attrs) {
attrs.onComplete = ((fn) => () => {
// All functions are done, clear the test zone.
testProxyZone = null;
ambientZone.scheduleMicroTask('jasmine.onComplete', fn);
})(attrs.onComplete);
super(attrs);
_super.call(this, attrs);
}

execute() {
ZoneQueueRunner.prototype.execute = function () {
if(Zone.current !== ambientZone) throw new Error("Unexpected Zone: " + Zone.current.name);
testProxyZone = ambientZone.fork(new ProxyZoneSpec());
if (!Zone.currentTask) {
Expand All @@ -109,10 +114,12 @@
// addEventListener callback would think that it is the top most task and would
// drain the microtask queue on element.click() which would be incorrect.
// For this reason we always force a task when running jasmine tests.
Zone.current.scheduleMicroTask('jasmine.execute().forceTask', () => super.execute());
Zone.current.scheduleMicroTask('jasmine.execute().forceTask',
() => QueueRunner.prototype.execute.call(this));
} else {
super.execute();
_super.prototype.execute.call(this);
}
}
};
};
return ZoneQueueRunner;
}(QueueRunner));
})();

0 comments on commit b52cf02

Please sign in to comment.