Directive unit testing fails

Issue I am using jest.js for testing with my angular app. here is the directive I use in html: <textarea errorHighlighter formControlName=”Url” name=”Url” cols=”50″ rows=”5″ placeholder=”Enter Page URL” (ngModelChange)=”pageUrlChanges($event)”></textarea> here is my directive.ts file: import { Directive, ElementRef, SimpleChanges, HostListener,

Continue reading

Angular ngrx test with jest throw error ` console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29`

Issue I do the testing for my ngrx/store with jestjs. i got the testing passing result. eventhough getting an error like: console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29 Error: Cross origin http://localhost forbidden at dispatchError (C:\722333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:60:19) at Object.validCORSHeaders (C:\722333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xhr-utils.js:72:5) at receiveResponse (C:\722333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:842:21) at Request.<anonymous> (C:\722333\USD\Project\AOS.UI\NG-USD\node_modules\jsdom\lib\jsdom\living\xmlhttprequest.js:677:38)

Continue reading

Testing Angular component extending an abstract class

Issue I have this setup for a number of components: @Directive() export abstract class BaseComponent implements OnInit { @Select(PortalState.portal) public portalState: Observable<PortalModel>; public portal: PortalModel; protected ngUnsubscribe: Subject<void> = new Subject(); constructor( protected someService: SomeService, protected route: ActivatedRoute ){ }

Continue reading

Jest test fails with Unexpected token, expected ";"

Issue I have a Node project using Typescript and Jest. Currently I have this project structure With this tsconfig.json file “compilerOptions”: { “target”: “ES2017”, “module”: “commonjs”, “allowJs”: true, “outDir”: “dist”, “rootDir”: “src”, “strict”: true, “moduleResolution”: “node”, “esModuleInterop”: true } this

Continue reading

Jest test assertion, expect a nested object

Issue When I do the below expect in jest with native test expect(mockOnChange).toHaveBeenCalledWith({ order_type: expect.arrayContaining([‘1’]), number: ”, ouser_ids: expect.arrayContaining([‘user_id1’]), payment_type: expect.arrayContaining([‘2’]), start_date: old_Date, end_date: current_date, state: ‘all’, }); I’m getting the following error Object { "end_date": "2022/06/07", "number": "", –

Continue reading

Test unable to run test cases exits with TypeError: Jest: a transform must export a `process` function

Issue { //tsconfig.base.json "compileOnSave": false, "compilerOptions": { "baseUrl": "./", "outDir": "./dist/out-tsc", "sourceMap": true, "declaration": false, "downlevelIteration": true, "experimentalDecorators": true, "module": "esnext", "moduleResolution": "node", "importHelpers": true, "target": "es2015", "typeRoots": ["node_modules/@types"], "lib": ["es2018", "dom"] }, "angularCompilerOptions": { "fullTemplateTypeCheck": true, "strictInjectionParameters": true }

Continue reading

tsconfig problems with test and compiling

Issue This is my tsconfig.json { "compilerOptions": { "noImplicitAny": true, "declaration": false, "strict": true, "strictNullChecks": false, "target": "ES2019", "module": "commonjs", "resolveJsonModule": true, "allowJs": true, "checkJs": false, "outDir": "dist", "esModuleInterop": true, "inlineSourceMap": true, "rootDir": "./", "baseUrl": "./", "typeRoots": [ "./node_modules/@types" ],

Continue reading

Jest Spy not being called

Issue I am trying to run a test using the winston logger package. I want to spy on the createlogger function and assert that it is being called with the correct argument. Logger.test.ts import { describe, expect, it, jest, beforeEach,

Continue reading

Jest/Rxjs – timeout when calling for subscription from a getter

Issue I have a service that updates an Rxjs Subject whenever a method in the service is called: @Injectable() export class AppAlertService implements IAppAlertService { private readonly _alertBehaviourSubject: Subject<IAlertConfiguration> = new Subject<IAlertConfiguration>(); public get alertConfiguration(): Observable<IAlertConfiguration> { return this._alertBehaviourSubject.asObservable(); }

Continue reading

How to test a http request function in a service with Angular and Jest

Issue I created a simple service returning a json object. getCountries() { return this.httpService.get(‘../../assets/countries.json’); } The test describe(‘TestServiceService’, () => { let service: TestServiceService; let httpController: HttpTestingController; beforeEach(() => { TestBed.configureTestingModule({ imports: [HttpClientTestingModule] }); httpController = TestBed.inject(HttpTestingController); service = TestBed.inject(TestServiceService);

Continue reading

Watch and rerun Jest JS tests

Issue The Jest documentation suggests using npm test to execute tests. Is there a way of watching your source and tests to rerun Jest tests automatically when relevant files have been changed? Solution Thanks to Erin Stanfill for pointing out,

Continue reading

Testing with 'toHaveStyle' passes when expecting fail

Issue I’m learning about testing and react. I’m having a little trouble understanding why a test is passing when it should not: From App.js <button style={{backgroundColor: ‘gray’}}> Gray button </button> From App.test.js expect(colorButton).toHaveStyle( {backgroundColor: ‘gray’ }); // passes ok expect(colorButton).toHaveStyle(

Continue reading

How to mock AxiosInstance?

Issue I have axios with following HttpClient class export default class HttpClient { constructor(baseUrl: string) { const axiosInstance = axios.create({ validateStatus(status: number) { return status === 200 || status === 201; }, }); axiosInstance.interceptors.request.use((config) => { if (AuthUtil.getAuthHeader()) config.headers =

Continue reading

aws cdk 2.0 init app fails to build with prettier issues, which is from jest-snapshot

Issue node: v16.7.0 cdk: 2.0.0 (build 4b6ce31) package.json: "devDependencies": { "@types/jest": "^26.0.10", "@types/node": "10.17.27", "aws-cdk": "2.0.0", "jest": "^26.4.2", "ts-jest": "^26.2.0", "ts-node": "^9.0.0", "typescript": "^3.9.7" }, "dependencies": { "aws-cdk-lib": "2.0.0", "constructs": "^10.0.0", "source-map-support": "^0.5.16" } npm run build/watch stack trace: node_modules/@types/prettier/index.d.ts:41:54

Continue reading

Query a simple button with jest and

Issue I’m trying to test my component, so here it’s : import React from ‘react’; import { useNavigate } from ‘react-router-dom’; import ‘../styles/mainPageStyles.css’; const MainPage = (): React.ReactElement => { const navigate = useNavigate(); const handleBeginQuiz = () => {

Continue reading

How to test NestJs response interceptor

Issue I tried to follow this thread but it I keep getting an error. transform-response.interceptor.ts: import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from ‘@nestjs/common’; import { Observable } from ‘rxjs’; import { map } from ‘rxjs/operators’; import { ApiResponseInterface }

Continue reading