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

Allow array of regexp strings for testRegex #7209

Merged
merged 6 commits into from
Oct 20, 2018

Conversation

jamietre
Copy link
Contributor

Summary

Resolves #7180
Depends on #7207

Creating dynamic configurations to include or exclude specific things is difficult because of inconsistency in how config allows specifying inclusions and exclusions.

  • testRegex allows only a single regexp
  • testMatch allows an array of globs
  • testPathIgnorePatterns allows an array of regexps

So there's no format (regex or glob) that can be used to build an array that works both for including and excluding.

This PR allows testRegex to accept an array of regex strings as well as a string, resolving this problem.

Because this is the first configuration option that's overloaded with more than one valid type, jest-validation has also been enhanced with syntax that allows specifying more than one valid example in configuration.

Test plan

  • existing unit tests pass (except as expected where internal config changed types)
  • added unit tests to cover valid and error conditions

@jamietre
Copy link
Contributor Author

The CI failure appears to be unrelated or random.

@SimenB
Copy link
Member

SimenB commented Oct 18, 2018

Can you rebase? 🙂 Just doing an interactive one and removing the first commit should be enough, right?

@jamietre jamietre force-pushed the jamiet/multiple-test-regexes-2 branch from fb5b31d to c2c23f0 Compare October 18, 2018 18:21
@jamietre
Copy link
Contributor Author

Rebased - I also had a type error in jest-validate which is fixed here, hope that's ok to do here. Not sure why that didn't cause a flow type failure in the last PR, the unit tests have a type that's not compatible with the incorrect type I used on MultipleValidOptions?? (I use TypeScript most of the time so I'm much less familiar with the nuances of flow..)

.vscode/settings.json Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
if (config.testRegex && filename.match(config.testRegex)) {
if (
config.testRegex &&
config.testRegex.some(regex => filename.match(regex))
Copy link
Member

Choose a reason for hiding this comment

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

I know you didn't introduce it, but can you use test instead?

Copy link
Contributor Author

@jamietre jamietre Oct 18, 2018

Choose a reason for hiding this comment

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

To do this efficiently, it was necessary to change the internal type in ProjectConfig to an array of RegExp instead of string (rather than parse the strings into RegExp on every use here). This makes more sense anyway, but it touched a few things.

@SimenB SimenB requested a review from thymikee October 18, 2018 18:53
.vscode/settings.json Outdated Show resolved Hide resolved
@@ -34,6 +34,6 @@ export function validationCondition(option: any, validOption: any): boolean {
return getValues(validOption).some(e => validationConditionSingle(option, e));
}

export function MultipleValidOptions(...args: Array<any>) {
Copy link
Member

Choose a reason for hiding this comment

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

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. Failed rebase.

@@ -1,5 +1,7 @@
{
"editor.rulers": [80],
"editor.rulers": [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the most annoying thing.... so when I fixed this, then saved, of course it incorrectly reformatted the fixed file itself!! I keel you vscode

Copy link
Collaborator

Choose a reason for hiding this comment

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

git checkout master .vscode/settings.json

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

These are a couple of fantastic pull requests, thank you so much!

I'd like another set of eyes before merging, but this LGTM 🙂

@thymikee
Copy link
Collaborator

I'll have one more look tomorrow morning

Copy link
Collaborator

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

Looks good! I left 2 small things to address and we can ship it :)

packages/jest-config/src/normalize.js Outdated Show resolved Hide resolved

it('when more than one testRegex is provided and filename is not a test file', () => {
testShouldInstrument('source_file.js', defaultOptions, {
testRegex: [/.*\_(test)\.(js)$/, /.*\.(test)\.(js)$/, /never/],
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see that we only test happy path in this test file. It would be great to add at least two smoke tests (1 for testRegex, 1 for testMatch) when the file should not be instrumented (e.g. add 4th expected argument to testShouldInstrument with default value of true).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are already two describe sections in this file, the 2nd -- "should return false" -- tests for scenarios that don't match, the unhappy path. I did add a test there already for testRegex.

I do see that the description of the spec was incorrect, I have fixed that, as well as a couple other existing ones that were also incorrect. Maybe this was throwing you off - either way if there's still some additional coverage you'd like to see let me know.

I have also added a test of error condition to normalize.test.js.

Copy link
Collaborator

@thymikee thymikee Oct 19, 2018

Choose a reason for hiding this comment

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

Oh my, I totally missed second testShouldInstrument. Should be renamed to testShouldNotInstrument 😛

@jamietre jamietre force-pushed the jamiet/multiple-test-regexes-2 branch from a2f8e71 to e25d89c Compare October 19, 2018 13:54
@@ -100,7 +101,10 @@ export default ({
testMatch: ['**/__tests__/**/*.js?(x)', '**/?(*.)+(spec|test).js?(x)'],
testNamePattern: 'test signature',
testPathIgnorePatterns: [NODE_MODULES_REGEXP],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$',
testRegex: MultipleValidOptions(
Copy link
Collaborator

Choose a reason for hiding this comment

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

also, what do you think about naming this just multiple for simplicity? I'm good with both, so pick whatever you like :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't feel strongly, but I guess I'd lean towards leaving it more verbose to provide clear context, since Mutliple could potentially mean more than one thing.

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow configuring using an array of testRegex
4 participants