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

global setup and global teardown now gets globalConfig as a parameter #6486

Merged
merged 3 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Fixes

- `[jest-cli]` Pass `globalConfig` as a parameter to `globalSetup` and `globalTeardown` functions ([#6486](https://github.com/facebook/jest/pull/6486))
- `[jest-config]` Add missing options to the `defaults` object ([#6428](https://github.com/facebook/jest/pull/6428))
- `[expect]` Using symbolic property names in arrays no longer causes the `toEqual` matcher to fail ([#6391](https://github.com/facebook/jest/pull/6391))
- `[expect]` `toEqual` no longer tries to compare non-enumerable symbolic properties, to be consistent with non-symbolic properties. ([#6398](https://github.com/facebook/jest/pull/6398))
Expand Down
4 changes: 2 additions & 2 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,13 @@ Note that, if you specify a global reference value (like an object or array) her

Default: `undefined`

This option allows the use of a custom global setup module which exports an async function that is triggered once before all test suites.
This option allows the use of a custom global setup module which exports an async function that is triggered once before all test suites. This function gets Jest's `globalConfig` object as a parameter.

### `globalTeardown` [string]

Default: `undefined`

This option allows the use of a custom global teardown module which exports an async function that is triggered once after all test suites.
This option allows the use of a custom global teardown module which exports an async function that is triggered once after all test suites. This function gets Jest's `globalConfig` object as a parameter.

### `moduleDirectories` [array<string>]

Expand Down
16 changes: 16 additions & 0 deletions e2e/__tests__/global_setup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,19 @@ test('jest throws an error when globalSetup does not export a function', () => {
`TypeError: globalSetup file must export a function at ${setupPath}`,
);
});

test('globalSetup function gets jest config object as a parameter', () => {
const setupPath = path.resolve(
__dirname,
'../global-setup/setup-with-config.js',
);

const testPathPattern = 'custom_tests_dir/pass';

const result = runJest('global-setup', [
`--globalSetup=${setupPath}`,
`--testPathPattern=${testPathPattern}`,
]);

expect(result.stdout).toBe(testPathPattern);
});
16 changes: 16 additions & 0 deletions e2e/__tests__/global_teardown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,19 @@ test('jest throws an error when globalTeardown does not export a function', () =
`TypeError: globalTeardown file must export a function at ${teardownPath}`,
);
});

test('globalTeardown function gets jest config object as a parameter', () => {
const teardownPath = path.resolve(
__dirname,
'../global-teardown/teardown-with-config.js',
);

const testPathPattern = 'custom_tests_dir/pass';

const result = runJest('global-teardown', [
`--globalTeardown=${teardownPath}`,
`--testPathPattern=${testPathPattern}`,
]);

expect(result.stdout).toBe(testPathPattern);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe pass through slash to fix the os inconsistencies?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the advice, I realized that I can test it without giving a path to the testPathPattern so we won't have to deal with the inconsistency in the first place.

});
1 change: 1 addition & 0 deletions e2e/global-setup/custom_tests_dir/pass.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test('should pass', () => {});
10 changes: 10 additions & 0 deletions e2e/global-setup/setup-with-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 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.
*/

module.exports = function(jestConfig) {
console.log(jestConfig.testPathPattern);
};
1 change: 1 addition & 0 deletions e2e/global-teardown/custom_tests_dir/pass.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test('should pass', () => {});
10 changes: 10 additions & 0 deletions e2e/global-teardown/teardown-with-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* 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.
*/

module.exports = function(jestConfig) {
console.log(jestConfig.testPathPattern);
};
4 changes: 2 additions & 2 deletions packages/jest-cli/src/run_jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export default (async function runJest({
);
}

await globalSetup();
await globalSetup(globalConfig);
}
const results = await new TestScheduler(
globalConfig,
Expand All @@ -301,7 +301,7 @@ export default (async function runJest({
);
}

await globalTeardown();
await globalTeardown(globalConfig);
}
return processResults(results, {
collectHandles,
Expand Down