emberjs/ember-test-helpers

element doesn't exist on type TestContext

eugenioenko opened this issue · 2 comments

After this PRs have been merged
#1302
related to this one
#1301

The TextContext type doesn't have an element property defined.

module('Integration | Helper | helper', function (hooks) {
  setupRenderingTest(hooks);
  test('it renders', async function (assert) {
    await render(hbs`something`);
     /* 
      type warning here, property element doesnt exist in TestContext 
     */
    assert.equal(this.element?.textContent?.trim(), 'something');
  });
});

The issue seems to be that TestContext inherits from BaseContext and neither define an element property.
Before the PR 1302 was merged the BaseContext was a dictionary {[key: string]: unknown} and so element could be defined but without unknown as type. Now that BaseContext has been switched to an object, element is not a property of object

That's correct! The main TestContext doesn’t have that on it, so this was a deeply unsafe type which could (and did) lead people astray. See the ember-qunit v6.1 migration guide for a discussion of a few options here, including explicitly specifying the (unsafe, but at least safer!) this: RenderingTestContext parameter and switching to qunit-dom—the latter being the recommended path. (There's a very useful codemod to convert your tests to using qunit-dom; we ran it across the millions of lines of tests for LinkedIn.com a few years ago and haven't looked back!)

I see, thank you for the clarification and the provided solutions, really appreciated 👍