Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Node CI

on: [pull_request]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
node-version: [10, 12, 14]

steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm run test
env:
CI: true
NODE_ENV: test
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

5 changes: 4 additions & 1 deletion __mocks__/twilio.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const twilio = jest.genMockFromModule('twilio');
const actualTwilio = jest.requireActual('twilio');
const twilio: any = jest.genMockFromModule('twilio');

twilio['twiml'] = actualTwilio.twiml;

// mock specific functionality

Expand Down
84 changes: 49 additions & 35 deletions __tests__/runtime/route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const { VoiceResponse, MessagingResponse, FaxResponse } = twiml;
const mockResponse = (new MockResponse() as unknown) as ExpressResponse;
mockResponse.type = jest.fn(() => mockResponse);

function asExpressRequest(req: { query?: {}; body?: {} }): ExpressRequest {
return (req as unknown) as ExpressRequest;
}

describe('handleError function', () => {
test('returns string error', () => {
const mockRequest = (new MockRequest() as unknown) as ExpressRequest;
Expand Down Expand Up @@ -101,57 +105,67 @@ describe('handleError function', () => {

describe('constructEvent function', () => {
test('merges query and body', () => {
const event = constructEvent({
body: {
Body: 'Hello',
},
query: {
index: 5,
},
} as ExpressRequest);
const event = constructEvent(
asExpressRequest({
body: {
Body: 'Hello',
},
query: {
index: 5,
},
})
);
expect(event).toEqual({ Body: 'Hello', index: 5 });
});

test('overrides query with body', () => {
const event = constructEvent({
body: {
Body: 'Bye',
},
query: {
Body: 'Hello',
From: '+123456789',
},
} as ExpressRequest);
const event = constructEvent(
asExpressRequest({
body: {
Body: 'Bye',
},
query: {
Body: 'Hello',
From: '+123456789',
},
})
);
expect(event).toEqual({ Body: 'Bye', From: '+123456789' });
});

test('handles empty body', () => {
const event = constructEvent({
body: {},
query: {
Body: 'Hello',
From: '+123456789',
},
} as ExpressRequest);
const event = constructEvent(
asExpressRequest({
body: {},
query: {
Body: 'Hello',
From: '+123456789',
},
})
);
expect(event).toEqual({ Body: 'Hello', From: '+123456789' });
});

test('handles empty query', () => {
const event = constructEvent({
body: {
Body: 'Hello',
From: '+123456789',
},
query: {},
} as ExpressRequest);
const event = constructEvent(
asExpressRequest({
body: {
Body: 'Hello',
From: '+123456789',
},
query: {},
})
);
expect(event).toEqual({ Body: 'Hello', From: '+123456789' });
});

test('handles both empty', () => {
const event = constructEvent({
body: {},
query: {},
} as ExpressRequest);
const event = constructEvent(
asExpressRequest({
body: {},
query: {},
})
);
expect(event).toEqual({});
});
});
Expand Down
67 changes: 35 additions & 32 deletions __tests__/templating/filesystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ jest.mock('pkg-install');
jest.mock('../../src/utils/fs');
jest.mock('../../src/utils/logger');

import path from 'path';
import { install } from 'pkg-install';
import { fsHelpers } from '@twilio-labs/serverless-api';
import got from 'got';
import path, { join } from 'path';
import { install } from 'pkg-install';
import { mocked } from 'ts-jest/utils';
import { writeFiles } from '../../src/templating/filesystem';
import {
downloadFile,
fileExists,
mkdir,
readFile,
writeFile,
mkdir,
} from '../../src/utils/fs';

beforeEach(() => {
Expand Down Expand Up @@ -108,22 +108,22 @@ test('installation with basic functions', async () => {
expect(downloadFile).toHaveBeenCalledTimes(3);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/.env',
'testing/.env'
join('testing', '.env')
);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/hello.js',
'testing/functions/example/hello.js'
join('testing', 'functions', 'example', 'hello.js')
);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/README.md',
'testing/readmes/example/hello.md'
join('testing', 'readmes', 'example', 'hello.md')
);

expect(mkdir).toHaveBeenCalledTimes(2);
expect(mkdir).toHaveBeenCalledWith('testing/functions/example', {
expect(mkdir).toHaveBeenCalledWith(join('testing', 'functions', 'example'), {
recursive: true,
});
expect(mkdir).toHaveBeenCalledWith('testing/readmes/example', {
expect(mkdir).toHaveBeenCalledWith(join('testing', 'readmes', 'example'), {
recursive: true,
});
});
Expand Down Expand Up @@ -165,22 +165,22 @@ test('installation with functions and assets', async () => {
expect(downloadFile).toHaveBeenCalledTimes(3);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/.env',
'testing/.env'
join('testing', '.env')
);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/hello.js',
'testing/functions/example/hello.js'
join('testing', 'functions', 'example', 'hello.js')
);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/hello.wav',
'testing/assets/example/hello.wav'
join('testing', 'assets', 'example', 'hello.wav')
);

expect(mkdir).toHaveBeenCalledTimes(2);
expect(mkdir).toHaveBeenCalledWith('testing/functions/example', {
expect(mkdir).toHaveBeenCalledWith(join('testing', 'functions', 'example'), {
recursive: true,
});
expect(mkdir).toHaveBeenCalledWith('testing/assets/example', {
expect(mkdir).toHaveBeenCalledWith(join('testing', 'assets', 'example'), {
recursive: true,
});
});
Expand Down Expand Up @@ -228,23 +228,23 @@ test('installation with functions and assets and blank namespace', async () => {
expect(downloadFile).toHaveBeenCalledTimes(4);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/.env',
'testing/.env'
join('testing', '.env')
);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/hello.js',
'testing/functions/hello.js'
join('testing', 'functions', 'hello.js')
);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/hello.wav',
'testing/assets/hello.wav'
join('testing', 'assets', 'hello.wav')
);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/README.md',
'testing/readmes/hello.md'
join('testing', 'readmes', 'hello.md')
);

expect(mkdir).toHaveBeenCalledTimes(1);
expect(mkdir).toHaveBeenCalledWith('testing/readmes', {
expect(mkdir).toHaveBeenCalledWith(join('testing', 'readmes'), {
recursive: true,
});
});
Expand Down Expand Up @@ -275,6 +275,7 @@ test('installation with an empty dependency file', async () => {
// buffer but depending on inputs `got` can actually return an object.
// @ts-ignore
mocked(got).mockImplementation(() =>
//@ts-ignore
Promise.resolve({ body: { dependencies: {} } })
);

Expand Down Expand Up @@ -308,7 +309,7 @@ test('installation with an empty dependency file', async () => {
expect(downloadFile).toHaveBeenCalledTimes(1);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/.env',
'testing/.env'
join('testing', '.env')
);

expect(got).toHaveBeenCalledTimes(1);
Expand All @@ -324,6 +325,7 @@ test('installation with a dependency file', async () => {
// buffer but depending on inputs `got` can actually return an object.
// @ts-ignore
mocked(got).mockImplementation(() =>
// @ts-ignore
Promise.resolve({
body: { dependencies: { foo: '^1.0.0', got: '^6.9.0' } },
})
Expand Down Expand Up @@ -359,7 +361,7 @@ test('installation with a dependency file', async () => {
expect(downloadFile).toHaveBeenCalledTimes(1);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/.env',
'testing/.env'
join('testing', '.env')
);

expect(got).toHaveBeenCalledTimes(1);
Expand All @@ -382,6 +384,7 @@ test('installation with an existing dot-env file', async () => {
// buffer but depending on inputs `got` can actually return an object.
// @ts-ignore
mocked(got).mockImplementation(() =>
// @ts-ignore
Promise.resolve({ body: 'HELLO=WORLD\n' })
);

Expand Down Expand Up @@ -410,7 +413,7 @@ test('installation with an existing dot-env file', async () => {

expect(writeFile).toHaveBeenCalledTimes(1);
expect(writeFile).toHaveBeenCalledWith(
'testing/.env',
join('testing', '.env'),
'# Comment\n' +
'FOO=BAR\n' +
'\n\n' +
Expand All @@ -429,7 +432,7 @@ test('installation with overlapping function files throws errors before writing'
);

mocked(fileExists).mockImplementation(p =>
Promise.resolve(p == 'functions/example/hello.js')
Promise.resolve(p == join('functions', 'example', 'hello.js'))
);

await expect(
Expand Down Expand Up @@ -468,7 +471,7 @@ test('installation with overlapping asset files throws errors before writing', a
);

mocked(fileExists).mockImplementation(p =>
Promise.resolve(p == 'assets/example/hello.wav')
Promise.resolve(p == join('assets', 'example', 'hello.wav'))
);

await expect(
Expand All @@ -493,7 +496,7 @@ test('installation with overlapping asset files throws errors before writing', a
directory: '',
},
],
'./',
join('.', path.sep),
'example',
'hello'
)
Expand Down Expand Up @@ -540,37 +543,37 @@ test('installation with functions and assets in nested directories', async () =>
directory: '',
},
],
'./testing/',
join('.', 'testing'),
'example',
'hello'
);

expect(downloadFile).toHaveBeenCalledTimes(4);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/.env',
'testing/.env'
join('testing', '.env')
);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/hello.js',
'testing/functions/example/admin/hello.js'
join('testing', 'functions', 'example', 'admin', 'hello.js')
);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/README.md',
'testing/readmes/example/hello.md'
join('testing', 'readmes', 'example', 'hello.md')
);
expect(downloadFile).toHaveBeenCalledWith(
'https://example.com/woohoo.jpg',
'testing/assets/example/success/woohoo.jpg'
join('testing', 'assets', 'example', 'success', 'woohoo.jpg')
);

expect(mkdir).toHaveBeenCalledTimes(3);
expect(mkdir).toHaveBeenCalledWith('testing/functions/example', {
expect(mkdir).toHaveBeenCalledWith(join('testing', 'functions', 'example'), {
recursive: true,
});
expect(mkdir).toHaveBeenCalledWith('testing/readmes/example', {
expect(mkdir).toHaveBeenCalledWith(join('testing', 'readmes', 'example'), {
recursive: true,
});
expect(mkdir).toHaveBeenCalledWith('testing/assets/example', {
expect(mkdir).toHaveBeenCalledWith(join('testing', 'assets', 'example'), {
recursive: true,
});
});
Loading