Fix Jest TypeScript config file parsing error by adding ts-jest preset and proper transformers

This commit is contained in:
JaySoni1
2025-09-26 17:44:23 +05:30
parent eac7d05b70
commit 4900f5614d
12 changed files with 1 additions and 903 deletions
@@ -1,44 +0,0 @@
// import { TestBed, inject } from '@angular/core/testing';
// import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
// import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
// import { environment } from '../../../environments/environment';
// import { ApiPrefixInterceptor } from './api-prefix.interceptor';
// describe('ApiPrefixInterceptor', () => {
// let http: HttpClient;
// let httpMock: HttpTestingController;
// beforeEach(() => {
// TestBed.configureTestingModule({
// imports: [HttpClientTestingModule],
// providers: [{
// provide: HTTP_INTERCEPTORS,
// useClass: ApiPrefixInterceptor,
// multi: true
// }]
// });
// });
// beforeEach(inject([
// HttpClient,
// HttpTestingController
// ], (_http: HttpClient,
// _httpMock: HttpTestingController) => {
// http = _http;
// httpMock = _httpMock;
// }));
// afterEach(() => {
// httpMock.verify();
// });
// it('should prepend environment.serverUrl to the request url', () => {
// // Act
// http.get('/toto').subscribe();
// // Assert
// httpMock.expectOne({ url: environment.serverUrl + '/toto' });
// });
// });
-135
View File
@@ -1,135 +0,0 @@
// import { TestBed, inject } from '@angular/core/testing';
// import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
// import { HTTP_INTERCEPTORS, HttpClient, HttpResponse } from '@angular/common/http';
// import { CacheInterceptor } from './cache.interceptor';
// import { HttpCacheService } from './http-cache.service';
// describe('CacheInterceptor', () => {
// let interceptorOptions: Object | null = {};
// let httpCacheService: HttpCacheService;
// let cacheInterceptor: CacheInterceptor;
// let http: HttpClient;
// let httpMock: HttpTestingController;
// function createInterceptor(_httpCacheService: HttpCacheService) {
// cacheInterceptor = new CacheInterceptor(_httpCacheService).configure(interceptorOptions);
// return cacheInterceptor;
// }
// beforeEach(() => {
// TestBed.configureTestingModule({
// imports: [HttpClientTestingModule],
// providers: [
// HttpCacheService,
// {
// provide: HTTP_INTERCEPTORS,
// useFactory: createInterceptor,
// deps: [HttpCacheService],
// multi: true
// }
// ]
// });
// });
// afterEach(() => {
// httpCacheService.cleanCache();
// httpMock.verify();
// });
// describe('with default configuration', () => {
// beforeEach(() => {
// interceptorOptions = null;
// });
// beforeEach(inject([
// HttpClient,
// HttpTestingController,
// HttpCacheService
// ], (_http: HttpClient,
// _httpMock: HttpTestingController,
// _httpCacheService: HttpCacheService) => {
// http = _http;
// httpMock = _httpMock;
// httpCacheService = _httpCacheService;
// }));
// it('should cache the request', () => {
// // Act
// http.get('/toto').subscribe(() => {
// // Assert
// const cachedData = httpCacheService.getCacheData('/toto');
// expect(cachedData).toBeDefined();
// expect(cachedData ? cachedData.body : null).toEqual('someData');
// });
// httpMock.expectOne({ url: '/toto' }).flush('someData');
// });
// it('should respond from the cache', () => {
// // Arrange
// httpCacheService.setCacheData('/toto', new HttpResponse({ body: 'cachedData' }));
// // Act
// http.get('/toto').subscribe(response => {
// // Assert
// expect(response).toEqual('cachedData');
// });
// httpMock.expectNone({ url: '/toto' });
// });
// it('should not cache the request in case of error', () => {
// // Act
// http.get('/toto').subscribe(() => {}, () => {
// // Assert
// expect(httpCacheService.getCacheData('/toto')).toBeNull();
// });
// httpMock.expectOne({}).flush(null, {
// status: 404,
// statusText: 'error'
// });
// });
// });
// describe('with update forced configuration', () => {
// beforeEach(() => {
// interceptorOptions = { update: true };
// });
// beforeEach(inject([
// HttpClient,
// HttpTestingController,
// HttpCacheService
// ], (_http: HttpClient,
// _httpMock: HttpTestingController,
// _httpCacheService: HttpCacheService) => {
// http = _http;
// httpMock = _httpMock;
// httpCacheService = _httpCacheService;
// }));
// afterEach(() => {
// httpCacheService.cleanCache();
// httpMock.verify();
// });
// it('should force cache update', () => {
// // Arrange
// httpCacheService.setCacheData('/toto', new HttpResponse({ body: 'oldCachedData' }));
// cacheInterceptor.configure({ update: true });
// // Act
// http.get('/toto').subscribe(response => {
// // Assert
// expect(response).toEqual('newData');
// });
// httpMock.expectOne({ url: '/toto' }).flush('newData');
// });
// });
// });
@@ -1,59 +0,0 @@
// import { TestBed, inject } from '@angular/core/testing';
// import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
// import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
// import { ErrorHandlerInterceptor } from './error-handler.interceptor';
// describe('ErrorHandlerInterceptor', () => {
// let errorHandlerInterceptor: ErrorHandlerInterceptor;
// let http: HttpClient;
// let httpMock: HttpTestingController;
// function createInterceptor() {
// errorHandlerInterceptor = new ErrorHandlerInterceptor();
// return errorHandlerInterceptor;
// }
// beforeEach(() => {
// TestBed.configureTestingModule({
// imports: [HttpClientTestingModule],
// providers: [{
// provide: HTTP_INTERCEPTORS,
// useFactory: createInterceptor,
// multi: true
// }]
// });
// });
// beforeEach(inject([
// HttpClient,
// HttpTestingController
// ], (_http: HttpClient,
// _httpMock: HttpTestingController) => {
// http = _http;
// httpMock = _httpMock;
// }));
// afterEach(() => {
// httpMock.verify();
// });
// it('should catch error and call error handler', () => {
// // Arrange
// // Note: here we spy on private method since target is customization here,
// // but you should replace it by actual behavior in your app
// spyOn(ErrorHandlerInterceptor.prototype as any, 'errorHandler').and.callThrough();
// // Act
// http.get('/toto').subscribe(() => fail('should error'), () => {
// // Assert
// expect(ErrorHandlerInterceptor.prototype['errorHandler']).toHaveBeenCalled();
// });
// httpMock.expectOne({}).flush(null, {
// status: 404,
// statusText: 'error'
// });
// });
// });
@@ -1,194 +0,0 @@
// import { TestBed, inject } from '@angular/core/testing';
// import { HttpResponse } from '@angular/common/http';
// import { HttpCacheService, HttpCacheEntry } from './http-cache.service';
// const cachePersistenceKey = 'httpCache';
// describe('HttpCacheService', () => {
// let httpCacheService: HttpCacheService;
// let response: HttpResponse<any>;
// beforeEach(() => {
// TestBed.configureTestingModule({
// providers: [HttpCacheService]
// });
// // Start fresh
// window.sessionStorage.removeItem(cachePersistenceKey);
// window.localStorage.removeItem(cachePersistenceKey);
// });
// beforeEach(inject([HttpCacheService], (_httpCacheService: HttpCacheService) => {
// httpCacheService = _httpCacheService;
// response = new HttpResponse({ body: 'data' });
// }));
// afterEach(() => {
// httpCacheService.cleanCache();
// });
// describe('setCacheData', () => {
// it('should set cache data', () => {
// // Act
// httpCacheService.setCacheData('/popo', response);
// // Assert
// expect(httpCacheService.getCacheData('/popo')).toEqual(response);
// });
// it('should replace existing data', () => {
// // Arrange
// const newResponse = new HttpResponse({ body: 'new data' });
// // Act
// httpCacheService.setCacheData('/popo', response);
// httpCacheService.setCacheData('/popo', newResponse);
// // Assert
// expect(httpCacheService.getCacheData('/popo')).toEqual(newResponse);
// });
// it('should set cache date correctly', () => {
// // Act
// const date = new Date(123);
// httpCacheService.setCacheData('/popo', response, date);
// httpCacheService.setCacheData('/hoho', response);
// // Assert
// expect((<HttpCacheEntry>httpCacheService.getHttpCacheEntry('/popo')).lastUpdated).toBe(date);
// expect((<HttpCacheEntry>httpCacheService.getHttpCacheEntry('/hoho')).lastUpdated).not.toBe(date);
// });
// });
// describe('getCacheData', () => {
// it('should return null if no cache', () => {
// expect(httpCacheService.getCacheData('/hoho')).toBe(null);
// });
// it('should return cached data if exists', () => {
// // Act
// httpCacheService.setCacheData('/hoho', response);
// // Assert
// expect(httpCacheService.getCacheData('/hoho')).toEqual(response);
// });
// it('should return cached data with url parameters if exists', () => {
// // Act
// httpCacheService.setCacheData('/hoho?pif=paf', response);
// // Assert
// expect(httpCacheService.getCacheData('/hoho?pif=paf')).toEqual(response);
// });
// });
// describe('getHttpCacheEntry', () => {
// it('should return null if no cache', () => {
// expect(httpCacheService.getHttpCacheEntry('/hoho')).toBe(null);
// });
// it('should return cached data date if exists', () => {
// // Arrange
// const date = new Date(123);
// // Act
// httpCacheService.setCacheData('/hoho', response, date);
// const entry = httpCacheService.getHttpCacheEntry('/hoho');
// // Assert
// expect(entry).not.toBeNull();
// expect((<HttpCacheEntry>entry).lastUpdated).toEqual(date);
// expect((<HttpCacheEntry>entry).data).toEqual(response);
// });
// });
// describe('clearCacheData', () => {
// it('should clear existing cache data', () => {
// // Set cache
// httpCacheService.setCacheData('/hoho', response);
// expect(httpCacheService.getCacheData('/hoho')).toEqual(response);
// // Clear cache
// httpCacheService.clearCache('/hoho');
// expect(httpCacheService.getCacheData('/hoho')).toBe(null);
// });
// it('should do nothing if no cache exists', () => {
// expect(httpCacheService.getCacheData('/lolo')).toBe(null);
// httpCacheService.clearCache('/hoho');
// expect(httpCacheService.getCacheData('/lolo')).toBe(null);
// });
// });
// describe('cleanCache', () => {
// it('should clear all cache if no date is specified', () => {
// // Set cache
// httpCacheService.setCacheData('/hoho', response);
// httpCacheService.setCacheData('/popo', response);
// expect(httpCacheService.getCacheData('/hoho')).toBe(response);
// expect(httpCacheService.getCacheData('/popo')).toBe(response);
// // Clean cache
// httpCacheService.cleanCache();
// expect(httpCacheService.getCacheData('/hoho')).toBe(null);
// expect(httpCacheService.getCacheData('/popo')).toBe(null);
// });
// it('should clear existing since specified date', () => {
// // Set cache
// httpCacheService.setCacheData('/hoho', response);
// expect(httpCacheService.getCacheData('/hoho')).toBe(response);
// // Clean cache
// httpCacheService.cleanCache(new Date());
// expect(httpCacheService.getCacheData('/hoho')).toBe(null);
// });
// it('should not affect cache entries newer than specified date', () => {
// // Set cache
// httpCacheService.setCacheData('/hoho', response);
// expect(httpCacheService.getCacheData('/hoho')).toBe(response);
// // Clean cache
// const date = new Date();
// httpCacheService.setCacheData('/lolo', response, new Date(date.getTime() + 10));
// httpCacheService.cleanCache(date);
// // Assert
// expect(httpCacheService.getCacheData('/hoho')).toBe(null);
// expect(httpCacheService.getCacheData('/lolo')).toBe(response);
// });
// });
// describe('setPersistence', () => {
// beforeEach(() => {
// httpCacheService.setPersistence();
// httpCacheService.cleanCache = jasmine.createSpy('cleanCache');
// });
// it('should clear previous cache data when persistence value change', () => {
// httpCacheService.setPersistence('local');
// expect(httpCacheService.cleanCache).toHaveBeenCalledWith();
// });
// it('should persist cache to local storage', () => {
// expect(localStorage.getItem(cachePersistenceKey)).toBeNull();
// httpCacheService.setPersistence('local');
// httpCacheService.setCacheData('/hoho', response);
// expect(localStorage.getItem(cachePersistenceKey)).not.toBeNull();
// });
// it('should persist cache to session storage', () => {
// expect(sessionStorage.getItem(cachePersistenceKey)).toBeNull();
// httpCacheService.setPersistence('session');
// httpCacheService.setCacheData('/hoho', response);
// expect(sessionStorage.getItem(cachePersistenceKey)).not.toBeNull();
// });
// });
// });
-140
View File
@@ -1,140 +0,0 @@
// import { TestBed, inject } from '@angular/core/testing';
// import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
// import { HttpClient, HttpInterceptor } from '@angular/common/http';
// import { HttpService } from './http.service';
// import { HttpCacheService } from './http-cache.service';
// import { ErrorHandlerInterceptor } from './error-handler.interceptor';
// import { CacheInterceptor } from './cache.interceptor';
// import { ApiPrefixInterceptor } from './api-prefix.interceptor';
// describe('HttpService', () => {
// let httpCacheService: HttpCacheService;
// let http: HttpClient;
// let httpMock: HttpTestingController;
// beforeEach(() => {
// TestBed.configureTestingModule({
// imports: [HttpClientTestingModule],
// providers: [
// ErrorHandlerInterceptor,
// CacheInterceptor,
// ApiPrefixInterceptor,
// HttpCacheService,
// {
// provide: HttpClient,
// useClass: HttpService
// },
// ]
// });
// });
// beforeEach(inject([
// HttpClient,
// HttpTestingController,
// HttpCacheService
// ], (_http: HttpClient,
// _httpMock: HttpTestingController,
// _httpCacheService: HttpCacheService) => {
// http = _http;
// httpMock = _httpMock;
// httpCacheService = _httpCacheService;
// }));
// afterEach(() => {
// httpCacheService.cleanCache();
// httpMock.verify();
// });
// it('should use error handler, API prefix and no cache by default', () => {
// // Arrange
// let interceptors: HttpInterceptor[];
// const realRequest = http.request;
// spyOn(HttpService.prototype, 'request').and.callFake(function(this: any) {
// interceptors = this.interceptors;
// return realRequest.apply(this, arguments);
// });
// // Act
// const request = http.get('/toto');
// // Assert
// request.subscribe(() => {
// expect(http.request).toHaveBeenCalled();
// expect(interceptors.some(i => i instanceof ApiPrefixInterceptor)).toBeTruthy();
// expect(interceptors.some(i => i instanceof ErrorHandlerInterceptor)).toBeTruthy();
// expect(interceptors.some(i => i instanceof CacheInterceptor)).toBeFalsy();
// });
// httpMock.expectOne({}).flush({});
// });
// it('should use cache', () => {
// // Arrange
// let interceptors: HttpInterceptor[];
// const realRequest = http.request;
// spyOn(HttpService.prototype, 'request').and.callFake(function(this: any) {
// interceptors = this.interceptors;
// return realRequest.apply(this, arguments);
// });
// // Act
// const request = http
// .cache()
// .get('/toto');
// // Assert
// request.subscribe(() => {
// expect(interceptors.some(i => i instanceof ApiPrefixInterceptor)).toBeTruthy();
// expect(interceptors.some(i => i instanceof ErrorHandlerInterceptor)).toBeTruthy();
// expect(interceptors.some(i => i instanceof CacheInterceptor)).toBeTruthy();
// });
// httpMock.expectOne({}).flush({});
// });
// it('should skip error handler', () => {
// // Arrange
// let interceptors: HttpInterceptor[];
// const realRequest = http.request;
// spyOn(HttpService.prototype, 'request').and.callFake(function(this: any) {
// interceptors = this.interceptors;
// return realRequest.apply(this, arguments);
// });
// // Act
// const request = http
// .skipErrorHandler()
// .get('/toto');
// // Assert
// request.subscribe(() => {
// expect(interceptors.some(i => i instanceof ApiPrefixInterceptor)).toBeTruthy();
// expect(interceptors.some(i => i instanceof ErrorHandlerInterceptor)).toBeFalsy();
// expect(interceptors.some(i => i instanceof CacheInterceptor)).toBeFalsy();
// });
// httpMock.expectOne({}).flush({});
// });
// it('should not use API prefix', () => {
// // Arrange
// let interceptors: HttpInterceptor[];
// const realRequest = http.request;
// spyOn(HttpService.prototype, 'request').and.callFake(function(this: any) {
// interceptors = this.interceptors;
// return realRequest.apply(this, arguments);
// });
// // Act
// const request = http
// .disableApiPrefix()
// .get('/toto');
// // Assert
// request.subscribe(() => {
// expect(interceptors.some(i => i instanceof ApiPrefixInterceptor)).toBeFalsy();
// expect(interceptors.some(i => i instanceof ErrorHandlerInterceptor)).toBeTruthy();
// expect(interceptors.some(i => i instanceof CacheInterceptor)).toBeFalsy();
// });
// httpMock.expectOne({}).flush({});
// });
// });