Skip to content

Commit

Permalink
Add projectConfig argument to shouldRunTestSuite hook (#6350)
Browse files Browse the repository at this point in the history
* Add projectConfig argument to shouldRunTestSuite hook

* Use object for shouldRunTestSuite api
  • Loading branch information
rogeliog authored and cpojer committed May 30, 2018
1 parent 049bac4 commit 126858b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
8 changes: 7 additions & 1 deletion packages/jest-cli/src/run_jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-watch/src/jest_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
Expand Down
12 changes: 10 additions & 2 deletions types/JestHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>}>,
};

export type FileChange = (fs: JestHookExposedFS) => void;
export type ShouldRunTestSuite = (testPath: string) => Promise<boolean>;
export type ShouldRunTestSuite = (
testSuiteInfo: TestSuiteInfo,
) => Promise<boolean>;
export type TestRunComplete = (results: AggregatedResult) => void;

export type JestHookSubscriber = {
Expand All @@ -27,5 +35,5 @@ export type JestHookSubscriber = {
export type JestHookEmitter = {
onFileChange: (fs: JestHookExposedFS) => void,
onTestRunComplete: (results: AggregatedResult) => void,
shouldRunTestSuite: (testPath: string) => Promise<boolean>,
shouldRunTestSuite: (testSuiteInfo: TestSuiteInfo) => Promise<boolean>,
};
6 changes: 3 additions & 3 deletions website/versioned_docs/version-23.0/WatchPlugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>`) for handling asynchronous operations) to specify if a test should be run or not.

Expand All @@ -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'));
});
}
Expand Down

0 comments on commit 126858b

Please sign in to comment.