testcontainers-node
testcontainers-node copied to clipboard
Containers should be using protected variables, not private.
Expected Behaviour I should be able to extend the PostgreSqlContainer without issues as follows
public override async start() {
return new StartedPostgreSqlContainer(
await super.start(),
this.database,
this.username,
this.password,
);
}
Actual Behaviour I'm required to add
private override database = 'test';
private override username = 'test';
private override password = 'test';
Testcontainer Logs Not relevant
Steps to Reproduce
import postgresql from '@testcontainers/postgresql';
export class PostgreSqlContainer extends postgresql.PostgreSqlContainer {
// These are the same as upstream, however they use 'private' instead of 'protected'
private override database = 'test';
private override username = 'test';
private override password = 'test';
public override async start() {
return new StartedPostgreSqlContainer(
await super.start(),
this.database,
this.username,
this.password,
);
}
}
/*
* This is here to wrap the provided postgresql container so we can use 'using'
*/
export class StartedPostgreSqlContainer
extends postgresql.StartedPostgreSqlContainer
implements Disposable
{
async [Symbol.dispose](): Promise<void> {
await this.stop();
}
}
Environment Information
- Operating System: Arch Linux on WSL2
- Docker Version: 27.2.0, build 3ab4256
- Node version: v20.17.0
- Testcontainers version: 10.12.0
What do you need that using the public withUsername/password doesn't provide?
I'm wanting to use the using command brought in with Typescript 5.2, I thought my only options were to extend the existing class, wait for you to add
[Symbol.dispose]: async () => {
await (await container).stop();
},
to StartedTestContainer (which would need a typescript bump), or now I realise I can just use a wrapper with
export const DisposableContainer = async <
T extends Promise<StartedTestContainer>,
>(
container: T,
) => {
return Object.assign(await container, {
[Symbol.dispose]: async () => {
await (await container).stop();
},
});
};
Which might be helpful for others.