{"record":{"id":"66756635a296f4b4","repo":"angular/components","slug":"this-testelement-was-not-created-by-the-testbedhar","errorCode":null,"errorMessage":"This TestElement was not created by the TestbedHarnessEnvironment","messagePattern":"This TestElement was not created by the TestbedHarnessEnvironment","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cdk/testing/testbed/testbed-harness-environment.ts","lineNumber":144,"sourceCode":"  }\n\n  /**\n   * Creates a `HarnessLoader` at the document root. This can be used if harnesses are\n   * located outside of a fixture (e.g. overlays appended to the document body).\n   */\n  static documentRootLoader(\n    fixture: ComponentFixture<unknown>,\n    options?: TestbedHarnessEnvironmentOptions,\n  ): HarnessLoader {\n    return new TestbedHarnessEnvironment(document.body, fixture, options);\n  }\n\n  /** Gets the native DOM element corresponding to the given TestElement. */\n  static getNativeElement(el: TestElement): Element {\n    if (el instanceof UnitTestElement) {\n      return el.element;\n    }\n    throw Error('This TestElement was not created by the TestbedHarnessEnvironment');\n  }\n\n  /**\n   * Creates an instance of the given harness type, using the fixture's root element as the\n   * harness's host element. This method should be used when creating a harness for the root element\n   * of a fixture, as components do not have the correct selector when they are created as the root\n   * of the fixture.\n   */\n  static async harnessForFixture<T extends ComponentHarness>(\n    fixture: ComponentFixture<unknown>,\n    harnessType: ComponentHarnessConstructor<T>,\n    options?: TestbedHarnessEnvironmentOptions,\n  ): Promise<T> {\n    const environment = new TestbedHarnessEnvironment(fixture.nativeElement, fixture, options);\n    await environment.forceStabilize();\n    return environment.createComponentHarness(harnessType, fixture.nativeElement);\n  }\n","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/angular/components/blob/0411926e7d8ae06b32236ec1048a888cfad5abf2/src/cdk/testing/testbed/testbed-harness-environment.ts#L126-L162","documentation":"TestbedHarnessEnvironment.getNativeElement only unwraps UnitTestElement instances it created itself. Passing a TestElement produced by a different environment (e.g. TestbedHarnessEnvironment from another module injector or a non-testbed environment) fails the instanceof check. The library throws instead of returning a wrong/undefined DOM node.","triggerScenarios":"Calling TestbedHarnessEnvironment.getNativeElement(el) where el was created by another harness environment implementation (e.g. Selenium/Playwright environment, or a UnitTestElement from a different Angular testing module instance).","commonSituations":"Mixing harness environments across test bundles; sharing TestElement instances between Karma testbed tests and e2e-style environments; multiple copies of @angular/cdk/testing bundled in one app causing instanceof to fail across realms.","solutions":["Only pass TestElement instances obtained from the same TestbedHarnessEnvironment (harness.host or environment-level APIs)","Use harness.host() and await it to get the environment-created TestElement instead of constructing elements manually","Ensure a single copy of @angular/cdk is installed/bundled (check for duplicate @angular/cdk versions in node_modules)","Use (el as any).element only if you are certain the element is a UnitTestElement, with a guard"],"exampleFix":"// before\nconst el = someOtherEnv.getTestElement(nativeEl);\nconst dom = TestbedHarnessEnvironment.getNativeElement(el); // throws\n// after\nconst harness = await loader.getHarness(MyCompHarness);\nconst testEl = await harness.host();\nconst dom = TestbedHarnessEnvironment.getNativeElement(testEl); // ok","handlingStrategy":"type-guard","validationCode":"import { UnitTestElement } from '@angular/cdk/testing/testbed';\nif (!(el instanceof UnitTestElement)) {\n  throw new Error('Pass a TestElement created by TestbedHarnessEnvironment');\n}","typeGuard":"import { UnitTestElement } from '@angular/cdk/testing/testbed';\nimport { TestElement } from '@angular/cdk/testing';\nfunction isUnitTestElement(el: TestElement): el is UnitTestElement {\n  return el instanceof UnitTestElement;\n}","tryCatchPattern":"try {\n  const native = TestbedHarnessEnvironment.getNativeElement(el);\n} catch (e) {\n  if ((e as Error).message.includes('not created by the TestbedHarnessEnvironment')) {\n    throw new Error('Obtain the element from the same harness environment');\n  }\n  throw e;\n}","preventionTips":["Always get TestElements via harness.host() from the same environment","Avoid mixing CDK testing environments in one test bundle","Check for duplicate @angular/cdk copies in node_modules","Never construct TestElement implementations by hand for getNativeElement"],"tags":["testing","angular-cdk","type-mismatch"],"backgroundTag":"element-wrong-environment","analyzedSha":"0411926e7d8ae06b32236ec1048a888cfad5abf2","analyzedAt":"2026-08-31T11:58:23.400Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}