Skip to content

Commit

Permalink
Merge pull request #173 from humanmade/add-tests-gh
Browse files Browse the repository at this point in the history
Add Jest tests
  • Loading branch information
joehoyle authored Feb 28, 2024
2 parents 8702dfa + 52e27a8 commit 15c77f0
Show file tree
Hide file tree
Showing 29 changed files with 9,417 additions and 15,441 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Test

on:
pull_request:
branches:
- '**'

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18.x
- run: npm install
- run: npm run test

2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ node_modules/
.idea
lambda.zip
.aws-sam/
test-filesize
.DS_Store
dist/
/tests/test-filesize/output/
24 changes: 21 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
# Contributing

## Building
## Building for Lambda

You'll need to [install the AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) as AWS SAM is used to build the ZIP and text the fixtures.
You'll need to [install the AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) as AWS SAM.

```
npm install
npm run build // Builds the function for use in SAM
npm run test // Invoke a function via SAM using a fixture from ./events/
```

### Building locally

Tachyon is written in TypeScript. All TypeScript files are in `.src` and running `npx tsc` will build everything to `./dist`. You can run `npx tsc -w` to watch for file changes to update `./dist`. This is needed if you are running the server locally (see below) or running the Lambda environment via the SAM cli (see below.)

### Running a server locally

Invoking the function via Lambda locally is somewhat slow (see below), in many cases you may want to start a local Node server which maps the Node request into a Lambda-like request. `./src/server.ts` exists for that reason. The local server will still connect to the S3 bucket (set with the `S3_BUCKET` env var) for files.


### Running Lambda Locally

Before testing any of the Lambda function calls via the `sam` CLI, you must run `sam build -u` to build the NPM deps via the Lambda docker container. This will also build the `./dist/` into the SAM environment, so any subsequent changes to files in `./src` but be first built (which updates `./dist`), and then `sam build -u` must be run.

To run Tachyon in a Lambda local environment via docker, use the `sam local invoke -e events/animated-gif.json` CLI command. This will call the function via the `src/lambda-handler.handler` function.

### Writing tests

Tests should be written using Jest. Files matching `./tests/**/test-*.ts` will automatically be included in the Jest testsuite. For tests, you don't need to run `npx tsc` to compile TypeScript files to `./dist`, as this is integrated automatically via the `ts-jest` package.

Run `npm test` to run the tests.
13 changes: 11 additions & 2 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
declare type ResponseStream = {
setContentType( type: string ): void;
write( stream: string | Buffer ): void;
end(): void;
};

declare type StreamifyHandler = ( event: APIGatewayProxyEventV2, response: ResponseStream ) => Promise<any>;

declare var awslambda: {
streamifyResponse: (
handler: ( event: APIGatewayProxyEventV2, response: ResponseStream ) => Promise<any>
) => ( event: APIGatewayProxyEventV2, context: ResponseStream ) => void;
handler: StreamifyHandler
) => ( event: APIGatewayProxyEventV2, context: ResponseStream ) => void,
};

16 changes: 16 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['<rootDir>/tests/**/test-*.ts'],
extensionsToTreatAsEsm: ['.ts'],
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
tsconfig: './tsconfig.test.json',
},
],
},
};
Loading

0 comments on commit 15c77f0

Please sign in to comment.