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

Add projectConfig argument to shouldRunTestSuite hook #6350

Merged
merged 2 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(testPath vs path) and (config vs projectConfig):

I like testPath and config better, but I'm fine with anything

}),
),
);

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