Skip to content

Commit 0f491e2

Browse files
committed
Convert EmulatorConfig's HostAndPort to be an optional property
1 parent c05e3f2 commit 0f491e2

File tree

4 files changed

+25
-49
lines changed

4 files changed

+25
-49
lines changed

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,29 +84,20 @@ export interface DiscoveredEmulators {
8484
*/
8585
export function getEmulatorHostAndPort(
8686
emulator: keyof DiscoveredEmulators,
87-
conf?: EmulatorConfig,
87+
endpoint?: HostAndPort,
8888
discovered?: DiscoveredEmulators
89-
) {
90-
if (conf && 'host' in conf && 'port' in conf) {
91-
const { host, port } = conf;
92-
if (host || port) {
93-
if (!host || !port) {
94-
throw new Error(
95-
`Invalid configuration ${emulator}.host=${host} and ${emulator}.port=${port}. ` +
96-
'If either parameter is supplied, both must be defined.'
97-
);
98-
}
99-
if (discovered && !discovered[emulator]) {
100-
console.warn(
101-
`Warning: config for the ${emulator} emulator is specified, but the Emulator hub ` +
102-
'reports it as not running. This may lead to errors such as connection refused.'
103-
);
104-
}
105-
return {
106-
host: fixHostname(conf.host, discovered?.hub?.host),
107-
port: conf.port
108-
};
89+
): HostAndPort | undefined {
90+
if (endpoint) {
91+
if (discovered && !discovered[emulator]) {
92+
console.warn(
93+
`Warning: config for the ${emulator} emulator is specified, but the Emulator hub ` +
94+
'reports it as not running. This may lead to errors such as connection refused.'
95+
);
10996
}
97+
return {
98+
host: fixHostname(endpoint.host, discovered?.hub?.host),
99+
port: endpoint.port
100+
};
110101
}
111102
const envVar = EMULATOR_HOST_ENV_VARS[emulator];
112103
const fallback = discovered?.[emulator] || emulatorFromEnvVar(envVar);

packages/rules-unit-testing/src/initialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export async function initializeTestEnvironment(
7373
for (const emulator of SUPPORTED_EMULATORS) {
7474
const hostAndPort = getEmulatorHostAndPort(
7575
emulator,
76-
config[emulator],
76+
config[emulator]?.endpoint,
7777
discovered
7878
);
7979
if (hostAndPort) {

packages/rules-unit-testing/src/public_types/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export interface TestEnvironmentConfig {
128128
* An object containing the hostname and port number of an emulator.
129129
* @public
130130
*/
131-
export interface HostAndPort {
131+
export type HostAndPort = {
132132
/**
133133
* The host of the emulator. Can be omitted if discovered automatically through the hub or
134134
* specified via environment variables. See `TestEnvironmentConfig` for details.
@@ -140,7 +140,7 @@ export interface HostAndPort {
140140
* specified via environment variables. See `TestEnvironmentConfig` for details.
141141
*/
142142
port: number;
143-
}
143+
};
144144

145145
/**
146146
* Configuration for a given emulator.
@@ -149,7 +149,12 @@ export interface HostAndPort {
149149
export type EmulatorConfig = {
150150
/** The security rules source code under test for this emulator. Strongly recommended. */
151151
rules?: string;
152-
} & (HostAndPort | {}); // Both or none of host and port should be specified.
152+
/**
153+
* Endpoint of the emulator. Can be omitted if discovered automatically through the hub.
154+
* See `TestEnvironmentConfig` for details.
155+
*/
156+
endpoint?: HostAndPort;
157+
};
153158

154159
/**
155160
* An object used to control the rules unit test environment. Can be used to create RulesTestContext

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
EMULATOR_HOST_ENV_VARS,
2323
getEmulatorHostAndPort
2424
} from '../../src/impl/discovery';
25-
import { HostAndPort } from '../../src/public_types';
25+
import { EmulatorConfig, HostAndPort } from '../../src/public_types';
2626
import { restoreEnvVars, stashEnvVars } from '../test_utils';
2727

2828
describe('discoverEmulators()', () => {
@@ -106,10 +106,8 @@ describe('getEmulatorHostAndPort()', () => {
106106
expect(result).to.be.undefined;
107107
});
108108

109-
it('returns undefined if config option does not contain host/port', async () => {
110-
const result = getEmulatorHostAndPort('hub', {
111-
rules: '/* security rules only, no host/port */'
112-
});
109+
it('returns undefined if endpoint is undefined', async () => {
110+
const result = getEmulatorHostAndPort('hub');
113111

114112
expect(result).to.be.undefined;
115113
});
@@ -123,22 +121,6 @@ describe('getEmulatorHostAndPort()', () => {
123121
expect(result?.host).to.equal('::1');
124122
});
125123

126-
it('throws if only host is present', async () => {
127-
expect(() =>
128-
getEmulatorHostAndPort('hub', {
129-
host: '[::1]'
130-
} as HostAndPort)
131-
).to.throw(/hub.port=undefined/);
132-
});
133-
134-
it('throws if only port is present', async () => {
135-
expect(() =>
136-
getEmulatorHostAndPort('database', {
137-
port: 1234
138-
} as HostAndPort)
139-
).to.throw(/Invalid configuration database.host=undefined/);
140-
});
141-
142124
it('connect to 127.0.0.1 if host is wildcard 0.0.0.0', async () => {
143125
const result = getEmulatorHostAndPort('hub', {
144126
host: '0.0.0.0',
@@ -304,9 +286,7 @@ describe('getEmulatorHostAndPort()', () => {
304286

305287
it('takes host and port from env var if config has no host/port', async () => {
306288
process.env[EMULATOR_HOST_ENV_VARS.hub] = '127.0.0.1:3445';
307-
const result = getEmulatorHostAndPort('hub', {
308-
rules: '/* security rules only, no host/port */'
309-
});
289+
const result = getEmulatorHostAndPort('hub');
310290

311291
expect(result?.host).to.equal('127.0.0.1');
312292
expect(result?.port).to.equal(3445);

0 commit comments

Comments
 (0)