build: upgrade jasmine to 2.99.x and fix tests (#19904)

PR Close #19904
This commit is contained in:
George Kalpakas 2018-07-05 01:53:51 +03:00 committed by Miško Hevery
parent e01b539ee5
commit c0e2dba07b
3 changed files with 52 additions and 7 deletions

View File

@ -85,8 +85,8 @@
"hammerjs": "2.0.8",
"husky": "^0.14.3",
"incremental-dom": "0.4.1",
"jasmine": "2.8",
"jasmine-core": "2.8",
"jasmine": "2",
"jasmine-core": "2",
"jpm": "1.3.1",
"karma": "0.13.20",
"karma-browserstack-launcher": "0.1.9",

View File

@ -22,22 +22,50 @@ const ng1Versions = [
export function createWithEachNg1VersionFn(setNg1: typeof setAngularJSGlobal) {
return (specSuite: () => void) => ng1Versions.forEach(({label, files}) => {
describe(`[AngularJS v${label}]`, () => {
// Problem:
// As soon as `angular-mocks.js` is loaded, it runs `beforeEach` and `afterEach` to register
// setup/tear down callbacks. Jasmine 2.9+ does not allow `beforeEach`/`afterEach` to be
// nested inside a `beforeAll` call (only inside `describe`).
// Hacky work-around:
// Patch the affected jasmine methods while loading `angular-mocks.js` (inside `beforeAll`) to
// capture the registered callbacks. Also, inside the `describe` call register a callback with
// each affected method that runs all captured callbacks.
// (Note: Currently, async callbacks are not supported, but that should be OK, since
// `angular-mocks.js` does not use them.)
const methodsToPatch = ['beforeAll', 'beforeEach', 'afterEach', 'afterAll'];
const methodCallbacks = methodsToPatch.reduce<{[name: string]: any[]}>(
(aggr, method) => ({...aggr, [method]: []}), {});
const win = window as any;
function patchJasmineMethods(): () => void {
const originalMethods: {[name: string]: any} = {};
methodsToPatch.forEach(method => {
originalMethods[method] = win[method];
win[method] = (cb: any) => methodCallbacks[method].push(cb);
});
return () => methodsToPatch.forEach(method => win[method] = originalMethods[method]);
}
beforeAll(done => {
// Load AngularJS before running tests.
files
.reduce(
(prev, file) => prev.then(() => new Promise<void>((resolve, reject) => {
const restoreMethods = patchJasmineMethods();
const script = document.createElement('script');
script.src = `base/angular/node_modules/${file}`;
script.onerror = reject;
script.onload = () => {
document.body.removeChild(script);
restoreMethods();
resolve();
};
document.body.appendChild(script);
})),
Promise.resolve())
.then(() => setNg1((window as any).angular))
.then(() => setNg1(win.angular))
.then(done, done.fail);
});
@ -45,13 +73,18 @@ export function createWithEachNg1VersionFn(setNg1: typeof setAngularJSGlobal) {
// In these tests we are loading different versions of AngularJS on the same window.
// AngularJS leaves an "expandoId" property on `document`, which can trick subsequent
// `window.angular` instances into believing an app is already bootstrapped.
(window as any).angular.element(document).removeData();
win.angular.element(document).removeData();
// Remove AngularJS to leave a clean state for subsequent tests.
setNg1(undefined);
delete (window as any).angular;
delete win.angular;
});
methodsToPatch.forEach(method => win[method](function() {
// Run the captured callbacks. (Async callbacks not supported.)
methodCallbacks[method].forEach(cb => cb.call(this));
}));
specSuite();
});
});

View File

@ -3255,7 +3255,11 @@ isstream@~0.1.1, isstream@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a"
jasmine-core@2.8, jasmine-core@~2.8.0:
jasmine-core@2, jasmine-core@~2.99.0:
version "2.99.1"
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-2.99.1.tgz#e6400df1e6b56e130b61c4bcd093daa7f6e8ca15"
jasmine-core@~2.8.0:
version "2.8.0"
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-2.8.0.tgz#bcc979ae1f9fd05701e45e52e65d3a5d63f1a24e"
@ -3265,7 +3269,15 @@ jasmine-diff@^0.1.3:
dependencies:
diff "^3.2.0"
jasmine@2.8, jasmine@^2.5.3:
jasmine@2:
version "2.99.0"
resolved "https://registry.yarnpkg.com/jasmine/-/jasmine-2.99.0.tgz#8ca72d102e639b867c6489856e0e18a9c7aa42b7"
dependencies:
exit "^0.1.2"
glob "^7.0.6"
jasmine-core "~2.99.0"
jasmine@^2.5.3:
version "2.8.0"
resolved "https://registry.yarnpkg.com/jasmine/-/jasmine-2.8.0.tgz#6b089c0a11576b1f16df11b80146d91d4e8b8a3e"
dependencies: