Skip to content

Commit e7b9b12

Browse files
committed
Throw error if only one of host or port are defined
1 parent 2b830aa commit e7b9b12

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

packages/rules-unit-testing/src/impl/discovery.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,23 @@ export function getEmulatorHostAndPort(
8787
conf?: EmulatorConfig,
8888
discovered?: DiscoveredEmulators
8989
) {
90-
if (conf && 'host' in conf && 'port' in conf) {
90+
if (conf && ('host' in conf || 'port' in conf)) {
91+
const { host, port } = conf as any;
92+
if (!host || !port) {
93+
throw new Error(
94+
`Invalid configuration ${emulator}.host=${host} and ${emulator}.port=${port}. ` +
95+
'If either parameter is supplied, both must be defined.'
96+
);
97+
}
9198
if (discovered && !discovered[emulator]) {
9299
console.warn(
93100
`Warning: config for the ${emulator} emulator is specified, but the Emulator hub ` +
94101
'reports it as not running. This may lead to errors such as connection refused.'
95102
);
96103
}
97104
return {
98-
host: fixHostname(conf.host, discovered?.hub?.host),
99-
port: conf.port
105+
host: fixHostname(host, discovered?.hub?.host),
106+
port: port
100107
};
101108
}
102109
const envVar = EMULATOR_HOST_ENV_VARS[emulator];

packages/rules-unit-testing/test/impl/discovery.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ describe('getEmulatorHostAndPort()', () => {
140140
expect(result?.host).to.equal('::1');
141141
});
142142

143+
it('throws if only host is present', async () => {
144+
expect(() =>
145+
getEmulatorHostAndPort('hub', {
146+
host: '[::1]'
147+
} as HostAndPort)
148+
).to.throw(/hub.port=undefined/);
149+
});
150+
151+
it('throws if only port is present', async () => {
152+
expect(() =>
153+
getEmulatorHostAndPort('database', {
154+
port: 1234
155+
} as HostAndPort)
156+
).to.throw(/Invalid configuration database.host=undefined/);
157+
});
158+
143159
it('uses discovered host/port if both config and env var are unset', async () => {
144160
const result = getEmulatorHostAndPort('hub', undefined, {
145161
hub: { host: '::1', port: 3333 }

0 commit comments

Comments
 (0)