Skip to content

Commit 15e63b2

Browse files
ranyitzcpojer
authored andcommitted
global setup and global teardown now gets globalConfig as a parameter (#6486)
* feat: global setup and global teardown now gets globalConfig as a parameter * update changelog * remove full path from testPathPattern
1 parent 49d7bbf commit 15e63b2

File tree

9 files changed

+59
-4
lines changed

9 files changed

+59
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Fixes
44

5+
- `[jest-cli]` Pass `globalConfig` as a parameter to `globalSetup` and `globalTeardown` functions ([#6486](https://github.com/facebook/jest/pull/6486))
56
- `[jest-config]` Add missing options to the `defaults` object ([#6428](https://github.com/facebook/jest/pull/6428))
67
- `[expect]` Using symbolic property names in arrays no longer causes the `toEqual` matcher to fail ([#6391](https://github.com/facebook/jest/pull/6391))
78
- `[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))

docs/Configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,13 +303,13 @@ Note that, if you specify a global reference value (like an object or array) her
303303

304304
Default: `undefined`
305305

306-
This option allows the use of a custom global setup module which exports an async function that is triggered once before all test suites.
306+
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.
307307

308308
### `globalTeardown` [string]
309309

310310
Default: `undefined`
311311

312-
This option allows the use of a custom global teardown module which exports an async function that is triggered once after all test suites.
312+
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.
313313

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

e2e/__tests__/global_setup.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,19 @@ test('jest throws an error when globalSetup does not export a function', () => {
3939
`TypeError: globalSetup file must export a function at ${setupPath}`,
4040
);
4141
});
42+
43+
test('globalSetup function gets jest config object as a parameter', () => {
44+
const setupPath = path.resolve(
45+
__dirname,
46+
'../global-setup/setup-with-config.js',
47+
);
48+
49+
const testPathPattern = 'pass';
50+
51+
const result = runJest('global-setup', [
52+
`--globalSetup=${setupPath}`,
53+
`--testPathPattern=${testPathPattern}`,
54+
]);
55+
56+
expect(result.stdout).toBe(testPathPattern);
57+
});

e2e/__tests__/global_teardown.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,19 @@ test('jest throws an error when globalTeardown does not export a function', () =
4949
`TypeError: globalTeardown file must export a function at ${teardownPath}`,
5050
);
5151
});
52+
53+
test('globalTeardown function gets jest config object as a parameter', () => {
54+
const teardownPath = path.resolve(
55+
__dirname,
56+
'../global-teardown/teardown-with-config.js',
57+
);
58+
59+
const testPathPattern = 'pass';
60+
61+
const result = runJest('global-teardown', [
62+
`--globalTeardown=${teardownPath}`,
63+
`--testPathPattern=${testPathPattern}`,
64+
]);
65+
66+
expect(result.stdout).toBe(testPathPattern);
67+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test('should pass', () => {});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = function(jestConfig) {
9+
console.log(jestConfig.testPathPattern);
10+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test('should pass', () => {});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
module.exports = function(jestConfig) {
9+
console.log(jestConfig.testPathPattern);
10+
};

packages/jest-cli/src/run_jest.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ export default (async function runJest({
278278
);
279279
}
280280

281-
await globalSetup();
281+
await globalSetup(globalConfig);
282282
}
283283
const results = await new TestScheduler(
284284
globalConfig,
@@ -301,7 +301,7 @@ export default (async function runJest({
301301
);
302302
}
303303

304-
await globalTeardown();
304+
await globalTeardown(globalConfig);
305305
}
306306
return processResults(results, {
307307
collectHandles,

0 commit comments

Comments
 (0)