-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Only run tests with title matching expression #476
Comments
This aims to resolve issue avajs#476. --grep can be passed to ava which means to skip every test that doesn't match the supplied pattern search for the test title. The pattern can contain regexp strings and the skipped flag will be set based on the output of "test" (so anywhere in the title of the test will work).
This aims to resolve issue avajs#476. --grep can be passed to ava which means to skip every test that doesn't match the supplied pattern search for the test title. The pattern can contain regexp strings and the skipped flag will be set based on the output of "test" (so anywhere in the title of the test will work).
I agree we should have something like this, but I'd like to discuss how we can innovate on this instead of just copying the Mocha option. I also have a feeling if we add this, people will follow up and demand Maybe we could use globs instead?
Regex might work well too. Just putting it out there. |
I mean to be honest for my use case all that's needed is just to search for a string literal in the titles. I guess I just wanted to make sure it's versatile for different use cases, although globs would be more intuitive (and match the globs in the file args). |
I personally think globs make more sense. Basic string literal searches only get us so far; however, constructing a valid |
Can you think of any use-cases where a glob might not work? |
And to be clear, |
Off the top of my head, no; however, I can think of many times where mocha's grep was too limited to the point where I don't even use it so pretty much anything (especially globs) would be a vast improvement. |
The only other thing I'd suggest is to change $ ava --glob='fo*' --glob='!bar' |
Would be awesome if you could provide some examples. That would help make a decision. |
This aims to resolve issue avajs#476. --match can be passed to ava which means to only run tests with a title matching the provided match string. The skipped metadata flag will be added to each test not matching the match string.
This aims to resolve issue avajs#476. --grep can be passed to ava which means to skip every test that doesn't match the supplied pattern search for the test title. The pattern can contain regexp strings and the skipped flag will be set based on the output of "test" (so anywhere in the title of the test will work).
For now, my PR is just going to do --match just because RegEx/glob matching is fairly expensive when done on large test suites. Since titles are pretty easy to change I think that should cover most if not all use cases. |
@matthewbauer There's no "for now". Anything we add we'll have to support for a long time, and as I originally said, not interested in more than one flag for matching tests. I think we should figure out how to do it correctly. Not just a quick fix :)
No. The regex and globbing are still just matching against strings. You'd have to have hundreds of thousands of tests for it to matter at all. |
Sorry, I don't have any off the top of my head. I only mocha pretty infrequently and when I do I tend to use |
So here are the use cases for this flag as I see them:
I'd be interested to see if there are any other possible use cases. Additionally, I'd like to make it versatile enough so that it's not necessarily dependent on the CLI flag so it could work in a web context. |
The main reason I'm hesitant with globs is that at least for me they only make since when you're dealing with file paths. I've certainly seen them used elsewhere but they can lead to some issues if you aren't aware of the file path association. For instance: test("test group 1: 4 - 2 = 2", function(t){})
test("test group 1: 2 + 2 = 4", function(t){})
test("test group 1: 4 / 2 = 2", function(t){}) You might expect for |
Yup, agreed. That is the main PITA with globs for sure. Only other flexible option is That being said, I concede that I have less skin in the game since I've gotten by without this feature entirely using |
|
I'm seeing what @matthewbauer is seeing unfortunately: var multimatch = require('multimatch')
var matches = multimatch([
'test group 1: 4 - 2 = 2',
'test group 1: 2 + 2 = 4',
'test group 1: 4 / 2 = 2'
], ['test group 1:**'])
// => [ 'test group 1: 4 - 2 = 2', 'test group 1: 2 + 2 = 4' ] |
Scratch that. I think we should just accept that having a Maybe we should combine the test name with the file path: // tests/foo.js
test('bar', t => {...})
// grep matches against `tests/foo/bar` If we ever do nested groups, we could combine them with slashes: // tests/foo.js
group('bar', test => {
test('baz', t => {...});
// tests/foo/bar/baz
}); |
I wasn't really considering multiple test files at this point. If you only wanted it to match |
Yeah - that's what I was thinking. Back to the core question though: Should we do this? The only time I've used this feature in mocha is by tagging a few tests as "slow" and avoiding them while in watch mode and doing TDD (preferring fast feedback over completeness). |
I'm thinking there should be some way to selectively run tests on based from the CLI. For me, travis times out if I run the whole test file. An alternative would be to let test scripts read command line flags and then skip or run a test based on its own rules. This might be a more KISS approach. A helper function could be defined like: import test from 'ava'
function _test(title, fn) {
if (title.indexOf(test.flags.match) !== -1) {
test(title, fn);
} else {
test.skip(title, fn);
}
} |
Whoa! That's a long test run. Why is it timing out? Might this fix it? Is there some way AVA could fix your timeout issues by just being faster? (Any code you could share that demonstrates a bottleneck in AVA would be hugely helpful). As a hotfix, you could use environment variables:
import test from 'ava';
const selectors = (process.env.TEST_ONLY || '').split(',');
function testIf(selector, title, testFn) {
if (selectors.indexof(selector) !== -1) {
test(title, testFn);
}
}
testIf('foo', 'it handles foo', t => {
...
}); |
I don't think we should combine test paths and titles. Been thinking about it and maybe even using globs is overdoing it a bit. Globs have a lot of archaic rules and is actually really complex and really meant for paths. Maybe we could do our own simple replacers? |
At that point we are just replacing |
@jamestalmage My thinking is that some characters require escaping in a regex, so this would be simpler. And |
With mocha I used to do: describe('foo ', function () {
it('bar', function () {
...
});
it('baz - @slow', function () {
...
});
}); and then ran:
I don't see how to do that with what you are suggesting. What about one of these options:
|
@sindresorhus Oops, missed that one. Would like to see support for the |
Regardless of the outcome here, I made a simple module for wildcard matching as I can see it being useful in other cases too. Could use some feedback on it: sindresorhus/matcher#1 |
@sindresorhus Thanks! Need it in another module of mine as well :). |
@sindresorhus Looks good & simple, exactly what we need! |
Alright. Seems we have an agreement :) @matthewbauer Can you update your pull request to use |
#477 went stale, so this is now PR welcome again. |
I'd be happy to get cracking on this, unless of course someone beats me to it. Got 8 hours of day job to take care of now so there's a wide open window of opportunity to do so! |
@kasperisager To save your time — grab https://gist.github.com/develar/da7d3c01c3b8e6e0eace — it is a patch to apply to master (original PR is outdated). You only need to use proposed It works for me, but I am very busy now to prepare PR (well, can do in two days if you will not do first :)) |
@develar Cheers! I managed to sneak in a couple o' minutes and I'm just about to push some changes and wire up a PR. Thanks. @sindresorhus - Before I push my feature branch, I'd just like to verify whether the the aim is to support I got a wee bit lost reading through the discussion. |
@matthewbauer Yes |
@sindresorhus Cool! I'm guessing we want to support multiple patterns on a
|
👍 I usually prefer that too as it also has clearer intent. |
Good stuff. I'll get back to this asap. Got some other business I need to take care of for now. The one thing that bothers me just ever so slightly is how to get the That is to say; // works
var runner = new Runner({
match: ['*oo', '!foo']
});
// obviously this does not
var runner = new Runner({
match: '*oo',
match: '!foo'
}); The first example is what I would expect to utilise inside Or is there some magic on the CLI level, converting multiple occurrences of the same string option to an array? If so, I'm quite confident I'll have a PR posted tonight. |
@kasperlewau yes const cli = meow();
console.log(cli.flags);
|
@SamVerschueren Awesome! Not such a long ways to go then : ) |
Alright, on my lunch break. Thinking of the following; How do we enforce the How do we differentiate between a passed-in CLI flag and a configured value in I'm a little unsure as to what the relationship between the |
This is a bit of a distraction. Just follow the precedent by the
The flag parser is passed the default config from
There's a
|
Thank you so much for that @novemberborn!
Gotcha. I was casting to an array using
I should really get a new set of glasses (5 years since, I do believe), this makes it a lot clearer to me. As it's not part of the subject of the PR/issue I'd prefer not to add |
Yea that's fair. Just had a look and those are actually not testable in the same way. |
@novemberborn - do we need to add an issue to properly test that? |
@jamestalmage maybe. It's hard though without stubbing some dependencies in |
I would go with that option. The fixtures needn't be that complicated: // fixtures/match-pkg-conf/foo-passes.js
test('foo', t => t.pass());
test('bar', t => t.fail()); // fixtures/match-pkg-conf/bar-passes.js
test('foo', t => t.fail());
test('bar', t => t.pass()); // fixtures/match-pkg-conf/package.json
{
"ava": {
"match": "foo"
}
} Then just call the CLI with |
Usually our CLI tests aren't there to fully exercise the system (that's what our API unit tests are for), they just need to verify all the glue code works. |
@jamestalmage yes @kasperlewau added tests for |
This is something that I would do with mocha. Here's the mocha help:
I'm hoping to get something like that working.
The text was updated successfully, but these errors were encountered: