Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow --errorOnDeprecated to error on DEFAULT_TIMEOUT_INTERVAL #6367

Merged
merged 1 commit into from
Jun 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions e2e/__tests__/__snapshots__/error-on-deprecated.test.js.snap
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 33 additions & 0 deletions e2e/__tests__/__snapshots__/timeouts_legacy.test.js.snap
Original file line number Diff line number Diff line change
@@ -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: <<REPLACED>>
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: <<REPLACED>>
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: <<REPLACED>>
Ran all test suites.
"
`;
1 change: 1 addition & 0 deletions e2e/__tests__/error-on-deprecated.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
90 changes: 90 additions & 0 deletions e2e/__tests__/timeouts_legacy.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
12 changes: 12 additions & 0 deletions e2e/error-on-deprecated/__tests__/DEFAULT_TIMEOUT_INTERVAL.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
16 changes: 16 additions & 0 deletions packages/jest-jasmine2/src/error_on_private.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ export default function(j$) {
queueableFn: {
fn,
timeout() {
return timeout || j$.DEFAULT_TIMEOUT_INTERVAL;
return timeout || j$._DEFAULT_TIMEOUT_INTERVAL;
},
},
throwOnExpectationFailure,
Expand Down Expand Up @@ -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;
},
});
};
Expand All @@ -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;
},
});
};
Expand All @@ -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;
},
});
};
Expand All @@ -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;
},
});
};
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/jasmine/jasmine_light.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down