angular: Cannot set signal input in tests with non-standard setup (Stackblitz/Jest preset without JIT transform) - NG0303
Which @angular/* package(s) are the source of the bug?
core
Is this a regression?
No
Description
In unit tests using this function to set signal based inputs
fixture = TestBed.createComponent(ParagraphBlockComponent);
component = fixture.componentInstance;
fixture.componentRef.setInput('title', 'my title');
results in error
NG0303: Can't set value of the 'title' input on the 'ParagraphBlockComponent' component. Make sure that the 'title' property is annotated with @Input() or a mapped @Input('title') exists.
this is the component:
@Component({
standalone: true,
selector: 'paragraph-block',
templateUrl: './paragraph-block.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ParagraphBlockComponent {
title = input<string>();
}
Please provide a link to a minimal reproduction of the bug
No response
Please provide the exception or error you saw
NG0303: Can't set value of the 'title' input on the 'ParagraphBlockComponent' component. Make sure that the 'title' property is annotated with @Input() or a mapped @Input('title') exists.
Please provide the environment you discovered this bug in (run ng version
)
Angular 17.1.0
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 5 months ago
- Reactions: 2
- Comments: 21 (12 by maintainers)
Thanks @TarsisDragomir, no worries. Thank you for giving feedback early. I’m in contact with the Jest preset maintainer to help enable signal inputs.
I have exactly same issue when using the new signal input api that 17.1 introduced. Using jest for testing but cannot use componentRef.setInput anymore (get same error as reporter of this issue). I am using ‘alias’; tried both the name and the alias but none of them work. It really looks that it fails because it no longer has the
@Input()
annotation. Somebody changed this thread to a stackblitz issue, but not sure if that is correct. Or maybe I am missing something that I should have upgraded/changed also after going to 17.1. Any help is welcome. Thanks!$project = input<Project>(getEmptyProject(), { alias: 'project' });
I never know how Stackblitz’ compilation pipeline works (the legacy one, without Webcontainers), but the behavior is expected if the custom JIT transform that was added in 17.1 is not being used.
@dapperdandev The official
jest
builder should have worked since we launched signal inputs. Do you see any issues? If so, please create an issue and link here.Closing because:
https://github.com/thymikee/jest-preset-angular/pull/2303 https://stackblitz.com/edit/ng-jasmine-wec9ye?file=src%2Fapp%2Fapp.component.ts
You also need to update jest to the latest version, in v 29.5.x this issue still occurs.
The issue has been fixed for Jest: https://github.com/thymikee/jest-preset-angular/issues/2246
You’re right, the same test does pass locally.
@JoostK I can reproduce the issue on stackblitz : https://stackblitz.com/edit/ng-jasmine?file=src%2Fapp%2Fapp.component.spec.ts