Skip to content

Commit

Permalink
feat(jest-jasmine2): introduce legacyExecutionOrder flag to enable un…
Browse files Browse the repository at this point in the history
…documented execution order

We do not set the currentDeclarationSuite upon node start in order to keep a legacy, undocumented ordering of beforeEach execution.
Specifically, this applies to beforeEach that were added inside of tests.
Facebook depends on this behavior internally (see jestjs#5964 for discussion)

Also: Added type of CreateOptions in jasmine_light

Added legacyExecutionOrder: false to DEFAULT_PROJECT_CONFIG
  • Loading branch information
niieani committed Apr 30, 2018
1 parent 9cee675 commit 2a4733d
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 12 deletions.
1 change: 1 addition & 0 deletions TestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const DEFAULT_PROJECT_CONFIG: ProjectConfig = {
haste: {
providesModuleNodeModules: [],
},
legacyExecutionOrder: false,
moduleDirectories: [],
moduleFileExtensions: ['js'],
moduleLoader: '/test_module_loader_path',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Correct beforeEach order ensures the correct order for beforeEach 1`] = `
exports[`Correct beforeEach order ensures the correct order for beforeEach with legacyExecutionOrder flag 1`] = `
" console.log __tests__/before-each-queue.test.js:3
BeforeEach
Expand All @@ -18,3 +18,22 @@ exports[`Correct beforeEach order ensures the correct order for beforeEach 1`] =
"
`;

exports[`Correct beforeEach order ensures the correct order for beforeEach without legacyExecutionOrder flag 1`] = `
" console.log __tests__/before-each-queue.test.js:3
BeforeEach
console.log __tests__/before-each-queue.test.js:7
It Foo
console.log __tests__/before-each-queue.test.js:3
BeforeEach
console.log __tests__/before-each-queue.test.js:10
BeforeEach Inline Foo
console.log __tests__/before-each-queue.test.js:15
It Bar
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ exports[`not throwing Error objects 5`] = `
37 | });
38 |
at packages/jest-jasmine2/build/jasmine/Env.js:542:34
at packages/jest-jasmine2/build/jasmine/Env.js:558:34
at __tests__/during_tests.test.js:36:3
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ exports[`--showConfig outputs config info and exits 1`] = `
\\"haste\\": {
\\"providesModuleNodeModules\\": []
},
\\"legacyExecutionOrder\\": false,
\\"moduleDirectories\\": [
\\"node_modules\\"
],
Expand Down
9 changes: 7 additions & 2 deletions integration-tests/__tests__/before-each-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
const runJest = require('../runJest');

describe('Correct beforeEach order', () => {
it('ensures the correct order for beforeEach', () => {
const result = runJest('before-each-queue');
it('ensures the correct order for beforeEach with legacyExecutionOrder flag', () => {
const result = runJest('before-each-queue/with-legacy-execution-order');
expect(result.stdout).toMatchSnapshot();
});

it('ensures the correct order for beforeEach without legacyExecutionOrder flag', () => {
const result = runJest('before-each-queue/without-legacy-execution-order');
expect(result.stdout).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"legacyExecutionOrder": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
describe('test', () => {
beforeEach(() => {
console.log('BeforeEach');
});

it('foo', () => {
console.log('It Foo');

beforeEach(() => {
console.log('BeforeEach Inline Foo');
});
});

it('bar', () => {
console.log('It Bar');

beforeEach(() => {
console.log('BeforeEach Inline Bar');
});
});
});
1 change: 1 addition & 0 deletions packages/jest-config/src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default ({
haste: {
providesModuleNodeModules: [],
},
legacyExecutionOrder: false,
moduleDirectories: ['node_modules'],
moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
moduleNameMapper: {},
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const getConfigs = (
forceCoverageMatch: options.forceCoverageMatch,
globals: options.globals,
haste: options.haste,
legacyExecutionOrder: options.legacyExecutionOrder,
moduleDirectories: options.moduleDirectories,
moduleFileExtensions: options.moduleFileExtensions,
moduleLoader: options.moduleLoader,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ export default function normalize(options: InitialOptions, argv: Argv) {
case 'forceCoverageMatch':
case 'forceExit':
case 'lastCommit':
case 'legacyExecutionOrder':
case 'listTests':
case 'logHeapUsage':
case 'mapCoverage':
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/valid_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default ({
},
json: false,
lastCommit: false,
legacyExecutionOrder: false,
logHeapUsage: true,
moduleDirectories: ['node_modules'],
moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
Expand Down
1 change: 1 addition & 0 deletions packages/jest-jasmine2/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async function jasmine2(
);
const jasmineFactory = runtime.requireInternalModule(JASMINE);
const jasmine = jasmineFactory.create({
legacyExecutionOrder: config.legacyExecutionOrder,
process,
testPath,
});
Expand Down
8 changes: 7 additions & 1 deletion packages/jest-jasmine2/src/jasmine/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ export default function(j$) {
}
},
nodeStart(suite) {
currentDeclarationSuite = suite;
if (!j$.legacyExecutionOrder) {
// We do not set the currentDeclarationSuite upon node start
// in order to keep a legacy, undocumented ordering of beforeEach execution.
// Specifically, this applies to beforeEach that were added inside of tests.
// Facebook depends on this behavior internally (see #5964 for discussion)
currentDeclarationSuite = suite;
}
currentlyExecutingSuites.push(suite);
defaultResourcesForRunnable(
suite.id,
Expand Down
8 changes: 7 additions & 1 deletion packages/jest-jasmine2/src/jasmine/jasmine_light.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ import SpyRegistry from './spy_registry';
import Suite from './Suite';
import Timer from './Timer';

exports.create = function(createOptions: Object) {
type CreateOptions = {|
legacyExecutionOrder: boolean,
process: typeof process,
testPath: string,
|};

exports.create = function(createOptions: CreateOptions) {
const j$ = Object.assign({}, createOptions);

j$.DEFAULT_TIMEOUT_INTERVAL = 5000;
Expand Down
7 changes: 1 addition & 6 deletions packages/jest-jasmine2/src/tree_processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,11 @@ export default function treeProcessor(options: Options) {
}

function getNodeWithChildrenHandler(node: TreeNode, enabled: boolean) {
// NOTE: We create the array of queueableFns preemptively,
// in order to keep a legacy, undocumented ordering of beforeEach execution.
// Specifically, this applies to beforeEach that were added inside of tests.
// Facebook depends on this behavior internally (see #5964 for discussion)
const queueableFns = wrapChildren(node, enabled);
return async function fn(done: (error?: any) => void = () => {}) {
nodeStart(node);
await queueRunnerFactory({
onException: error => node.onException(error),
queueableFns,
queueableFns: wrapChildren(node, enabled),
userContext: node.sharedUserContext(),
});
nodeComplete(node);
Expand Down
3 changes: 3 additions & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export type DefaultOptions = {|
globals: ConfigGlobals,
globalSetup: ?string,
globalTeardown: ?string,
legacyExecutionOrder: boolean,
haste: HasteConfig,
detectLeaks: boolean,
moduleDirectories: Array<string>,
Expand Down Expand Up @@ -99,6 +100,7 @@ export type InitialOptions = {
globals?: ConfigGlobals,
globalSetup?: ?string,
globalTeardown?: ?string,
legacyExecutionOrder?: boolean,
haste?: HasteConfig,
reporters?: Array<ReporterConfig | string>,
logHeapUsage?: boolean,
Expand Down Expand Up @@ -231,6 +233,7 @@ export type ProjectConfig = {|
forceCoverageMatch: Array<Glob>,
globals: ConfigGlobals,
haste: HasteConfig,
legacyExecutionOrder: boolean,
moduleDirectories: Array<string>,
moduleFileExtensions: Array<string>,
moduleLoader: Path,
Expand Down

0 comments on commit 2a4733d

Please sign in to comment.