Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinon create stub instance - include protected properties

Tags:

sinon

My question is how does one include protected properties when creating a stub instance.

In my jest test I have:

const sandbox = createSandbox();
let manager: SinonStubbedInstance<EntityManager>;
let repo: Repo;

beforeEach(() => {
    manager = sandbox.createStubInstance(EntityManager);
    repo = new Repo(manager);
});

afterEach(() => sandbox.restore());

Which is attempting to make a stub of:

export declare class EntityManager {

/**
 * Connection used by this entity manager.
 */
readonly connection: Connection;

/**
 * Custom query runner to be used for operations in this entity manager.
 * Used only in non-global entity manager.
 */
readonly queryRunner?: QueryRunner;

/**
 * Once created and then reused by en repositories.
 */
protected repositories: Repository<any>[];

/**
 * Plain to object transformer used in create and merge operations.
 */
.......
}

So I don't seem to be able to have readonly properties and protected properties included in the stub.

At the "repo = new Repo(manager);" line. The above code yields the following exception:

Argument of type 'SinonStubbedInstance<EntityManager>' is not assignable to parameter of type 'EntityManager'.
Property 'repositories' is missing in type 'SinonStubbedInstance<EntityManager>'.ts(2345)

Is there anyway to tell Sinon to include the properties? Any help would be most appreciated.

like image 712
Rhys Tufvesson Avatar asked Mar 08 '26 17:03

Rhys Tufvesson


1 Answers

I solved this problem with

repo = new Repo(manager as any);
like image 92
TJ Liu Avatar answered Mar 12 '26 12:03

TJ Liu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!