Skip to content

[BUG] Invalid execution of class inheritance by Test runner #21119

Closed

Description

@J-Sek

Context:

  • Playwright Version: 1.31.0
  • Operating System: Linux
  • Node.js version: 16.17.0
  • Browser: Chromium

Command: playwright test

Code Snippet

import { test, expect } from "@playwright/test";

interface IModel {
  id: number;
}

class Model implements IModel {
  id!: number;

  constructor(data?: IModel) {
    if (data) {
      for (var property in data) {
        if (data.hasOwnProperty(property))
          (<any>this)[property] = (<any>data)[property];
      }
    }
  }
}

interface IDemoA {
  id: number;
  name: string | null;
}

class DemoA extends Model implements IDemoA {
  name!: string | null;

  constructor(data?: IDemoA) {
    super(data);
  }
}

test("Minimal repro", () => {
  const a = new DemoA({
    id: 5,
    name: "My name",
  });

  console.log(a); // DemoA { id: 5, name: undefined }
  expect(a.name).toBe("My name");
});

Describe the bug

Code above consists mostly of classes and interfaces generated by NSwag. Wherever is extends the fields are set on this in super but are not exposed after we exit Model constructor.

This is especially weird because ts-node, TS REPL (and node for compiled JavaScript code) are executing the code without issues. The a.name contains expected value.

Activity

  1. yury-s commented on Feb 22, 2023

    @yury-s
    Member

    I can reproduce it.

  2. yury-s commented on Feb 22, 2023

    @yury-s
    Member

    Playwright transforms DemoA into this:

    class DemoA extends Model {
      constructor(data) {
        super(data);
        this.name = void 0;
      }
    }
  3. J-Sek commented on Feb 23, 2023

    @J-Sek
    Author

    Yury Semikhatsky (@yury-s) My tsconfig.json is configured for "target": "es2015", and the output in dist is plain and simple.

    image

    Where can I inspect this code with = void 0?

    And maybe by any chance you know whether it is described in docs or how to control this behavior?

  4. dgozman commented on Feb 28, 2023

    @dgozman
    Collaborator

    Turns out, this is a long-known issue between TypeScript and Babel. For example, see this issue: babel/babel#14291.

    TypeScript introduced the useDefineForClassFields flag that makes it compatible with the newest spec, see details here.

    We can probably allow declare modifiers, as outlined in the TypeScript release notes above. In this case a workaround would be declare name!: string | null that avoids generating this.name = void 0.

  5. dgozman commented on Mar 2, 2023

    @dgozman
    Collaborator

    With #21281, the workaround now is to use declare name: string. See comment above for details.

  6. mxschmitt commented on Mar 20, 2023

    @mxschmitt
    Contributor

    Fix got reverted since it regressed other behaviour. Keeping it open until a new fix is merged.

  7. mxschmitt commented on Mar 31, 2023

    @mxschmitt
    Contributor

    I filed babel/babel#15530 for it, since it looks like an upstream bug.

  8. J-Sek commented on Mar 31, 2023

    @J-Sek
    Author

    Thanks, I will keep track of the upstream issue.

    Unfortunately the workaround is not going to work for me (even if would be merged) as NSwag does not give the possiblity to generate TS with declare keyword in front of the field name. At least I can't see it in their configuration. And I would rather not write post-processing on generated 70 KLOC.

  9. pavelfeldman commented on Nov 17, 2023

    @pavelfeldman
    Member

    Why was this issue closed?

    Thank you for your contribution to our project. This issue has been closed due to its limited upvotes and recent activity, and insufficient feedback for us to effectively act upon. Our priority is to focus on bugs that reflect higher user engagement and have actionable feedback, to ensure our bug database stays manageable.

    Should you feel this closure was in error, please create a new issue and reference this one. We're open to revisiting it given increased support or additional clarity. Your understanding and cooperation are greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions