Title: Template of fixture is not updated using signals (in test) · Issue #56863 · angular/angular · GitHub
Open Graph Title: Template of fixture is not updated using signals (in test) · Issue #56863 · angular/angular
X Title: Template of fixture is not updated using signals (in test) · Issue #56863 · angular/angular
Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? Yes Description In the following test scenario: describe('Test with input', () => { @Component({ standalone: true, template: `{{ htmlData }}`, changeDetect...
Open Graph Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? Yes Description In the following test scenario: describe('Test with input', () => { @Component({ standalone: true, ...
X Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? Yes Description In the following test scenario: describe('Test with input', () => { @Component({ standal...
Opengraph URL: https://github.com/angular/angular/issues/56863
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Template of fixture is not updated using signals (in test)","articleBody":"### Which @angular/* package(s) are the source of the bug?\n\ncore\n\n### Is this a regression?\n\nYes\n\n### Description\n\nIn the following test scenario:\r\n```\r\ndescribe('Test with input', () => {\r\n\t@Component({\r\n\t\tstandalone: true,\r\n\t\ttemplate: `{{ htmlData }}`,\r\n\t\tchangeDetection: ChangeDetectionStrategy.OnPush,\r\n\t})\r\n\tclass TestComponentWithInput {\r\n\t\tdata = input\u003cstring>()\r\n\t\thtmlData?: string\r\n\t\tconstructor() {\r\n\t\t\teffect(() => {\r\n\t\t\t\tthis.htmlData = this.data()\r\n\t\t\t})\r\n\t\t}\r\n\t}\r\n\tit('#1 should have \"foobar\" in template', () => {\r\n\t\tconst fixture = TestBed.configureTestingModule({\r\n\t\t\timports: [TestComponentWithInput],\r\n\t\t}).createComponent(TestComponentWithInput)\r\n\t\tfixture.detectChanges() // \u003c---- This is necessary\r\n\t\tfixture.componentRef.setInput('data', 'foobar')\r\n\t\tfixture.detectChanges()\r\n\r\n\t\texpect(fixture.debugElement.nativeElement.textContent).toContain('foobar')\r\n\t})\r\n\r\n\tit('#2 should have \"foobar\" in template', () => {\r\n\t\tconst fixture = TestBed.configureTestingModule({\r\n\t\t\timports: [TestComponentWithInput],\r\n\t\t}).createComponent(TestComponentWithInput)\r\n\t\t//fixture.detectChanges() // \u003c---- Template is not updated because of missing detectChanges\r\n\t\tfixture.componentRef.setInput('data', 'foobar')\r\n\t\tfixture.detectChanges()\r\n\r\n\t\texpect(fixture.debugElement.nativeElement.textContent).toContain('foobar')\r\n\t})\r\n})\r\n```\r\nOnly test no. 1 does work and test no. 2 will fail. Apparently the `fixture.detectChanges()` before setting the input is needed. Otherwise the template does not get updated (See test no. 2).\\\r\nIs this the correct behaviour or is this a bug?\r\nIf this is the expected behaviour, how can i test a required signal input, like in the following scenario:\r\n```\r\ndescribe('Test with required input', () => {\r\n\t@Component({\r\n\t\tstandalone: true,\r\n\t\ttemplate: `{{ htmlData }}`,\r\n\t\tchangeDetection: ChangeDetectionStrategy.OnPush,\r\n\t})\r\n\tclass TestComponentWithRequiredInput {\r\n\t\tdata = input.required\u003cstring>()\r\n\t\thtmlData?: string\r\n\t\tconstructor() {\r\n\t\t\teffect(() => {\r\n\t\t\t\tthis.htmlData = this.data()\r\n\t\t\t})\r\n\t\t}\r\n\t}\r\n\tit('#1 should have \"foobar\" in template', () => {\r\n\t\tconst fixture = TestBed.configureTestingModule({\r\n\t\t\timports: [TestComponentWithRequiredInput],\r\n\t\t}).createComponent(TestComponentWithRequiredInput)\r\n\t\tfixture.detectChanges() // \u003c---- This is necessary but throws error\r\n\t\tfixture.componentRef.setInput('data', 'foobar')\r\n\t\tfixture.detectChanges()\r\n\r\n\t\texpect(fixture.debugElement.nativeElement.textContent).toContain('foobar')\r\n\t})\r\n\r\n\tit('#2 should have \"foobar\" in template (but i am broken)', () => {\r\n\t\tconst fixture = TestBed.configureTestingModule({\r\n\t\t\timports: [TestComponentWithRequiredInput],\r\n\t\t}).createComponent(TestComponentWithRequiredInput)\r\n\t\t//fixture.detectChanges() // \u003c---- Template is not updated because of missing detectChanges\r\n\t\tfixture.componentRef.setInput('data', 'foobar')\r\n\t\tfixture.detectChanges()\r\n\r\n\t\texpect(fixture.debugElement.nativeElement.textContent).toContain('foobar')\r\n\t})\r\n})\r\n```\r\nIn this scenario both of the tests will fail. The test no. 1 fails (and throws an the error `NG0950: Input is required but no value is available yet`) because i need to set the input before using `fixture.detectChanges()` and the test no. 2 does fail because i have not used `fixture.detectChanges` before setting the input and therefore the template is not updated.\\\r\n\r\nI hope i was able to describe my dilemma :)\n\n### Please provide a link to a minimal reproduction of the bug\n\n_No response_\n\n### Please provide the exception or error you saw\n\n_No response_\n\n### Please provide the environment you discovered this bug in (run `ng version`)\n\n```true\nAngular CLI: 18.0.4\r\nNode: 20.12.2\r\nPackage Manager: npm 10.3.0\r\nOS: darwin arm64\r\n\r\nAngular: 18.0.3\r\n... animations, cdk, common, compiler, compiler-cli, core, forms\r\n... language-service, material, platform-browser\r\n... platform-browser-dynamic, platform-server, router\r\n... service-worker\r\n\r\nPackage Version\r\n---------------------------------------------------------\r\n@angular-devkit/architect 0.1703.8\r\n@angular-devkit/build-angular 18.0.4\r\n@angular-devkit/core 18.0.4\r\n@angular-devkit/schematics 18.0.4\r\n@angular/cli 18.0.4\r\n@angular/ssr 18.0.4\r\n@schematics/angular 18.0.4\r\nng-packagr 18.0.0\r\nrxjs 7.8.1\r\ntypescript 5.4.4\r\nwebpack 5.92.0\r\nzone.js 0.14.2\n```\n\n\n### Anything else?\n\n_No response_","author":{"url":"https://github.com/Undestor","@type":"Person","name":"Undestor"},"datePublished":"2024-07-05T07:53:10Z","url":"https://github.com/angular/angular/issues/56863"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:82a02851-b843-1a41-0c47-72a80b2db7ee |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BF68:76F02:37E9C28:47823C6:6B01F07C |
| html-safe-nonce | 38f7a32345214f77795213bc4ccce3c7cbe6ee62137ca7eb6d7d3eb86c072981 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRjY4Ojc2RjAyOjM3RTlDMjg6NDc4MjNDNjo2QjAxRjA3QyIsInZpc2l0b3JfaWQiOiI2MTEzMDIwMjg4NDAyODQ1ODIwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 846453993f300aee59e75479cb04947f09cc4714fbc73a75ebfb6a9ee4598363 |
| hovercard-subject-tag | issue:2392057664 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/56863/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a2b7c309969c33040d589116fc62774edc932a7c7cfbdd14a05b21ac58c3be36/angular/angular/issues/56863 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a2b7c309969c33040d589116fc62774edc932a7c7cfbdd14a05b21ac58c3be36/angular/angular/issues/56863 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? core Is this a regression? Yes Description In the following test scenario: describe('Test with input', () => { @Component({ standalone: true, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Undestor |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width