When I generate a Jest code coverage report for my Vue 2.7.X app, the lines shown as covered/uncovered in the report don't make any sense:
The red sections in this report should indicate code that's not covered (executed) by the test suite, but obviously it makes no sense to show comments (lines 290, 291) as uncovered, or to show (part of) line 298 as uncovered when the lines before and after are covered.
I guess what's happening is that the lines which Jest detects as uncovered are not being correctly mapped back to the source code, so there may be a problem with the Babel transpilation.
I generate the code coverage report with yarn jest --coverage and the application source code is written in JavaScript (rather than TypeScript).
some of the dependencies from package.json which may be relevant to the problem are shown below:
"devDependencies": {
"#babel/core": "^7.20.5",
"#babel/preset-env": "^7.20.2",
"#vue/test-utils": "1.3.3",
"#vue/vue2-jest": "29.2.2",
"#vitejs/plugin-vue2": "^2.2.0",
"babel-jest": "^29.3.1",
"http-proxy": "^1.18.1",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"sass": "1.32.13",
"sass-lint": "^1.13.1",
"start-server-and-test": "^1.14.0",
"unplugin-vue-components": "^0.22.12",
"vite": "^4.0.1",
"vite-plugin-rewrite-all": "^1.0.1",
"vue-template-compiler": "^2.7.14"
}
Minimum Reproducible Example
I've created a minimal Git repo that demonstrates the problem.
Clone the repo
Run yarn install && yarn test:unit:coverage to generate the coverage report (or use npm instead)
Open the file coverage/lcov-report/index.html to see the report
If you open the page for components/toaster-message.vue, it says 1/2 branches and 2/3 functions are covered, but none of the lines are marked in red (hideAppMessage should be red because it's not tested).
If you open the page for views/login.vue the lines that are marked in red (uncovered) don't make any sense. There are no tests for this component.
What worked for me was adding coverageProvider: 'v8' to the jest.config.js. I'm not entirely sure why changing coverageProvider works, but seems like vue-jest is having general problems with collecting coverage correctly due to babel dependency changes.
Try adding this to your Jest configuration:
coverageProvider: 'v8'
Then npm run test:unit:coverage.
This worked on Node v16.16.0 and v18.12.1.
jest uses its own version of Babel to transpile your code, but the source map that generates it could be unnacurate or have a differrent version than the one you are using.
You can make sure that jest is using the same version of Babel that you're using in your app by adding the following configuration to your package.json file:
"jest": {
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/babel-jest"
}
}
Also, could it be that even though your package.json has that version you have something else in the package-lock.json?
Looks like solution with coverageProvider: 'v8' added by #morganney works.
For the problem of generating a coverage report.
Failed to write coverage reports:
ERROR: Error: ENOENT: no such file or directory, open 'path\to\file\<<jsx-config-pragma.js>>.html'
Just remove the .ts("\\.(tsx|ts|jsx|js)$") files from configuration (package.json)
...
"transform": {
"\\.(tsx|jsx|js)$": [
"#swc-node/jest",
{
"dynamicImport": true
}
]
},
...
I am trying to open, build and run someone else's Angular 4 project but I am not able to view the project when I run it my way. I don't see what is going wrong or what I should do now. I already had everything in place to use NPM and NodeJS
The steps I took were:
Open up the project
npm install
ng serve
The project compiles the right way. (I have an own Angular app and I know how this looks like) The console is showing:
'** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200 **'.
Then, when I opened up a web browser, I navigated to localhost:4200 and a web page with the following text were shown:
'Cannot GET /'
And on the console was the following text:
'GET http://localhost:4200/ 404 (Not Found)'
The project should work fine but I am not able to navigate to a working URL on the web page. Routing is set-up another way as I am used to doing this. In app.module.ts the following is implemented:
app.module.ts
const appRoutes: Routes = [
{ path: '', redirectTo: 'tree', pathMatch: 'full' },
{ path: 'admin', component: AdminPanelComponent, canActivate: [AuthGuard],
children: [{path:'', component: PanelComponent},{path: 'add', component:
AddTreeComponent}, {path:'manage-trees', component:ManageTreesComponent},
{path:'manage-users', component: ManageUsersComponent}, {path:'view-trees',
component: ViewTreeComponent}]},
{path:'tree', component: TreeComponent},
{path:'error', component: ErrorComponent},
{path:'unauthorized', component: UnauthorizedComponent},
{path:'login', component: LoginComponent},
{path:'entire-tree', component: EntireTreeComponent},
{ path: '**', component: PageNotFoundComponent },
];
Also opening up a web page like; localhost:4200/tree does not work. When I let angular stop serving the web page, the web page displays: "this site can't be reached'. So I think there is running something at localhost:4200... Also, another project of this person behaves the same way.
Does anybody know what is going on?
EDIT
app.module.ts
RouterModule.forRoot(appRoutes, { useHash: true })
Package.json
{
"name": "xxx",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"#angular/animations": "^4.0.0",
"#angular/common": "^4.0.0",
"#angular/compiler": "^4.0.0",
"#angular/core": "^4.0.0",
"#angular/forms": "^4.0.0",
"#angular/http": "^4.0.0",
"#angular/platform-browser": "^4.0.0",
"#angular/platform-browser-dynamic": "^4.0.0",
"#angular/router": "^4.0.0",
"angular-oauth2-oidc": "^1.0.20",
"angular-polyfills": "^1.0.1",
"angular2-jwt": "^0.2.3",
"angular2-spinner": "^1.0.10",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"ngx-bootstrap": "^1.8.0",
"rxjs": "^5.1.0",
"zone.js": "^0.8.4"
},
"devDependencies": {
"#angular/cli": "1.2.4",
"#angular/compiler-cli": "^4.0.0",
"#angular/language-service": "^4.0.0",
"#types/jasmine": "2.5.45",
"#types/node": "~6.0.60",
"codelyzer": "~3.0.1",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-coverage-istanbul-reporter": "^1.2.1",
"protractor": "~5.1.2",
"ts-node": "~3.0.4",
"tslint": "~5.3.2",
"typescript": "~2.3.3"
}
}
I also see an icon next to the tab name with the label: "Error".
OBSERVATION:
New observation:
After I ran npm install -g angular-cli I wasn't able to run ng serve. (You have to be inside an angular-cli project in order to use the build command after reinstall of angular-cli)
Then I ran npm install -g #angular/cli#latest and I was able to use ng serve again.
OBSERVATION 2:
After building the app with: 'ng build ...' there is no index.html in the 'dist' folder... When I set the website online, there is just a folder structure instead of a nice website. I think that's because there is no index.html.
The way I resolved this error was by finding and fixing the error that the console reported.
Run ng build in your command line/terminal, and it should display a useful error, such as the example in red here: Property 'name' does not exist on type 'object'.
For me it also was problem with path, but I had percentage sign in the root folder.
After I replaced %20 with space, it started to work :)
The problem was that I ran the commands within the folder /project/src/app. Navigating back to the project folder so to /project and running ng serve from there solved my problem.
I had the same error caused by build errors. I ran ng build in the directory of my application which helped me correct my errors
I had the same problem with an Angular 9.
In my case, I changed the angular.json file from
"aot": true
To
"aot": false
It works for me.
Check if in index.html base is set
<head>
<base href="/">
...
</head>
I was using export class TestCalendar implements OnInit{} but i did not write the function
ngOnInit() {
/* Display initial */
}
. After running the command ng serve , i found out that i was not using ngOnInit(){} . Once i implemented, it started working fine. Hope it helps someone.
Just figured out the reason when we type "ng serve" INSIDE OUR PROJECT..
for example C:\Users\EdgeTech1\Desktop\CSharp\WebAPI\MyProject>ng serve
could not resolve module C:\Users\EdgeTech1\Desktop\C
results: failed compiled
root cause:
My folder name was C# Project..
Note: I tried to remove the # in my Project Name, I rename C# Project to CSharp instead and I tried to open cmd prompt again, typed the same thing..
for example:
C:\Users\EdgeTech1\Desktop\CSharp\WebAPI\MyProject>ng serve
and my project compiled successfully.. so as much as possible avoid ASCII characters in naming projects files.
I had the same problem with an Angular 6+ app and ASP.NET Core 2.0
I had just previously tried to change the Angular app from CSS to SCSS.
My solution was to go to the src/angularApp folder and running ng serve. This helped me realize that I had missed changing the src/styles.css file to src/styles.scss
I was referring to one of my provider with two different casing. One of them was wrong but only the webpack compiler was complaining. I had to step into the ClientApp folder and use ng build or ng serve to see the errors. (ASP.NET Core SPA with Angular 5)
Check baseHref is set to "/" ( angular.cli )
"architect": {
"build": {
"builder": "#angular-devkit/build-angular:browser",
"options": {
"baseHref": "/"
if it didn't work, check if your base href in your index.html is set to "/"
For me the issue was with #Component Selector path was pointing to wrong path. After changing it solved the issue.
#Component({
selector: 'app-fetch-data',
templateUrl: './fetch-data.component.html',
providers: [ToolbarService, GroupService, FilterService, PageService, ExcelExportService, PdfExportService]
})
This error can apparently happen for a number of reasons. Here is my experience for those who land here after searching for "Cannot GET /"
I experienced this error when I installed ng-bootstrap v6 into an Angular 8 project. I downgraded ng-bootstrap to v5 and it seems to be okay now, since ng-bootstrap v6 is only compatible with Angular 9.
This happened while working in Visual Studio 2019 with the Angular .NET Core template. At the same time, the output panel of Visual Studio displayed this esoteric sounding error: "TS1086: An accessor cannot be declared in ambient context," which led me here, which, after a little reading, made me think it was a versioning issue. It appears that it was.
I fixed it by changing "#ng-bootstrap/ng-bootstrap": "^6.0.0"," to "#ng-bootstrap/ng-bootstrap": "^5.0.0" in the package.json file and rebuilding.
if you changed any name or file component , check if it is the right name
#NgModule({
declarations: [
NewNameComponent
],
imports: [
CommonModule,
],
exports:[
NewNameComponent
]
})
export class YourModule { }
Generally it is a versioning issue. Node.js v8 cannot compile with angular-cli 6.0 or later. angularcli v6 and above will work for lastest node versions. Please make sure if your node version is v8, then you need to install angular-cli upto 1.7.4.
enter ng -v command in cmd and check the cli and node versions.
See this answer here. You need to redirect all routes that Node is not using to Angular:
app.get('*', function(req, res) {
res.sendfile('./server/views/index.html')
})
You can see the errors after stopping debbuging by choosing the option to display ASP.NET Core Web Server output in the output window.
In my case I was pointing to a different templateUrl.
First, delete existing files package.lock.json and node_modules from your project.
Then, the first step is to write npm cache clean --force. Second, also write this command npm i on the terminal. This process resolve my error. :D
Many answers dont really make sense but still have upvotes, makes me currious why that would still work in some cases.
In angular.json
"serve": {
"builder": "#angular-devkit/build-angular:dev-server",
"options": {
"deployUrl": "/",
"baseHref": "/",
worked for me.
For me the problem was that I had saved my project folder under C:\Users\... which indeed is a problem. After I saved it under C:\ then npm install and ng serve --open it worked just fine!
In my angular.json file the deployUrl was set to static/ang.
"architect": {
"build": {
"builder": "#angular-devkit/build-angular:browser",
"options": {
"deployUrl": "/static/ang/",
....
This was causing my application to get served from localhost:4200/static/ang
Removing deployUrl fixed it for me.
EDIT:
I shifted deployUrl to under configurations since it was important when building for production. (Since my lazy loaded chunks were not getting served on the static url).
"architect": {
"build": {
"builder": "#angular-devkit/build-angular:browser",
"options": {
....
....
},
"configurations": {
"deployUrl": "/static/ang/",
Bumped into same issue, I tried this:
-ng build
And it worked!
Another problem may be that you're importing the component instead of it's module.
imports: [
YourComponent, // <-- Should be YourComponentModule
]
For me the problem was that the Angular project was not getting compiled when executing because of some undelared variable in the ts file which was binded to an html tag.
You can use ng serve --verbose true to display the build log in terminal to check where the application is breaking.
The weird thing that I was experiencing was that I could make changes to the components in Visual Studio 2019 while the app was running and see my changes but, when I restarted the app, I got the Cannot Get / error. Instead of running IIS Express, I chose to run the app using Angular JS and the build window showed me that there was an error in app.component.ts. It turned out to be an extra } at the end of the file. Not sure how it got there but, when I removed it, the app works fine.
For me the issue was that my local CLI was not the same version as my global CLI - updating it by running the following command solved the problem:
npm install --save-dev #angular/cli#latest
Deleting node modules folder worked for me.
Delete the node modules folder
Run npm install.
Re-run the application and it should work.
I have a meteor project, where I have a few jquery plugins installed and working fine - some via NPM, some via Atmosphere.
package.json:
{
"name": "eyeconnect",
"description": "Teleoftalmologia",
"version": "1.0.2",
"dependencies": {
"autosize": "^3.0.21",
"babel-runtime": "^6.23.0",
"bcrypt": "^0.8.7",
"bootstrap-select": "^1.12.2",
"croppie": "^2.5.0",
"dropzone": "^5.1.1",
"fibers": "^2.0.0",
"html-pdf": "^2.1.0",
"jquery": "^2.2.3",
"lodash": "^4.17.4",
"moment": "^2.15.1",
"select2": "^4.0.3",
"spin.js": "^2.3.2",
"typeahead.js": "^0.11.1"
}
}
.meteor/packages:
# Meteor packages used by this project, one per line.
# Check this file (and the other files in this directory) into your repository.
#
# 'meteor add' and 'meteor remove' will edit this file for you,
# but you can also edit it by hand.
meteor-base#1.1.0 # Packages every Meteor app needs to have
mobile-experience#1.0.4 # Packages for a great mobile UX
mongo # The database Meteor supports right now
blaze-html-templates#1.0.4 # Compile .html files into Meteor Blaze views
session#1.1.7 # Client-side reactive dictionary for your app
jquery#1.11.10 # Helpful client-side library
tracker#1.1.3 # Meteor's client-side reactive programming library
es5-shim#4.6.15 # ECMAScript 5 compatibility for older browsers.
ecmascript#0.8.1 # Enable ECMAScript2015+ syntax in app code
iron:router
twbs:bootstrap
accounts-ui#1.1.9
accounts-password#1.4.0
less#2.7.9
accounts-ui-unstyled#1.2.1
check#1.2.5
ejson#1.0.13
joshowens:accounts-entry
aldeed:collection2
aldeed:autoform
rajit:bootstrap3-datepicker
aldeed:autoform-bs-datepicker
msavin:mongol
edgee:slingshot
eluck:aws-sdk
email#1.2.3
force-ssl#1.0.14
standard-minifier-css#1.3.4
standard-minifier-js#2.1.1
horka:swipebox
random#1.0.10
meteorhacks:zones
matb33:collection-hooks
shell-server#0.2.4
meteorhacks:aggregate
percolate:synced-cron
dynamic-import#0.1.1
After several months, I decided to cleanup my local copy, by performing the following actions:
* remove the node_modules folder
* meteor reset
After this, I ran the app locally, and everything seems to work fine, except for the jquery select2 plugin. On the Blaze page where it's used, I always get the error that select2 is not a function in jQuery.
Calling code:
$("#cid10")
.select2({
allowClear: true,
data: dataSet
})
Error logged on browser:
Error: $(...).select2 is not a function
Important to say that I have no code change, just executed the cleanup actions mentioned above.
After some faildd attempts to make it work, I decided to checkout the repo (same code version) into another folder, built and run the project, and still the same error. However, on other machines running the same checkout, it works fine. Doesn't seem to be a browser cache/issue either - I cleaned cache/history, tried different browsers, still the same error.
I executed a simple jquery query via the console, and analyzed the function prototype of the returned object, and indeed the function select2 is not listed there. So it seems like for some reason, when I import select2 into the javascript file of the template where it's supposed to run, it does import, but is not registering itself within jquery as a new function.
Since select2 is installed via NPM, I'm importing it with:
import select2 from '/node_modules/select2/dist/js/select2.full.min.js';
Alternatively, I tried to copy both the js and css files from the node_module dist folder into my project's client folder instead of importing them, but get the same result.
Any help here would be very much appreciated!