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

Test glob support #2359

Merged
merged 7 commits into from
Feb 1, 2017
Merged

Test glob support #2359

merged 7 commits into from
Feb 1, 2017

Conversation

pugnascotia
Copy link
Contributor

Implement a new testGlob configuration option. This allows test files to be matched using extended glob patterns instead of regular expressions, as implemented by micromatch.

Per #926, globs are a frequently-used tool for matching files, and can be easier to use for filename matching. Other popular tools also use globs instead of regexs.

testRegex is still supported in this PR, but testGlob becomes the favoured option.

Existing tests have been modified where necessary to make them pass. New test cases have been added to SearchSource-test.js to check that glob matching works in addition to regex matching. A test has also been added to normalize-test.js to check that a deprecation warning is printed to the console.

@codecov-io
Copy link

codecov-io commented Dec 19, 2016

Codecov Report

Merging #2359 into master will increase coverage by 0.09%.

@@            Coverage Diff            @@
##           master   #2359      +/-   ##
=========================================
+ Coverage    67.9%     68%   +0.09%     
=========================================
  Files         140     140              
  Lines        5057    5072      +15     
=========================================
+ Hits         3434    3449      +15     
  Misses       1623    1623
Impacted Files Coverage Δ
packages/jest-editor-support/src/Settings.js 0% <ø> (ø)
packages/jest-config/src/validConfig.js 100% <ø> (ø)
packages/jest-runtime/src/transform.js 87.87% <ø> (ø)
packages/jest-runtime/src/index.js 86% <ø> (-0.06%)
packages/jest-config/src/defaults.js 100% <ø> (ø)
packages/jest-matchers/src/toThrowMatchers.js 78.84% <ø> (ø)
packages/jest-validate/src/validate.js 100% <100%> (ø)
packages/jest-cli/src/SearchSource.js 61.62% <100%> (+4.48%)
packages/jest-config/src/normalize.js 86.01% <100%> (+0.4%)
packages/jest-util/src/messages.js 73.33% <100%> (+0.36%)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 852ac99...0dddf39. Read the comment docs.

// micromatch doesn't support '..' through the globstar ('**') to avoid
// infinite recursion.

it('supports ../ paths and unix separators via textRegex', () => {
Copy link

Choose a reason for hiding this comment

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

testRegex is not used in this test at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, this used to rely on the default, so I've changed it to explicitly set a regex.

@develar
Copy link
Contributor

develar commented Jan 5, 2017

Is it going to be merged?

@pugnascotia
Copy link
Contributor Author

The CI job is failing and I don't understand why - anyone have any suggestions?

@cpojer
Copy link
Member

cpojer commented Jan 6, 2017

There is some issue with eslint on travis, I'll figure it out. @pugnascotia thanks for working on this, it'll take me a bit longer to get to this PR as there are currently 20+ open requests and I'm still catching up with work at FB after my vacation.

@pugnascotia
Copy link
Contributor Author

No worries, just pleased to help out.

@pugnascotia
Copy link
Contributor Author

Any luck figuring out what was up with CI? Do you want me to merge up to master again?

@cpojer
Copy link
Member

cpojer commented Jan 16, 2017

Do you mind rebasing this?

@pugnascotia pugnascotia force-pushed the test-glob-support branch 2 times, most recently from af14c3d to f02cc5b Compare January 16, 2017 12:33
@pugnascotia
Copy link
Contributor Author

I've rebased and squashed, and npm test passes, though the PR seems stuck on thinking there are still conflicts.

@pugnascotia pugnascotia changed the title WIP - Test glob support Test glob support Jan 16, 2017
@cpojer
Copy link
Member

cpojer commented Jan 16, 2017

Are you sure you rebased against Jest's master branch? It doesn't seem like it.

@pugnascotia
Copy link
Contributor Author

What...I'm sure I fetched before rebasing. Weird. OK, should be there now.

@thymikee
Copy link
Collaborator

@pugnascotia maybe you fetched from your fork's master, happens to me sometimes 😄

@pugnascotia pugnascotia force-pushed the test-glob-support branch 2 times, most recently from 5891d3e to 1b88af0 Compare January 18, 2017 13:25
@jest-bot
Copy link
Contributor

jest-bot commented Jan 18, 2017

Warnings
⚠️ Changes were made to package.json, but not to yarn.lock - Perhaps you need to run `yarn install`?

Generated by 🚫 dangerJS

@pugnascotia
Copy link
Contributor Author

I've rebased (again) onto master, and this time I've actually fixed the problems that arose. In particular, in commit b4f65e1 I altered the order in which config keys are validated, so that keys that are still in force but are deprecated will trigger warnings.

Re: the package.json change, there isn't a dep change so it's right that yarn.lock hasn't been updated.

import type {AssertionResult, TestResult} from 'types/TestResult';

const chalk = require('chalk');
const mm = require('micromatch');
Copy link
Member

Choose a reason for hiding this comment

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

can we call this variable micromatch everywhere please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep.

@@ -23,6 +23,11 @@ const shouldInstrument = (filename: Path, config: Config): boolean => {
return false;
}

if (config.testGlob && config.testGlob.length
&& micromatch.any(filename, config.testGlob)) {
Copy link
Member

Choose a reason for hiding this comment

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

the code style in Jest is:

if (
  config.testGlob &&
  config.testGlob.length &&
  micromatch.any(filename, config.testGlob)
) {

@@ -116,7 +114,6 @@ class Runtime {
this._mocksPattern =
config.mocksPattern ? new RegExp(config.mocksPattern) : null;
this._shouldAutoMock = config.automock;
this._testRegex = new RegExp(replacePathSepForRegex(config.testRegex));
Copy link
Member

Choose a reason for hiding this comment

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

Nice, this seems to have been dead code.

testGlob: [
'**/__tests__/**/*.js?(x)',
'**/?(*.)(spec|test).js?(x)',
],
testRegex: '(/__tests__/.*|\\.(test|spec))\\.jsx?$',
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need both?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Um...we probably don't :-)

if (config.testRegex && (!config.testGlob || config.testGlob.length === 0)) {
// Prevent default glob conflicting with any explicit `testRegex` value
newConfig.testGlob = [];
}
Copy link
Member

Choose a reason for hiding this comment

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

Seems like we should warn if both are given?

Copy link
Member

Choose a reason for hiding this comment

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

Wait, I don't really understand this. Why are you setting testGlob to an empty array if testGlob is empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, that's little messy, I'll tidy that up.

.gitignore Outdated
@@ -2,6 +2,8 @@
.eslintcache
*.swp
*~
.idea
*.iml
Copy link
Member

Choose a reason for hiding this comment

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

do you mind removing these here and instead moving these into a global gitignore config for your computer?


case 'testRegex':
value = config[key];
break;
Copy link
Member

Choose a reason for hiding this comment

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

why did you add this as a separate option in this switch case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gah, that's a leftover from an earlier implementation, when the deprecation warnings were generated here. Good catch.

@cpojer
Copy link
Member

cpojer commented Jan 21, 2017

Finally had some time to look at this. Am I understanding this correctly – it'll support both testGlob and testRegex and have the following behaviors:

  • Use both testGlob and testRegex if they are specified.
  • Print a deprecation warning for testRegex.

?

@pugnascotia
Copy link
Contributor Author

That was my thinking, yes. Since we can't (AFAIK) turn an arbitrary regex into a glob, I felt it was best to allow testRegex but print a warning, them remove it entirely in a later release.

@pugnascotia pugnascotia force-pushed the test-glob-support branch 2 times, most recently from 1cb273a to 1b0f158 Compare January 23, 2017 10:28
@pugnascotia
Copy link
Contributor Author

pugnascotia commented Jan 23, 2017

That's the branch rebased onto facebook/master again.

@cpojer
Copy link
Member

cpojer commented Jan 23, 2017

I feel like we shouldn't show the deprecation warning for testRegex yet. Instead, I'd recommend this:

  • warn/throw if both testGlob and testRegex are given.
  • don't warn about use of testRegex. We'll visibly deprecate it in two majors from now to reduce the churn.

Are there any better names than "testGlob"? Any way we can drop the "glob" from it? I don't like it, especially as we'll be ending up with a singular option in the future :)

@thymikee
Copy link
Collaborator

thymikee commented Jan 24, 2017

maybe testPattern? But this may be misleading, as we use the word pattern in watch mode and it's for regex.

edit:
or testMatch

@pugnascotia
Copy link
Contributor Author

Rebased again and updated as per your comments. I liked testMatch so I went with that.

perl -p -i -e 's/testGlob/testMatch/g' $(git grep -l testGlob)

The existing validation in jest-validate did not allow for fields
that will be replaced but still have an effect, owing to the order
in which the validation is carried out. Swap the order so that
the deprecation check comes first.
@pugnascotia
Copy link
Contributor Author

Rebased onto master again - any more feedback on this?

@cpojer cpojer merged commit 2464593 into jestjs:master Feb 1, 2017
@cpojer
Copy link
Member

cpojer commented Feb 1, 2017

Thank you for working on this and for being so thorough. Sorry for the delay in reviews. This will be in Jest 19.

skovhus pushed a commit to skovhus/jest that referenced this pull request Apr 29, 2017
* Validate for deprecated fields first

The existing validation in jest-validate did not allow for fields
that will be replaced but still have an effect, owing to the order
in which the validation is carried out. Swap the order so that
the deprecation check comes first.

* Implement testGlob option

* Fixes from code review

* Rename testGlob to testMatch, and remove testMatch deprecation

* Forbid testMatch and testRegex being used together

* Update SearchSource.js

* Update messages.js
tushardhole pushed a commit to tushardhole/jest that referenced this pull request Aug 21, 2017
* Validate for deprecated fields first

The existing validation in jest-validate did not allow for fields
that will be replaced but still have an effect, owing to the order
in which the validation is carried out. Swap the order so that
the deprecation check comes first.

* Implement testGlob option

* Fixes from code review

* Rename testGlob to testMatch, and remove testMatch deprecation

* Forbid testMatch and testRegex being used together

* Update SearchSource.js

* Update messages.js
@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 14, 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.

8 participants