diff --git a/packages/jest-cli/src/run_jest.js b/packages/jest-cli/src/run_jest.js index 59a28adff6db..cb930dee3b20 100644 --- a/packages/jest-cli/src/run_jest.js +++ b/packages/jest-cli/src/run_jest.js @@ -58,7 +58,13 @@ const getTestPaths = async ( } const shouldTestArray = await Promise.all( - data.tests.map(test => jestHooks.shouldRunTestSuite(test.path)), + data.tests.map(test => + jestHooks.shouldRunTestSuite({ + config: test.context.config, + duration: test.duration, + testPath: test.path, + }), + ), ); const filteredTests = data.tests.filter((test, i) => shouldTestArray[i]); diff --git a/packages/jest-watch/src/jest_hooks.js b/packages/jest-watch/src/jest_hooks.js index 0d28f60ead83..d844229fd540 100644 --- a/packages/jest-watch/src/jest_hooks.js +++ b/packages/jest-watch/src/jest_hooks.js @@ -61,11 +61,11 @@ class JestHooks { this._listeners.onTestRunComplete.forEach(listener => listener(results), ), - shouldRunTestSuite: async testPath => + shouldRunTestSuite: async testSuiteInfo => Promise.all( - this._listeners.shouldRunTestSuite.map(listener => - listener(testPath), - ), + this._listeners.shouldRunTestSuite.map(listener => { + return listener(testSuiteInfo); + }), ).then(result => result.every(shouldRunTestSuite => shouldRunTestSuite), ), diff --git a/types/JestHooks.js b/types/JestHooks.js index 3952d9518fe3..bf5626cffb41 100644 --- a/types/JestHooks.js +++ b/types/JestHooks.js @@ -10,12 +10,20 @@ import type {AggregatedResult} from './TestResult'; import type {Path, ProjectConfig} from './Config'; +type TestSuiteInfo = { + config: ProjectConfig, + duration: ?number, + testPath: string, +}; + export type JestHookExposedFS = { projects: Array<{config: ProjectConfig, testPaths: Array}>, }; export type FileChange = (fs: JestHookExposedFS) => void; -export type ShouldRunTestSuite = (testPath: string) => Promise; +export type ShouldRunTestSuite = ( + testSuiteInfo: TestSuiteInfo, +) => Promise; export type TestRunComplete = (results: AggregatedResult) => void; export type JestHookSubscriber = { @@ -27,5 +35,5 @@ export type JestHookSubscriber = { export type JestHookEmitter = { onFileChange: (fs: JestHookExposedFS) => void, onTestRunComplete: (results: AggregatedResult) => void, - shouldRunTestSuite: (testPath: string) => Promise, + shouldRunTestSuite: (testSuiteInfo: TestSuiteInfo) => Promise, }; diff --git a/website/versioned_docs/version-23.0/WatchPlugins.md b/website/versioned_docs/version-23.0/WatchPlugins.md index f05e057df2c3..48ec86f72c09 100644 --- a/website/versioned_docs/version-23.0/WatchPlugins.md +++ b/website/versioned_docs/version-23.0/WatchPlugins.md @@ -37,7 +37,7 @@ class MyWatchPlugin { Below are the hooks available in Jest. -#### `jestHooks.shouldRunTestSuite(testPath)` +#### `jestHooks.shouldRunTestSuite({ config, duration, testPath })` Returns a boolean (or `Promise`) for handling asynchronous operations) to specify if a test should be run or not. @@ -46,12 +46,12 @@ For example: ```javascript class MyWatchPlugin { apply(jestHooks) { - jestHooks.shouldRunTestSuite(testPath => { + jestHooks.shouldRunTestSuite(({testPath}) => { return testPath.includes('my-keyword'); }); // or a promise - jestHooks.shouldRunTestSuite(testPath => { + jestHooks.shouldRunTestSuite(({testPath}) => { return Promise.resolve(testPath.includes('my-keyword')); }); }