efate
efate copied to clipboard
Object created lost its constructor
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