Skip to content

Commit

Permalink
global setup and global teardown now gets globalConfig as a parameter (
Browse files Browse the repository at this point in the history
…#6486)

* feat: global setup and global teardown now gets globalConfig as a parameter

* update changelog

* remove full path from testPathPattern
  • Loading branch information
ranyitz authored and cpojer committed Jun 20, 2018
1 parent 49d7bbf commit 15e63b2
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 4 deletions.
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 = '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 = 'pass';

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

expect(result.stdout).toBe(testPathPattern);
});
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

0 comments on commit 15e63b2

Please sign in to comment.