Describe the bug
Unable to run Jest tests with a custom reporter (https://jestjs.io/docs/en/cli#reporters).
To Reproduce
ng test. OKng test --reporters=default. Failure:ng test --reporters=default
Schema validation failed with the following errors:
Data path ".reporters" should be array.
ng test --reporters=default --reporters=default. OKExpected behavior
Jest builder should be able to pass single --reporters option to Jest.
Builder:
@angular-builders/[email protected]Libraries
@angular-devkit/[email protected]Additional context
Looks like the issue is relevant for some other array options, e.g. --testPathPattern. But the issue is not reproduced for --runTestsByPath, though it doesn't work with --runTestsByPath either.
To be clear, it's not a blocking issue, there is a workaround of specifying the same reporter twice, is that right?
I think this is Angular CLI's issue (to be precise it's how it parses the arguments) but it needs further investigation.
@meltedspark Agree, not a blocker. You can specify the same reporter, or better another one --reporters=default.
@meltedspark JetBrains IDE integration (for Angular projects using Jest runner) passes --reporter. This issue is required for it to be working.
Let me see if I got it clear. JetBrains IDE integration for Angular projects (not necessarily running with Jest I believe but the one that runs ng test) runs it with --reporter and not --reporters.
If this is the case, it would be another issue, please open a feature request.
Or maybe that's not the case and it's just a typo (you meant --reporters).
In any case I'd ask you to provide a full command line that the IDE passes to the ng test command so that I would be able to make a proper test. I will look into that.
You can see a full command in this jetbrains issue:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/360001785599-2018-3-Seems-to-Break-Jest-Running
unknown options are: --reporters and --testPathPattern
@meltedspark Sorry for the typo, I meant --reporters.
JetBrains IDE integration for Angular projects (not necessarily running with Jest I believe but the one that runs
ng test) runs it with--reporterand not--reporters.
IDE passes --reporters for Angular projects using Jest test runner only. For Angular projects using Karma test runner, a different command line is combined.
Example full output:
"C:\Program Files\nodejs\node.exe" --require C:\Users\user\AppData\Local\JetBrains\Toolbox\apps\WebStorm\ch-0\183.5153.33\plugins\JavaScriptLanguage\helpers\jest-intellij\lib\jest-intellij-stdin-fix.js C:\Users\user\temp\jest-sample\node_modules\@angular\cli\bin\ng test --colors --reporters C:\Users\user\AppData\Local\JetBrains\Toolbox\apps\WebStorm\ch-0\183.5153.33\plugins\JavaScriptLanguage\helpers\jest-intellij\lib\jest-intellij-reporter.js --verbose
Unknown option: '--reporters'
Unknown option: 'C:\Users\user\AppData\Local\JetBrains\Toolbox\apps\WebStorm\ch-0\183.5153.33\plugins\JavaScriptLanguage\helpers\jest-intellij\lib\jest-intellij-reporter.js'
Process finished with exit code 1
It'd be great if @angular-builders/jest supports all Jest CLI options. Is it possible?
Well, technically it should support all the Jest CLI options by now. Practically it turns out that there are some issues with some of them.
I'll look into reporters issue and see what can be done. As far as I understand the issue exists with all the array options, so solving it for reporters will solve for the others as well.
The fix is on its way, but that won't be enough for fixing the IDE issue. They are passing the --reporters in a wrong way. The right way to do that is --reporters=default --reporters=jest-junit etc. They don't use = sign therefore it won't work even after the issue is fixed here.
I suggest you to bring it up on their issue tracker.
Fixed in 7.2.1
@meltedspark Thanks a lot for the fix!
The fix is on its way, but that won't be enough for fixing the IDE issue. They are passing the
--reportersin a wrong way. The right way to do that is--reporters=default --reporters=jest-junitetc. They don't use=sign therefore it won't work even after the issue is fixed here.
Looks like, now tests can be run without = sign too: both ng test --reporters default and ng test --reporters=default work for me with @angular-builders/[email protected].
BTW, I'm a maintainer of Jest test runner in JetBrains IDEs, feel free to ping me regarding IDE-related issues.
Works like a charm.
Little note for future readers: For an angular multi project setup I had to use jest's testMatch config option for each project in angular.json:
"testMatch": [
"**/<project-name>/**/?(*.)+(spec).(ts|js)?(x)"
]
@sroettering That's weird, I thought this code is handling this. Isn't it?
If not, that would be a bug, could you confirm that please?
This is the jest (test) configuration I use in angular.json for every project:
...
"test": {
"builder": "@angular-builders/jest:run",
"options": {
"setupTestFrameworkScriptFile": "./setupJest.ts",
"detectOpenHandles": true,
"coverage": true
}
},
...
If I run any test now in Intellij with --showConfig option, the testMatch always stays the same no matter in which project the test file is located. If you need more info I could setup a demo repository.
@sroettering That would be great, thank you!
I created a demo repository here: https://github.com/sroettering/jest-sample
In there is the standard generated angular app and a library.
Running the app tests works perfectly fine, however, running the lib tests throws a validation error because some preprocessor file could not be found because it is searched in the "wrong" place.
In my real use case I have a global node_modules folder instead of a separate one for the library. I fixed that validation error by setting the working directory in the intellij jest run configuration to the project root (../jest-sample). Doing that causes the testMatch issues from above.
I think it's not an issue with the code that is generating the testMatch property, but rather with my configuration.
Update: I cloned the examples folder and tried to run the multiple-apps example directly from intellij and it didn't work either. The first app (my-first-app) runs correctly. The second one (my-second-app) uses a testMatch option which searches test files in my-first-app and when running my-shared-library tests the rootDir is set to the lib root.
Running the tests via ng test <project-name> from a terminal works, however.
Should I open another issue for this?
Yes please. That might also be an IDE integration issue (not sure how it handles multi project workspaces).
Please add @segrey to the discussion when opening a new issue.
Thank you for your contribution!
I am having a hard time getting the jest-junit report to show with multiple apps project. I am using the the builder v.7.2.1. Can anyone share an example app which can give a code coverage report(jest-junit) with multiple-apps config.