Is it possible to specify a path to a specific file when running ng test
? Or only run tests in specific files somehow? Can't seem to find any issues or documentation about it anywhere.
Thanks!
Mac OSX (El Capitan)
angular-cli: 1.0.0-beta.18
node: 4.4.2
os: darwin x64
Been having this problem this week too.
My workaround was to go to src/test.ts
, and modify line 30:
.then(() => require.context('./', true, /\.spec\.ts/))
to match only the tests I'm interested in.
This remains only a workaround though. I don't think this is good enough to be the official thing.
Hm, isn't it enough to just use fit
instead of it
on a test? e.g. fit('should create the app', async(() => { (...)
Ah, great! Didn't know about fit
, which totally works, thank you very much!
How to use fit?
You know how your tests are usually like:
describe('Something', () => {
beforeEach(() => {
// ... preparation
});
it('should ...', async(() => {
// ... test 1 logic here
}));
// ... more tests and/or nested `describe()` calls
});
If you replace the it()
call of one or more tests with fit()
, only these tests will be run.
You can do the same with describe()
as well. If you replace it with fdescribe()
(prepending f
to it), only the tests in this fdescribe()
call (and any other fit()
tests outside it) will be run.
does this support for mocha?
The usage of fit()
or fdescribe()
is really a workaround I believe and this functionality should be supported in ng test
In fact, I think a lot of the CLI from Facebook's Jest (http://facebook.github.io/jest/docs/en/cli.html#content) could be borrowed for example:
ng test --watch
- To watch and re-run only the tests which have changedng test --coverage
- Free code coverage reportMost of these are Karma / Jasmine issues rather than CLI issues.
At the moment the CLI is committed to only supporting one test tooling chain, which is Karma and Jasmine. Since there cannot be hidden 100% like a bundler is (because you use the test framework APIs in your tests), the choice is also exposed to the users. This has nice side effects like:
test.ts
file used to run the testsAlso, if you agree to give up calling ng test
(replacing it with a test
npm script for example, so you just run npm test
), you can easily move away to Jest if you prefer to. I have tried it myself in a medium size real project and found it pretty easy.
It's worth mentioning that the built in jasmine HTML reporter does allow you to click on a single test or suite of tests and only have these run, and the CLI itself supports a watch mode (enabled by default actually, and can be changed either per CLI call, or by changing the default in Karma config). It's not a Jest-style watch for changed files only, but that's because it's simply not Jest. It's Karma + Jasmine.
What might be useful though is a lint rule that would fail linting if you have fdescribe
/ fit
(similar to how some lint rules fail calling console
). Not sure if it's reasonable to include in the CLI template, because it's yet another weird package in the project's devDependencies
. But it's also easy to add manually in your project anyway.
a feature passing the spec name or a regex pattern like jest has it, would indeed by nice. i know about fit and xit, however its not optimal. you have to replace all its in the spec and even more important you might forget to unfit before commiting.
As far as I know, this is inherent in Karma itself. And / or Jasmine and / or karma-webpack.
It doesn't have an incremental run like jest, so the name pattern thing doesn't work as well.
If there is some reporter or plugin that can do it, you can add it to the Karma config yourself.
Also, if you really want to, you can use Jest with your Angular CLI project using https://github.com/thymikee/jest-preset-angular -- although no particular official support for it. ng test
might not work - not sure, but spec files should be compatible with Jest as it's very close to Jasmine.
i tried jest preset angular. it works somehow but not really well for me. i have some tests failing, which dont fail with ng test. for now i stick with fit and ng test, since i was able to speed up the test suite significantly using beforeAll for initialization of the testbed instead of beforeEach (thx to @vvasabi https://github.com/angular/angular/issues/12409#issuecomment-314814671).
this approach yields currently the best results for me. however the watch/pattern/mode jest offers seems superior and would be great to have in angular "natively" if somehow possible.
It seems that Jest is really great for you. I tried it a few months back on a medium project (200+ tests) and went fairly well after following
jasmine
to jest
or expect
transformIgnorePatterns
partIt worked well. The only reason I couldn't keep it was that the team I worked with was comfortable with current tooling and not ready for a change at the time, especially tha the tooling was standard across multiple teams.
It's beyond the scope of this issue and Angular CLI with Angular's current official preferred tooling. But I thought it might be useful to mention.
Also (and still out of scope), beforeAll
can have unexpected side effects between tests. I wouldn't really recommend it. Instead, ensuring you add too little to TestBed
testing module seemed to help a few teams on multiple projects, which you can achieve by mocking everything other than the test subject. You might even be able to test some things with direct instantiation
new WhatIWantToTest(mockDependency1)
.
But again, as it stands today, it's not very relevant here anyway.
Thanks for sharing the comment link still BTW.
Cheers,
@Meligy I tried giving specific file name by modifying line no 20 in src/test.ts like
const context = require.context('./', true, 'calc-details.compontnt.spec.ts');
but didn't work
The problem I am running into with Angular's current setup is that when I click a test to re-run only that test, as soon as I change any of the source code, the browser is refreshed and all the tests run again. Is that the intended behavior?
@filipesilva as far as I know, as soon as you use some other setup, that is not excluded, e.g. jest
with jest-preset-angular
it will disallow you usage of fit
or fdescribe
as jest doesn't support those.
We actually recently merged a new feature to run only specific spec files: https://github.com/angular/angular-cli/pull/13423
In regards to third party test runners like jest-preset-angular
, you really need to ask their maintainers about features. We don't maintain it.
Looks like a nice feature, but seems, that it is missing in docs at the moment. Not in current v8 version, nor in next version.
@jeserkin that is indeed true, thank you for bringing it up. I opened https://github.com/angular/angular/issues/31821 so we can get that sorted.
Actually I had the name wrong, the option is called include
and is listed in https://angular.io/cli/test:
Will update the PR to list that new name.
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
_This action has been performed automatically by a bot._
Most helpful comment
Hm, isn't it enough to just use
fit
instead ofit
on a test? e.g.fit('should create the app', async(() => { (...)