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
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@ jobs:
env:
CI: true

type-test:
name: Type test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- uses: actions/setup-node@v1
with:
node-version: 14

- name: Restore cache
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}

- run: yarn install

- run: yarn test:types

publish:
name: Publish
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ module.exports = {

### Testing

Run `yarn test` to run the unit tests.
Run `yarn test` to run the unit tests, and `yarn test:types` to run the type tests.

When writing a bugfix, start by writing a test that reproduces the problem. It should fail with the current version of Lambda Wrapper, and pass once you've implemented the fix.

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"clean": "rm -rf dist",
"lint": "eslint src tests",
"test": "jest",
"test:types": "tsc -p tsconfig-type-test.json",
"coverage": "yarn test --coverage"
},
"devDependencies": {
Expand All @@ -35,6 +36,7 @@
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.25.2",
"eslint-plugin-jsdoc": "^39.3.2",
"expect-type": "^0.18.0",
"jest": "^29.7.0",
"nyc": "^15.1.0",
"semantic-release": "^19.0.5",
Expand Down
2 changes: 2 additions & 0 deletions tests/type/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rules:
'@typescript-eslint/ban-ts-comment': off
39 changes: 39 additions & 0 deletions tests/type/DependencyInjection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expectTypeOf } from 'expect-type';

import lambdaWrapper, {
DependencyAwareClass,
DependencyInjection,
LoggerService,
SQSService,
} from '../../src/index';
import { mockContext, mockEvent } from '../mocks/aws';

describe('type.DependencyInjection', () => {
const di = new DependencyInjection(lambdaWrapper.config, mockEvent, mockContext);

describe('get', () => {
it('should return a LoggerService instance if passed LoggerService', () => {
expectTypeOf(di.get(LoggerService)).toEqualTypeOf<LoggerService>();
});

it('should return an SQSService instance if passed SQSService', () => {
// for this one we also expect the TConfig type parameter to be correct
expectTypeOf(di.get(SQSService)).toEqualTypeOf<SQSService<typeof di.config>>();
});

it('should accept any dependency-aware class', () => {
// Currently, the type system does not restrict you to getting only
// dependencies that are specified in the Lambda Wrapper config, however
// doing so will result in a runtime error.
class Good extends DependencyAwareClass {}
expectTypeOf(di.get).toBeCallableWith(Good);
expectTypeOf(di.get(Good)).toEqualTypeOf<InstanceType<typeof Good>>();
});

it('should not accept classes that are not dependency-aware', () => {
class Bad {}
// @ts-expect-error: it should _not_ be callable with Bad
expectTypeOf(di.get).toBeCallableWith(Bad);
});
});
});
62 changes: 62 additions & 0 deletions tests/type/SQSService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { expectTypeOf } from 'expect-type';

import lambdaWrapper, {
DependencyInjection,
QueueName,
SQSService,
} from '../../src/index';
import { mockContext, mockEvent } from '../mocks/aws';

describe('type.SQSService', () => {
const lwWithQueues = lambdaWrapper.configure({
sqs: {
queues: {
test1: 'test-queue-1',
test2: 'test-queue-2',
},
},
});

type expectedQueueNames = 'test1' | 'test2';

const di = new DependencyInjection(lwWithQueues.config, mockEvent, mockContext);
const sqs = di.get(SQSService);

describe('QueueName', () => {
it('should infer queue names from config', () => {
// use our configured LambdaWrapper instance
type queueName = QueueName<typeof lwWithQueues.config>;
expectTypeOf<queueName>().toEqualTypeOf<expectedQueueNames>();
});

it('should infer `never` if no queues are configured', () => {
// use the package's default out-of-the-box LambdaWrapper instance
type queueName = QueueName<typeof lambdaWrapper.config>;
expectTypeOf<queueName>().toBeNever();
});
});

describe('batchDelete', () => {
it('should accept only configured queue names', () => {
expectTypeOf(sqs.batchDelete).parameter(0).toEqualTypeOf<expectedQueueNames>();
});
});

describe('getMessageCount', () => {
it('should accept only configured queue names', () => {
expectTypeOf(sqs.getMessageCount).parameter(0).toEqualTypeOf<expectedQueueNames>();
});
});

describe('publish', () => {
it('should accept only configured queue names', () => {
expectTypeOf(sqs.publish).parameter(0).toEqualTypeOf<expectedQueueNames>();
});
});

describe('receive', () => {
it('should accept only configured queue names', () => {
expectTypeOf(sqs.receive).parameter(0).toEqualTypeOf<expectedQueueNames>();
});
});
});
10 changes: 10 additions & 0 deletions tsconfig-type-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": true,
},
"include": [
"tests/type/*.spec.ts",
"types/*.d.ts",
]
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2913,6 +2913,11 @@ exit@^0.1.2:
resolved "https://registry.yarnpkg.com/exit/-/exit-0.1.2.tgz#0632638f8d877cc82107d30a0fff1a17cba1cd0c"
integrity sha512-Zk/eNKV2zbjpKzrsQ+n1G6poVbErQxJ0LBOJXaKZ1EViLzH+hrLu9cdXI4zw9dBQJslwBEpbQ2P1oS7nDxs6jQ==

expect-type@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/expect-type/-/expect-type-0.18.0.tgz#16f62ff9d04950d7d286ed9179a52bc3ea366f2b"
integrity sha512-xjKoyyDLoia2h1WF+vwV8AmEpQ0drGW0InRgyywAHyOC+XSPYMxGoMXSwPjXs46D8FgLmp32sHMd1KrVingDuQ==

expect@^29.0.0, expect@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/expect/-/expect-29.7.0.tgz#578874590dcb3214514084c08115d8aee61e11bc"
Expand Down