Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test for specifying all regions, add memory type to catch compilc…
…ation errors
  • Loading branch information
thechenky committed May 8, 2019
commit 24f7c9c03b2ee780bff9a3136cd4759886947ce9
25 changes: 24 additions & 1 deletion spec/function-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ describe('FunctionBuilder', () => {
expect(fn.__trigger.regions).to.deep.equal(['us-east1', 'us-central1']);
});

it('should allow all supported regions to be set', () => {
let fn = functions
.region(
'us-central1',
'us-east1',
'europe-west1',
'europe-west2',
'asia-east2',
'asia-northeast1'
)
.auth.user()
.onCreate(user => user);

expect(fn.__trigger.regions).to.deep.equal([
'us-central1',
'us-east1',
'europe-west1',
'europe-west2',
'asia-east2',
'asia-northeast1',
]);
});

it('should allow valid runtime options to be set', () => {
let fn = functions
.runWith({
Expand Down Expand Up @@ -110,7 +133,7 @@ describe('FunctionBuilder', () => {
}).to.throw(Error, 'TimeoutSeconds');
});

it('should throw an error if user chooses an unsupported memory allocation', () => {
it('should throw an error if user chooses an invalid memory allocation', () => {
expect(() => {
return functions.runWith({
memory: 'unsupported',
Expand Down
17 changes: 12 additions & 5 deletions src/function-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ const SUPPORTED_REGIONS = [
/**
* List of available memory options supported by Cloud Functions.
*/
const MEMORY_OPTS = ['128MB', '256MB', '512MB', '1GB', '2GB'];
const VALID_MEMORY_OPTS = ['128MB', '256MB', '512MB', '1GB', '2GB'];

// Adding this memory type here to error on compile for TS users.
// Unfortunately I have not found a way to merge this with VALID_MEMORY_OPS
// without it being super ugly. But here they are right next to each other at least.
type memory = '128MB' | '256MB' | '512MB' | '1GB' | '2GB';

/**
* Cloud Functions max timeout value.
Expand All @@ -64,10 +69,12 @@ const MAX_TIMEOUT_SECONDS = 540;
function assertRuntimeOptionsValid(runtimeOptions: RuntimeOptions): boolean {
if (
runtimeOptions.memory &&
!_.includes(MEMORY_OPTS, runtimeOptions.memory)
!_.includes(VALID_MEMORY_OPTS, runtimeOptions.memory)
) {
throw new Error(
`The only valid memory allocation values are: ${MEMORY_OPTS.join(', ')}`
`The only valid memory allocation values are: ${VALID_MEMORY_OPTS.join(
', '
)}`
);
}
if (
Expand Down Expand Up @@ -124,13 +131,13 @@ export function runWith(runtimeOptions: RuntimeOptions): FunctionBuilder {

export interface RuntimeOptions {
timeoutSeconds?: number;
memory?: string;
memory?: memory;
}

export interface DeploymentOptions {
regions?: string[];
timeoutSeconds?: number;
memory?: string;
memory?: memory;
schedule?: Schedule;
}

Expand Down