From b1c2c43ca5547eb22aaebc6c20fae80db2fc0245 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Tue, 29 May 2018 21:08:00 -0700 Subject: [PATCH] Allow errorOnDeprecated to error on DEFAULT_TIMEOUT_INTERVAL --- .../error-on-deprecated.test.js.snap | 21 +++++ .../timeouts_legacy.test.js.snap | 33 +++++++ e2e/__tests__/error-on-deprecated.test.js | 1 + e2e/__tests__/timeouts_legacy.test.js | 90 +++++++++++++++++++ .../DEFAULT_TIMEOUT_INTERVAL.test.js | 12 +++ .../jest-jasmine2/src/error_on_private.js | 16 ++++ packages/jest-jasmine2/src/index.js | 12 +++ packages/jest-jasmine2/src/jasmine/Env.js | 10 +-- .../src/jasmine/jasmine_light.js | 2 +- packages/jest-runtime/src/index.js | 2 +- 10 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 e2e/__tests__/__snapshots__/timeouts_legacy.test.js.snap create mode 100644 e2e/__tests__/timeouts_legacy.test.js create mode 100644 e2e/error-on-deprecated/__tests__/DEFAULT_TIMEOUT_INTERVAL.test.js diff --git a/e2e/__tests__/__snapshots__/error-on-deprecated.test.js.snap b/e2e/__tests__/__snapshots__/error-on-deprecated.test.js.snap index c5542e8b0c26..160e8a7c2a2f 100644 --- a/e2e/__tests__/__snapshots__/error-on-deprecated.test.js.snap +++ b/e2e/__tests__/__snapshots__/error-on-deprecated.test.js.snap @@ -1,5 +1,26 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`DEFAULT_TIMEOUT_INTERVAL.test.js errors in errorOnDeprecated mode 1`] = ` +"FAIL __tests__/DEFAULT_TIMEOUT_INTERVAL.test.js + ✕ DEFAULT_TIMEOUT_INTERVAL + + ● DEFAULT_TIMEOUT_INTERVAL + + Illegal usage of \`jasmine.DEFAULT_TIMEOUT_INTERVAL\`, prefer \`jest.setTimeout\`. + + 8 | + 9 | test('DEFAULT_TIMEOUT_INTERVAL', () => { + > 10 | jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000; + | ^ + 11 | expect(true).toBe(true); + 12 | }); + 13 | + + at __tests__/DEFAULT_TIMEOUT_INTERVAL.test.js:10:3 + +" +`; + exports[`fail.test.js errors in errorOnDeprecated mode 1`] = ` "FAIL __tests__/fail.test.js ✕ fail diff --git a/e2e/__tests__/__snapshots__/timeouts_legacy.test.js.snap b/e2e/__tests__/__snapshots__/timeouts_legacy.test.js.snap new file mode 100644 index 000000000000..93626023f7a5 --- /dev/null +++ b/e2e/__tests__/__snapshots__/timeouts_legacy.test.js.snap @@ -0,0 +1,33 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`can read and write jasmine.DEFAULT_TIMEOUT_INTERVAL 1`] = ` +"Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites." +`; + +exports[`does not exceed the timeout using jasmine.DEFAULT_TIMEOUT_INTERVAL 1`] = ` +"PASS __tests__/a-banana.js + ✓ banana + +" +`; + +exports[`does not exceed the timeout using jasmine.DEFAULT_TIMEOUT_INTERVAL 2`] = ` +"Test Suites: 1 passed, 1 total +Tests: 1 passed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites." +`; + +exports[`exceeds the timeout set using jasmine.DEFAULT_TIMEOUT_INTERVAL 1`] = ` +"Test Suites: 1 failed, 1 total +Tests: 1 failed, 1 total +Snapshots: 0 total +Time: <> +Ran all test suites. +" +`; diff --git a/e2e/__tests__/error-on-deprecated.test.js b/e2e/__tests__/error-on-deprecated.test.js index 46e33ea3cc6b..a3ea5f318916 100644 --- a/e2e/__tests__/error-on-deprecated.test.js +++ b/e2e/__tests__/error-on-deprecated.test.js @@ -26,6 +26,7 @@ const testFiles = [ 'pending.test.js', 'spyOn.test.js', 'spyOnProperty.test.js', + 'DEFAULT_TIMEOUT_INTERVAL.test.js', ]; const SHOULD_NOT_PASS_IN_JEST = new Set([ diff --git a/e2e/__tests__/timeouts_legacy.test.js b/e2e/__tests__/timeouts_legacy.test.js new file mode 100644 index 000000000000..101d1f8f7e66 --- /dev/null +++ b/e2e/__tests__/timeouts_legacy.test.js @@ -0,0 +1,90 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * @flow + */ + +'use strict'; + +const path = require('path'); +const {extractSummary, cleanup, writeFiles} = require('../Utils'); +const runJest = require('../runJest'); +const ConditionalTest = require('../../scripts/ConditionalTest'); + +/** + * NOTE: This test should be removed once jest-circus is rolled out as a breaking change. + */ + +const DIR = path.resolve(__dirname, '../timeouts-legacy'); + +ConditionalTest.skipSuiteOnJestCircus(); + +beforeEach(() => cleanup(DIR)); +afterAll(() => cleanup(DIR)); + +test('exceeds the timeout set using jasmine.DEFAULT_TIMEOUT_INTERVAL', () => { + writeFiles(DIR, { + '__tests__/a-banana.js': ` + jasmine.DEFAULT_TIMEOUT_INTERVAL = 20; + + test('banana', () => { + return new Promise(resolve => { + setTimeout(resolve, 100); + }); + }); + `, + 'package.json': '{}', + }); + + const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false']); + const {rest, summary} = extractSummary(stderr); + expect(rest).toMatch( + /(jest\.setTimeout|jasmine\.DEFAULT_TIMEOUT_INTERVAL|Exceeded timeout)/, + ); + expect(summary).toMatchSnapshot(); + expect(status).toBe(1); +}); + +test('does not exceed the timeout using jasmine.DEFAULT_TIMEOUT_INTERVAL', () => { + writeFiles(DIR, { + '__tests__/a-banana.js': ` + jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000; + + test('banana', () => { + return new Promise(resolve => { + setTimeout(resolve, 20); + }); + }); + `, + 'package.json': '{}', + }); + + const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false']); + const {rest, summary} = extractSummary(stderr); + expect(rest).toMatchSnapshot(); + expect(summary).toMatchSnapshot(); + expect(status).toBe(0); +}); + +test('can read and write jasmine.DEFAULT_TIMEOUT_INTERVAL', () => { + writeFiles(DIR, { + '__tests__/a-banana.js': ` + const timeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; + jasmine.DEFAULT_TIMEOUT_INTERVAL = 154; + const newTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; + + test('banana', () => { + expect(timeout).toBe(5000); + expect(newTimeout).toBe(154); + }); + `, + 'package.json': '{}', + }); + + const {stderr, status} = runJest(DIR, ['-w=1', '--ci=false']); + const {summary} = extractSummary(stderr); + expect(summary).toMatchSnapshot(); + expect(status).toBe(0); +}); diff --git a/e2e/error-on-deprecated/__tests__/DEFAULT_TIMEOUT_INTERVAL.test.js b/e2e/error-on-deprecated/__tests__/DEFAULT_TIMEOUT_INTERVAL.test.js new file mode 100644 index 000000000000..3911f19eed98 --- /dev/null +++ b/e2e/error-on-deprecated/__tests__/DEFAULT_TIMEOUT_INTERVAL.test.js @@ -0,0 +1,12 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ +'use strict'; + +test('DEFAULT_TIMEOUT_INTERVAL', () => { + jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000; + expect(true).toBe(true); +}); diff --git a/packages/jest-jasmine2/src/error_on_private.js b/packages/jest-jasmine2/src/error_on_private.js index 5b3469a5a8b6..eef265001822 100644 --- a/packages/jest-jasmine2/src/error_on_private.js +++ b/packages/jest-jasmine2/src/error_on_private.js @@ -41,6 +41,22 @@ export function installErrorOnPrivate(global: Global): void { throwAtFunction(disabledJasmineMethods[methodName], jasmine[methodName]); }; }); + + function set() { + throwAtFunction( + 'Illegal usage of `jasmine.DEFAULT_TIMEOUT_INTERVAL`, prefer `jest.setTimeout`.', + set, + ); + } + + const original = jasmine.DEFAULT_TIMEOUT_INTERVAL; + // $FlowFixMe Flow seems to be confused about accessors and tries to enfoce having a `value` property. + Object.defineProperty(jasmine, 'DEFAULT_TIMEOUT_INTERVAL', { + configurable: true, + enumerable: true, + get: () => original, + set, + }); } function throwAtFunction(message, fn) { diff --git a/packages/jest-jasmine2/src/index.js b/packages/jest-jasmine2/src/index.js index 5f13c1f92154..ca4e8f824b6b 100644 --- a/packages/jest-jasmine2/src/index.js +++ b/packages/jest-jasmine2/src/index.js @@ -103,6 +103,18 @@ async function jasmine2( if (globalConfig.errorOnDeprecated) { installErrorOnPrivate(environment.global); + } else { + // $FlowFixMe Flow seems to be confused about accessors and tries to enfoce having a `value` property. + Object.defineProperty(jasmine, 'DEFAULT_TIMEOUT_INTERVAL', { + configurable: true, + enumerable: true, + get() { + return this._DEFAULT_TIMEOUT_INTERVAL; + }, + set(value) { + this._DEFAULT_TIMEOUT_INTERVAL = value; + }, + }); } const snapshotState: SnapshotState = runtime diff --git a/packages/jest-jasmine2/src/jasmine/Env.js b/packages/jest-jasmine2/src/jasmine/Env.js index f8d6cf62028c..d547c0a8f271 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.js +++ b/packages/jest-jasmine2/src/jasmine/Env.js @@ -417,7 +417,7 @@ export default function(j$) { queueableFn: { fn, timeout() { - return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; + return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; }, }, throwOnExpectationFailure, @@ -506,7 +506,7 @@ export default function(j$) { currentDeclarationSuite.beforeEach({ fn: beforeEachFunction, timeout() { - return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; + return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; }, }); }; @@ -515,7 +515,7 @@ export default function(j$) { currentDeclarationSuite.beforeAll({ fn: beforeAllFunction, timeout() { - return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; + return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; }, }); }; @@ -524,7 +524,7 @@ export default function(j$) { currentDeclarationSuite.afterEach({ fn: afterEachFunction, timeout() { - return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; + return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; }, }); }; @@ -533,7 +533,7 @@ export default function(j$) { currentDeclarationSuite.afterAll({ fn: afterAllFunction, timeout() { - return timeout || j$.DEFAULT_TIMEOUT_INTERVAL; + return timeout || j$._DEFAULT_TIMEOUT_INTERVAL; }, }); }; diff --git a/packages/jest-jasmine2/src/jasmine/jasmine_light.js b/packages/jest-jasmine2/src/jasmine/jasmine_light.js index b0113aa03913..ab1106bab399 100644 --- a/packages/jest-jasmine2/src/jasmine/jasmine_light.js +++ b/packages/jest-jasmine2/src/jasmine/jasmine_light.js @@ -45,7 +45,7 @@ import Timer from './Timer'; exports.create = function(createOptions: Object) { const j$ = Object.assign({}, createOptions); - j$.DEFAULT_TIMEOUT_INTERVAL = 5000; + j$._DEFAULT_TIMEOUT_INTERVAL = 5000; j$.getEnv = function(options: Object) { const env = (j$.currentEnv_ = j$.currentEnv_ || new j$.Env(options)); diff --git a/packages/jest-runtime/src/index.js b/packages/jest-runtime/src/index.js index 0193926caca9..13594738cf88 100644 --- a/packages/jest-runtime/src/index.js +++ b/packages/jest-runtime/src/index.js @@ -822,7 +822,7 @@ class Runtime { const setTimeout = (timeout: number) => { this._environment.global.jasmine - ? (this._environment.global.jasmine.DEFAULT_TIMEOUT_INTERVAL = timeout) + ? (this._environment.global.jasmine._DEFAULT_TIMEOUT_INTERVAL = timeout) : (this._environment.global[ Symbol.for('TEST_TIMEOUT_SYMBOL') ] = timeout);