Skip to content

Commit e27616b

Browse files
committed
Add outer host and port check, and move tests back
1 parent c27254f commit e27616b

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,24 @@ export function getEmulatorHostAndPort(
8989
) {
9090
if (conf && ('host' in conf || 'port' in conf)) {
9191
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-
);
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(host, discovered?.hub?.host),
107+
port: port
108+
};
97109
}
98-
if (discovered && !discovered[emulator]) {
99-
console.warn(
100-
`Warning: config for the ${emulator} emulator is specified, but the Emulator hub ` +
101-
'reports it as not running. This may lead to errors such as connection refused.'
102-
);
103-
}
104-
return {
105-
host: fixHostname(host, discovered?.hub?.host),
106-
port: port
107-
};
108110
}
109111
const envVar = EMULATOR_HOST_ENV_VARS[emulator];
110112
const fallback = discovered?.[emulator] || emulatorFromEnvVar(envVar);

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

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

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+
126142
it('connect to 127.0.0.1 if host is wildcard 0.0.0.0', async () => {
127143
const result = getEmulatorHostAndPort('hub', {
128144
host: '0.0.0.0',
@@ -140,22 +156,6 @@ describe('getEmulatorHostAndPort()', () => {
140156
expect(result?.host).to.equal('::1');
141157
});
142158

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-
159159
it('uses discovered host/port if both config and env var are unset', async () => {
160160
const result = getEmulatorHostAndPort('hub', undefined, {
161161
hub: { host: '::1', port: 3333 }

0 commit comments

Comments
 (0)