While running a sample Jest test on a particular structure, I noticed that the errors in the output summary from Jest are not lining up correctly.
Here is a snippet from my package.json file:
"devDependencies": {
"@types/jest": "^22.0.1",
"jest": "^22.1.4",
"jest-preset-angular": "^5.0.0",
"ts-jest": "^22.0.1",
"typescript": "^2.6.2"
},
The specific issue can be seen in the output below:
Venue › Venue Structure › should have all the structure fields
expect(received).toBeDefined()
Expected value to be defined, instead received
undefined
20 | it('should be an instance of DataStructure', () => {
21 | expect(VenueInfo instanceof DataStructure).toBeTruthy();
> 22 | })
23 |
24 | it('should have the proper number of data fields', () => {
25 | expect(VenueInfo.NumberOfFields).toBe(14); // LastUpdated is added automatically
at src/structures/Venue.spec.ts:22:35
Interestingly, the error seems to be occurring at line 29 according to the information within the it()
function.
The relevant part of the Venue.spec.ts file is provided below:
import { TestBed, ComponentFixture } from '@angular/core/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { Venue } from './Venue';
import { DataStructure } from '../library/base/DataStructure';
describe('Venue', () => {
let VenueInfo: Venue;
beforeEach(() => {
VenueInfo = new Venue();
});
describe('Class', () => {
it('should be an instance of Venue', () => {
expect(VenueInfo instanceof Venue).toBeTruthy(); ;
});
it('should be an instance of DataStructure', () => {
expect(VenueInfo instanceof DataStructure).toBeTruthy();
})
it('should have the proper number of data fields', () => {
expect(VenueInfo.NumberOfFields).toBe(14); });
});
describe('Venue Structure', () => {
it('should have all the structure fields', () => {
expect(VenueInfo.Key).toBeDefined();
expect(VenueInfo.Description).toBeDefined();
expect(VenueInfo.Address1).toBeDefined();
expect(VenueInfo.Address2).toBeDefined();
expect(VenueInfo.City).toBeDefined();
expect(VenueInfo.Province).toBeDefined();
expect(VenueInfo.Country).toBeDefined();
expect(VenueInfo.PostalCode).toBeDefined();
expect(VenueInfo.Telephone).toBeDefined();
expect(VenueInfo.Latitude).toBeDefined();
expect(VenueInfo.Longitude).toBeDefined();
expect(VenueInfo.MajorIntersection).toBeDefined();
expect(VenueInfo.DartBoards).toBeDefined();
});
});
});
Another important file to consider is tsconfig.json:
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"dom",
"es2015"
],
"module": "es2015",
"moduleResolution": "node",
"sourceMaps": true,
"target": "es5"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"src/**/*.spec.ts",
"src/test-config"
],
"compileOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}
Lastly, there was also a tsconfig.spec.json file present in the src directory of my Ionic application, here is what it contained:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"allowJs": true,
"module": "commonjs",
"outDir": "../out-tsc/spec",
"sourceMaps": true,
"target": "es5"
},
"files": [
"**/*.ts"
],
"include": [
"*.spec.ts",
"**/*.spec.ts",
"**/*.d.ts",
"test-config/*.ts",
"test-config/**/*.ts"
]
}