Jest 26 & Angular - Error Line Numbers incorrectly reported
Asked Answered
T

2

4

Ever since upgrading to Jest 26 & Angular 10, the unit test error line numbers are incorrectly reported. I have Been using Jest since Angular 4 and have upgraded with each new version.

I have read through every existing article on this and tried every suggestion I can find. None of them appear to work for my situation.

I have been able to create a minimum repo where you can see this happening (https://github.com/russcarver/angular-jest-test/tree/jest-line-errors). Be sure you're on the jest-line-errors branch. I've added comments to the app.component.spec.ts file (https://github.com/russcarver/angular-jest-test/blob/jest-line-errors/src/app/app.component.spec.ts) to show the issues.

The first one, oddly, is that I have to add a "dummy" test to the top of each spec file. Otherwise, the first test always fails in each file:

it('Should init the Test Suite', noop); // ****** The first test fails unless I add this ******

I created this solution out of desperation when nothing else would work. I have a hunch it's related.

This particular Angular upgrade also recommended splitting out the tsconfig.json file into tsconfig.base.json, tsconfig.app.json and tsconfig.spec.json.

When I change various options in tsconfig.spec.json, the error-line-numbers do change (specifically with the lib, module and target options). I have paired down this file to the minimum I need for my (large) project and have modified it in the repo to NOT extend tsconfig.base.json. I have also research and set the options to what I believe to be the best for my project (which targets the latest 2 versions of Chrome, Edge Safari & FireFox and uses all the latest JavaScript and TypeScript features).

I know it's not an Angular issue as I've been able to reproduce this with Angular 9, 10 and 11. Before all the tsconfig changes, it used to work in Angular 9 & Jest 25.

I also know (have tested) that it's not related to blank lines or comments in the spec files.

I have also tried a Babel configuration to no avail.

I used to use jest-preset-angular (which is now deprecated) but adding/removing that makes no difference.

Line number are incorrectly reported whether or not coverage is enabled/run.

We've been suffering through this since last July (2020). Any help would be greatly appreciated!

Toreutic answered 9/3, 2021 at 16:28 Comment(0)
T
0

This appears to be resolved after I:

  1. Upgraded to Angular 12.0.5
  2. Updated to "@types/jest": "26.0.23", "@angular-builders/jest": "12.0.0", "@types/jasmine": "3.7.7", "@types/jasminewd2": "2.0.9", "jasmine-core": "3.7.1", "jasmine-spec-reporter": "7.0.0", "jest": "26.6.3", "ts-jest": "26.5.6", and "ts-node": "10.0.0"
  3. I also removed the Babel configuration.
  4. NOTE: I tried upgrading to Jest 27 and that was a big mess.

I'm not sure which parts are responsible for the fix, but here is my package.json file. I'm happy to provide other files on request.

{
  "scripts": {
    "build": "npm run lint && npm run test-build && ng build --configuration=development",
    "build:dynamic": "npm run lint && npm run test-build && ng build --configuration=dynamic && npm run obfuscate",
    "lint": "ng lint",
    "ng": "ng",
    "obfuscate": "ng run resolver:obfuscate --configuration=production",
    "postinstall": "ngcc --tsconfig './tsconfig.app.json'",
    "refresh-project": "rimraf coverage/* && rimraf dist/* && rimraf node_modules/* && npm install",
    "start": "ng serve --host=0.0.0.0 --port 4200 --configuration=development",
    "start-dynamic": "ng serve --host=0.0.0.0 --port 4200 --configuration=dynamic",
    "test": "ng test -- --coverage --coverage-reporters=text-summary --coverage-reporters=lcov",
    "test-build": "ng test -- --coverage --coverage-reporters=text-summary --coverage-reporters=cobertura",
    "test-watch": "ng test -- --watch"
  },
  "private": true,
  "dependencies": {
    "@agm/core": "1.1.0",
    "@angular/animations": "12.0.5",
    "@angular/cdk": "12.0.5",
    "@angular/common": "12.0.5",
    "@angular/compiler": "12.0.5",
    "@angular/core": "12.0.5",
    "@angular/forms": "12.0.5",
    "@angular/material": "12.0.5",
    "@angular/platform-browser": "12.0.5",
    "@angular/platform-browser-dynamic": "12.0.5",
    "@angular/router": "12.0.5",
    "@auth0/angular-jwt": "5.0.2",
    "@google/markerclusterer": "2.0.9",
    "@material-extended/mde": "3.0.3",
    "@microsoft/applicationinsights-web": "2.6.3",
    "@microsoft/signalr": "5.0.7",
    "@okta/okta-angular": "3.1.0",
    "@srag/ngx-source-obfuscation": "2.0.0",
    "@types/file-saver": "2.0.2",
    "@types/googlemaps": "3.43.3",
    "@types/jest": "26.0.23",
    "@types/node": "14.17.3",
    "@types/sjcl": "1.0.29",
    "angular-user-idle": "2.2.6",
    "angulartics2": "10.0.0",
    "broadcast-channel": "3.7.0",
    "browser-globals": "0.0.2",
    "core-js": "3.10.0",
    "cross-env": "7.0.3",
    "date-fns": "2.22.1",
    "date-fns-tz": "1.1.4",
    "file-saver": "2.0.5",
    "gsap": "3.7.0",
    "hammerjs": "2.0.8",
    "kaop-ts": "4.3.0",
    "launchdarkly-js-client-sdk": "2.19.2",
    "ng2-file-upload": "1.4.0",
    "ngx-currency": "2.5.2",
    "ngx-mask": "12.0.0",
    "npm": "7.17.0",
    "rxjs": "6.6.7",
    "sass": "1.35.1",
    "save": "2.4.0",
    "sjcl": "1.0.8",
    "time-input-polyfill": "1.0.10",
    "web-animations-js": "2.3.2",
    "zone.js": "0.11.4"
  },
  "devDependencies": {
    "@angular-builders/jest": "12.0.0",
    "@angular-devkit/build-angular": "12.0.5",
    "@angular/cli": "12.0.5",
    "@angular/compiler-cli": "12.0.5",
    "@angular/language-service": "12.0.5",
    "@types/jasmine": "3.7.7",
    "@types/jasminewd2": "2.0.9",
    "codelyzer": "6.0.2",
    "crypto-browserify": "3.12.0",
    "jasmine-core": "3.7.1",
    "jasmine-spec-reporter": "7.0.0",
    "jest": "26.6.3",
    "jest-date-mock": "1.0.8",
    "jest-trx-results-processor": "2.2.0",
    "jest-zone-patch": "0.0.10",
    "rimraf": "3.0.2",
    "stream-browserify": "3.0.0",
    "ts-jest": "26.5.6",
    "ts-node": "10.0.0",
    "tslint": "6.1.3",
    "tslint-eslint-rules": "5.4.0",
    "typescript": "4.2.4",
    "webpack-bundle-analyzer": "4.4.2"
  },
  "engineStrict": true,
  "engines": {
    "node": "14.17.0",
    "npm": "7.17.0"
  }
}
Toreutic answered 17/6, 2021 at 21:52 Comment(0)
T
0

I finally have a better solution for this after upgrading to Angular 13 and Jest 27. Here are my changes:

Package.json changes:

"@angular-builders/jest": "13.0.0",
"@types/jasmine": "3.10.2",
"@types/jasminewd2": "2.0.10",
"@types/jest": "27.0.2",
"jest": "27.3.1",
"ts-jest": "27.0.7",

jest.config.js changes:

moduleFileExtensions: ['ts', 'html', 'js', 'json', 'mjs'],
resolver: 'jest-preset-angular/build/resolvers/ng-jest-resolver.js',
testRunner: 'jest-jasmine2',
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
transform: {
 '^.+\\.(ts|js|mjs|html|svg)$': 'jest-preset-angular',
},

Toreutic answered 15/11, 2021 at 4:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.