efate icon indicating copy to clipboard operation
efate copied to clipboard

Object created lost its constructor

Open Charly-xepelin opened this issue 5 months ago • 3 comments

The following example shows that the object created by the fixture factory does not carry object type identity.

import { defineFixture } from 'efate';

class Sample {
  constructor(public type: string) {}
}

describe('fixture', () => {
  it('should create object with type', () => {
    const sampleFixture = defineFixture<Sample>(t => {
      t.type.asString();
    });

    const sample = sampleFixture.create();

    expect(sample).toBeInstanceOf(Sample);
  });
});

The result got was:

Error: expect(received).toBeInstanceOf(expected)

Expected constructor: Sample
Received constructor: Object

Perhaps it can be fixed by manually setting the constructor to NewClass

Object.setPrototypeOf(obj, NewClass.prototype);
(obj as any).constructor = NewClass;

The problem is in the line:

    const fixture = {} as T;

of https://github.com/jcteague/efate/blob/master/packages/efate/src/fixture.ts

Charly-xepelin avatar Sep 16 '24 20:09 Charly-xepelin