From c0e2dba07bc43272f45537299f1a097b83bafb49 Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Thu, 5 Jul 2018 01:53:51 +0300 Subject: [PATCH] build: upgrade jasmine to 2.99.x and fix tests (#19904) PR Close #19904 --- package.json | 4 +- packages/upgrade/test/common/test_helpers.ts | 39 ++++++++++++++++++-- yarn.lock | 16 +++++++- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 2d42212d1c..9c28cd9ad1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/upgrade/test/common/test_helpers.ts b/packages/upgrade/test/common/test_helpers.ts index 1f5a27e979..c0f78a6b4f 100644 --- a/packages/upgrade/test/common/test_helpers.ts +++ b/packages/upgrade/test/common/test_helpers.ts @@ -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((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(); }); }); diff --git a/yarn.lock b/yarn.lock index 66b1faeecd..10327f33f9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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: