Closed msk86 closed 11 years ago
I think this depends on the framework you're using. I'm using Jasmine for the the tests, and I can achieve this by renaming individual test cases or test suites from it()
to iit()
or describe()
to ddescribe()
- it's a feature of the testing framework rather than a feature of Karma.
Update: iit()
and ddescribe()
have been replaced by fit()
and fdescribe()
as of karma-jasmine v0.3.3 and the link to the documentation for focused tests in Jasmine.
@psyked is absolutely correct. With Mocha, you can use it.only
and describe.only
.
Btw, please ask questions on maling list ;-)
WIth mocha you can do mocha test/test-name.js Does karma have this ability?
@base698 you can do something like that with Karma+karma-mocha (thanks to @gfxmonk):
karma start
# and then, everytime you wanna run some tests
karma run -- --grep=filteredtestexpr
This does not filter by filenames but rather the test description (it("should....")
). You could make a setup that does this based on filenames, if you really want to...
That said, the recommended workflow is you start Karma and let it to watch the files and run on every save, in which case this makes no sense - you wanna stay in the editor and that's why we use it.only
and describe.only
(mocha) or iit()
and ddescribe()
(Jasmine).
hii am new to angular js...anyone help me how to write testcases in angular js using karma-jasmine please send the examples to devidurga475@gmail.com
@devi432 my sides are in orbit
any update?I want to run a specific file test.
FYI for Jasmine you can use fdescribe
and fit
.
+1 @dbkaplun
For the record, I'm absolutely glad that this question was asked in public and not in a mailing list. None of us would have this information if it were otherwise.
can the comment of psyked commented on 21 May 2013 be updated to mention the f instead of d? And perhaps add a link to the documentation for these functions?
@bochove Should be useful http://thejsguy.com/2016/01/03/controlling-which-tests-run-in-jasmine.html
@vojtajina, following does not work for me, it runs all suite and tests still. The only different between following shell snippet and your is that I am passing configFile param to karma start
command.
karma start build/webpack.test.js
# and then, everytime you wanna run some tests
karma run -- --grep=filteredtestexpr
Also, not sure why the --grep
option is not mentioned in the karma docs.
gpatel$ ./node_modules/.bin/karma run --help
Karma - Spectacular Test Runner for JavaScript.
RUN - Run the tests (requires running server).
Usage:
../../.nvm/versions/node/v4.3.0/bin/node ./node_modules/.bin/karma run [<configFile>] [<options>] [ -- <clientArgs>]
Options:
--port <integer> Port where the server is listening.
--no-refresh Do not re-glob all the patterns.
--fail-on-empty-test-suite Fail on empty test suite.
--no-fail-on-empty-test-suite Do not fail on empty test suite.
--help Print usage.
--log-level <disable | error | warn | info | debug> Level of logging.
--colors Use colors when reporting and printing logs.
--no-colors Do not use colors when reporting or printing logs.
gpatel$
I don't like the describe.only
(mocha) and ddescribe
(jasmine) solutions as those are error prone. You might forget to revert the .only
part before committing and your jenkins/builds would still be in joy thinking the test suites are passing.. ;)
Is there seriously no way to run focused tests while working on a single file without modifying the file in question? That seems super stupid.
Exactly, +1 for this as sometimes you just push your code with focused tests.
Currently, I use command line arguments to define which set of tests I need to run. For example, I want to run those tests under src/controllers
folder, and the start command is something like:
karma start src/controllers
And in karma.config.js
, I select files based on the argument passed:
let path = process.argv[process.argv.length-1];
//only load files under the path
let files = `${path}/**/*.js`;
module.exports = function(config){
config.set({
// ...
files
})
}
@hamxabaig While I still want to see support for testing a single file, your problem can be avoided with git hooks.
What the heck do fdescribe
and fit
, stand for? lol
@ORESoftware Apparently it's 'focused':
Focusing specs will make it so that they are the only specs that run. Any spec declared with
fit
is focused. You can focus on adescribe
withfdescribe
@MarkoCen thank you so much! we were trying to add an npm script for running a suite of acceptance tests separate from our suite of unit tests. having a script per suite is much nicer than endlessly swapping describes for fdescribes and vice versa.
@alexluecke What kind of script would I put in the git hook? Could you give an example?
@reiinakano
You could put something like the following in the pre-commit
hook:
#!/usr/bin/env bash
focused_test_files=()
for file in $(git --no-pager diff --name-only --staged); do
# User might be commit only partial changes of a file that has focused tests.
# Make sure only the staged changes do not contain a focused test:
result=$(git --no-pager diff --cached $file \
| grep "^+" \
| grep "\(fdescribe\|fit\)"
)
[ -n "$result" ] && focused_test_files+=("$file")
done
if [ -n "$focused_test_files" ]; then
echo "Following files have focused tests:"
for f in "${focused_test_files[@]}"; do echo -e " $f"; done
exit 1
fi
I haven't vetted this too much, but it shouldn't have too many issues.
@devi432 lol
With grunt you can run package test like below :
grunt karma:apps-packageName
replace test from it
to fit
and set your test.
define the file name in the test.ts file
const context = require.context('./', true, /my-component.spec.ts\.spec\.ts$/);
@kedar9444 Thanks this is what I was searching for ...... Works beautifully 👍
@kedar9444 should be
const context = require.context('./', true, /my-component\.spec\.ts$/);
because .spec.ts
is already there.
FWIW, if you want to run a specific test or tests, you can do something like this:
(in your config)
module.exports = function(config) {
config.glob = config.glob ? config.glob : 'path/to/my/spec/files/**/*.js';
return config.set({
...
files: [
...
config.glob,
...
],
...
});
};
To run every spec file with a name starting with 'c':
karma start my/karma.conf.js --glob=path/to/my/spec/files/**/c*.js
To run a specific file:
karma start my/karma.conf.js --glob=path/to/my/spec/files/some_spec.js
You know I have lots of test cases, when code on only one file, I dont want karma to run all the test for me, that will take a lot of time. So, I can specify the test case I want to run under command line? thanks!