Skip to content

Commit

Permalink
fix: delete env var after test if it was not set (googleapis#774)
Browse files Browse the repository at this point in the history
* fix: delete env var after test if it was not set

* fix: wrap tests in suite

Co-authored-by: Benjamin E. Coe <[email protected]>
  • Loading branch information
2 people authored and AVaksman committed Jan 16, 2020
1 parent 1c79349 commit a260370
Showing 1 changed file with 42 additions and 49 deletions.
91 changes: 42 additions & 49 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,23 +270,29 @@ describe('Spanner', () => {
assert.strictEqual(config.baseUrl, SERVICE_PATH);
});

it('should parse emulator host without port correctly', () => {
const currentEmulator = process.env.SPANNER_EMULATOR_HOST;
try {
describe('SPANNER_EMULATOR_HOST', () => {
let currentEmulator: string | undefined;

beforeEach(() => (currentEmulator = process.env.SPANNER_EMULATOR_HOST));

afterEach(() => {
if (currentEmulator) {
process.env.SPANNER_EMULATOR_HOST = currentEmulator;
} else {
delete process.env.SPANNER_EMULATOR_HOST;
}
});

it('should parse emulator host without port correctly', () => {
const EMULATOR_HOST = 'somehost.local';
process.env.SPANNER_EMULATOR_HOST = `${EMULATOR_HOST}`;

const emulator = Spanner.getSpannerEmulatorHost();

assert.deepStrictEqual(emulator, {endpoint: EMULATOR_HOST});
} finally {
process.env.SPANNER_EMULATOR_HOST = currentEmulator;
}
});
});

it('should parse emulator host with port correctly', () => {
const currentEmulator = process.env.SPANNER_EMULATOR_HOST;
try {
it('should parse emulator host with port correctly', () => {
const EMULATOR_HOST = 'somehost.local';
const EMULATOR_PORT = 1610;
process.env.SPANNER_EMULATOR_HOST = `${EMULATOR_HOST}:${EMULATOR_PORT}`;
Expand All @@ -297,45 +303,34 @@ describe('Spanner', () => {
endpoint: EMULATOR_HOST,
port: EMULATOR_PORT,
});
} finally {
process.env.SPANNER_EMULATOR_HOST = currentEmulator;
}
});
});

it('should reject emulator host with protocol', () => {
const currentEmulator = process.env.SPANNER_EMULATOR_HOST;
try {
const EMULATOR_HOST = 'https://somehost.local:1234';
process.env.SPANNER_EMULATOR_HOST = `${EMULATOR_HOST}`;
Spanner.getSpannerEmulatorHost();
assert.fail('Missing expected error');
} catch (e) {
assert.strictEqual(
e.message,
'SPANNER_EMULATOR_HOST must not start with a protocol specification (http/https)'
);
} finally {
process.env.SPANNER_EMULATOR_HOST = currentEmulator;
}
});
it('should reject emulator host with protocol', () => {
try {
const EMULATOR_HOST = 'https://somehost.local:1234';
process.env.SPANNER_EMULATOR_HOST = `${EMULATOR_HOST}`;
Spanner.getSpannerEmulatorHost();
assert.fail('Missing expected error');
} catch (e) {
assert.strictEqual(
e.message,
'SPANNER_EMULATOR_HOST must not start with a protocol specification (http/https)'
);
}
});

it('should reject emulator host with invalid port number', () => {
const currentEmulator = process.env.SPANNER_EMULATOR_HOST;
try {
const EMULATOR_HOST = 'somehost.local:not_a_port';
process.env.SPANNER_EMULATOR_HOST = `${EMULATOR_HOST}`;
Spanner.getSpannerEmulatorHost();
assert.fail('Missing expected error');
} catch (e) {
assert.strictEqual(e.message, 'Invalid port number: not_a_port');
} finally {
process.env.SPANNER_EMULATOR_HOST = currentEmulator;
}
});
it('should reject emulator host with invalid port number', () => {
try {
const EMULATOR_HOST = 'somehost.local:not_a_port';
process.env.SPANNER_EMULATOR_HOST = `${EMULATOR_HOST}`;
Spanner.getSpannerEmulatorHost();
assert.fail('Missing expected error');
} catch (e) {
assert.strictEqual(e.message, 'Invalid port number: not_a_port');
}
});

it('should use SPANNER_EMULATOR_HOST', () => {
const currentEmulator = process.env.SPANNER_EMULATOR_HOST;
try {
it('should use SPANNER_EMULATOR_HOST', () => {
const EMULATOR_HOST = 'somehost.local';
const EMULATOR_PORT = 1610;
process.env.SPANNER_EMULATOR_HOST = `${EMULATOR_HOST}:${EMULATOR_PORT}`;
Expand All @@ -346,9 +341,7 @@ describe('Spanner', () => {

assert.strictEqual(config.baseUrl, EMULATOR_HOST);
assert.strictEqual(options.port, EMULATOR_PORT);
} finally {
process.env.SPANNER_EMULATOR_HOST = currentEmulator;
}
});
});
});

Expand Down

0 comments on commit a260370

Please sign in to comment.