Registry / testing / ngx-cva-test-suite

ngx-cva-test-suite

JSON →
library2.0.1jsnpmunverified

ngx-cva-test-suite is an Angular testing utility designed to streamline and standardize the process of unit testing custom form components that implement `ControlValueAccessor` (CVA). Currently at version 2.0.1, this library provides a comprehensive set of pre-defined test cases, ensuring that custom controls correctly handle essential CVA behaviors such as `onChange` and `onTouched` calls, disabled states, and `reset()` functionality, which are common sources of errors in CVA implementations. It is compatible with both Jest and Jasmine test runners and offers various configuration options to accommodate diverse component structures. Releases for this library typically align with major Angular version updates, with patch releases addressing bug fixes. Its key differentiator lies in its robust, pre-built test suite that reduces boilerplate and potential oversight when developing complex, custom form controls, aiming for extensive unit test coverage.

npm install ngx-cva-test-suite
INSTALL
IMPORT
SIG · NGX-CVA-TEST-SUITE
N
ngx-cva-test-suite
testingjavascriptv2.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

runValueAccessorTests
import { runValueAccessorTests } from 'ngx-cva-test-suite';
const { runValueAccessorTests } = require('ngx-cva-test-suite');
This is the primary function to invoke the CVA test suite. Designed for ESM in modern Angular projects.

This example demonstrates how to set up `ngx-cva-test-suite` for an Angular component implementing `ControlValueAccessor` and basic validation, showing the component definition alongside its test suite invocation.

import { runValueAccessorTests } from 'ngx-cva-test-suite'; import { Component, Input, Output, EventEmitter, forwardRef } from '@angular/core'; import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, Validator, AbstractControl, ValidationErrors } from '@angular/forms'; @Component({ selector: 'app-combobox', template: `<input class="combobox-input" [value]="value" (input)="onInput($event)" (blur)="onBlur()">`, providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => ComboboxComponent), multi: true }, { provide: NG_VALIDATORS, useExisting: forwardRef(() => ComboboxComponent), multi: true } ] }) export class ComboboxComponent implements ControlValueAccessor, Validator { value: string = ''; onChange: any = () => {}; onTouched: any = () => {}; @Input() options: string[] = []; @Output() valueChange = new EventEmitter<string>(); writeValue(value: any): void { this.value = value || ''; } registerOnChange(fn: any): void { this.onChange = fn; } registerOnTouched(fn: any): void { this.onTouched = fn; } setDisabledState(isDisabled: boolean): void { // Implement logic to disable/enable the input } onInput(event: Event): void { const newValue = (event.target as HTMLInputElement).value; this.value = newValue; this.onChange(newValue); this.valueChange.emit(newValue); } onBlur(): void { this.onTouched(); } setValue(value: string, emitEvent: boolean = false): void { this.value = value; if (emitEvent) { this.onChange(value); this.valueChange.emit(value); } } validate(control: AbstractControl): ValidationErrors | null { return null; // Example validation } } // In your test file (e.g., combobox.component.spec.ts) runValueAccessorTests({ component: ComboboxComponent, testModuleMetadata: { declarations: [ComboboxComponent], }, supportsOnBlur: true, nativeControlSelector: 'input.combobox-input', internalValueChangeSetter: (fixture, value) => { fixture.componentInstance.setValue(value, true); }, getComponentValue: (fixture) => fixture.componentInstance.value, // Optional: Add a custom name for the test suite in the console output name: 'ComboboxComponent CVA Tests' });
Debug
Known issues
breakingVersion 2.0.0 of `ngx-cva-test-suite` increased its minimum peer dependency for `@angular/core`, `@angular/forms`, and `@angular/platform-browser` to `^12.0.0`. Projects using Angular versions older than 12.0.0 must remain on `ngx-cva-test-suite` v1.x.x.
fix
Upgrade your Angular project to version 12 or newer, or downgrade `ngx-cva-test-suite` to a v1.x.x release compatible with your current Angular version.
affects: >=2.0.0
gotchaIncorrect configuration of `nativeControlSelector`, `internalValueChangeSetter`, or `getComponentValue` can lead to false test failures or incomplete test coverage. These properties must accurately reflect the internal structure and behavior of the `ControlValueAccessor` component being tested.
fix
Carefully review your component's template and TypeScript logic to ensure that `nativeControlSelector` targets the correct input element, `internalValueChangeSetter` properly simulates a value change within the component, and `getComponentValue` correctly extracts the component's internal value. Refer to the library's GitHub examples for guidance.
affects: >=1.0.0
gotchaThe `testModuleMetadata` property, which mimics `TestBed.configureTestingModule`, must correctly declare and import all necessary modules, components, and providers required by the component under test. Missing declarations or imports will result in Angular runtime errors during testing.
fix
Ensure `testModuleMetadata.declarations` includes the component being tested and any its direct dependencies. Also, add necessary `imports` (e.g., `ReactiveFormsModule` or `FormsModule` if your CVA interacts with forms directly) and `providers` (e.g., for `NG_VALUE_ACCESSOR`, `NG_VALIDATORS`).
affects: >=1.0.0
gotchaThe `supportsOnBlur` flag is critical for components that differentiate between `onChange` and `onTouched` calls (e.g., for `updateOn: 'blur'` strategy). Setting it incorrectly can lead to tests failing to assert `onTouched` behavior or causing unintended `onChange` emissions.
fix
Set `supportsOnBlur: true` if your component explicitly handles blur events to trigger `onTouched`. Ensure `nativeControlSelector` is also correctly specified when `supportsOnBlur` is true to allow the test suite to simulate blur events on the correct element.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Can't resolve 'ngx-cva-test-suite'
The package is not installed or the import path is incorrect.
fix
Run `npm install ngx-cva-test-suite --save-dev` or `yarn add ngx-cva-test-suite --dev`. Ensure the import statement `import { runValueAccessorTests } from 'ngx-cva-test-suite';` is correct.
TypeError: (0 , ngx_cva_test_suite__WEBPACK_IMPORTED_MODULE_0__.runValueAccessorTests) is not a function
This usually indicates a CommonJS `require()` syntax is used in an ESM-only context or an incorrect named import.
fix
Ensure you are using `import { runValueAccessorTests } from 'ngx-cva-test-suite';` for ESM environments, which is standard for modern Angular projects. Avoid `const { runValueAccessorTests } = require('ngx-cva-test-suite');`.
Package '@angular/core' is not compatible with 'ngx-cva-test-suite@2.0.1'. Package '@angular/core' must be version '>=12.0.0'.
Your Angular project's version of `@angular/core` is older than the minimum required by `ngx-cva-test-suite` v2.
fix
Update your Angular project's dependencies to Angular v12.0.0 or higher, or downgrade `ngx-cva-test-suite` to a v1.x.x version that supports your current Angular version.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
@angular/corerequiredRequired for Angular component testing infrastructure.
@angular/formsrequiredProvides the ControlValueAccessor interface and form control utilities.
@angular/platform-browserrequiredNeeded for browser-based Angular application setup and testing.
Agent activity
4 hits · last 30 days
node
4
Resources
ngx-cva-test-suite — npm install ngx-cva-test-suite · libregistry