Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[swc-angular-plugin] Issue with inherited classes #376

Open
santiagof4 opened this issue May 8, 2024 · 1 comment
Open

[swc-angular-plugin] Issue with inherited classes #376

santiagof4 opened this issue May 8, 2024 · 1 comment
Labels
bug Something isn't working swc-angular

Comments

@santiagof4
Copy link

Hello!

First of all, thanks a lot for the effort! This plugin has already incredible results in our huge project with more than 2000 tests.

The issue

When running our tests with plain Jest, everything works (but super slow 😉), but when running our tests with swc-angular this issue arises:

Looks like classes that extend from other class lose values of those properties that are not present in the parent class.

Example:

interface IModelBase {
  id?: string;
  name: string;
}

class ModelBase implements IModelBase {
  id?: string;
  name: string;

  constructor(model: Partial<IModelBase>) {
    Object.assign(this, model);
  }
}

interface IModel {
  version: number;
}

export class Model extends ModelBase implements IModel {
  version: number;

  constructor(model: Partial<IModel & IModelBase>) {
    super(model);
  }
}

When testing something with that class, version is always undefined:

import { Model } from './model';

describe("failing class tests", () => {
  it('should init class with all the values', () => {
    const model = new Model({ id: '123', name: 'test', version: 1 });

    expect(model.id).toEqual('123');
    expect(model.name).toEqual('test');
    expect(model.version).toEqual(1); // This fails, 'version' is undefined
  })
})

This also works with Jasmine and in normal runtime.

Reproduction

https://github.com/santiagof4/jest-angular-swc-class-reproduction

  1. clone package
  2. npm install
  3. nx test
  4. Test in https://github.com/santiagof4/jest-angular-swc-class-reproduction/blob/main/src/app/failing-test.spec.ts fails.
@pumano
Copy link
Contributor

pumano commented Dec 1, 2024

code above transpiled to:

class ModelBase {
    constructor(model) {
        Object.assign(this, model);
    }
}
export class Model extends ModelBase {
    constructor(model) {
        super(model);
    }
}

see swc playground

when reproduce transpiled code and use it with test:

describe("failing class tests", () => {
  it('should init class with all the values', () => {
    const model = new Model({ id: '123', name: 'test', version: 1 });

    expect(model.id).toEqual('123');
    expect(model.name).toEqual('test');
    expect(model.version).toEqual(1); // This fails, 'version' is undefined
  })
})

it works as expected, but when use with classes and interfaces, it not working any more and fails on "version": 1

Try to dig to it soon @jahusa02

UPD:
when I try to run debugger and set breakpoint, under the hood it looks like it transformed using @swc/helpers to something like:

function Model(model) {        
 _class_call_check(this, Model);
 var _this;       
 _this = _call_super(this, Model, [
 model
 ]), _define_property(_this, "version", void 0);
  return _this;    
}

and that comes to incorrect result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working swc-angular
Projects
None yet
Development

No branches or pull requests

3 participants