Skip to content

Instantly share code, notes, and snippets.

@tbjers
Created October 26, 2017 00:00
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbjers/3d31764ce04fd07bc00f4588fa620b9f to your computer and use it in GitHub Desktop.
Save tbjers/3d31764ce04fd07bc00f4588fa620b9f to your computer and use it in GitHub Desktop.
Basics of Jest coverage with Typescript
import * as jestPlugin from 'serverless-jest-plugin';
import * as mod from './../handlers/campaigns/handler';
const lambdaWrapper = jestPlugin.lambdaWrapper;
const wrapped = lambdaWrapper.wrap(mod, { handler: 'campaigns' });
describe('λ: campaigns', () => {
beforeAll(done => {
done();
});
it('GET /error', () => {
const request = {
isOffline: true,
requestContext: {
httpMethod: 'GET',
resourcePath: '/error',
},
};
return wrapped.run(request).then(response => {
expect(response).toBeDefined();
expect(response.body).toBeDefined();
expect(response.statusCode).toEqual(404);
expect(JSON.parse(response.body)).toEqual({
Error: 'API route GET /error not found!',
});
});
});
// ...
{
"devDependencies": {
"@types/bluebird": "^3.5.12",
"@types/jest": "^21.1.1",
"@types/node": "^8.0.31",
"jest": "^21.2.0",
"serverless-dynamodb-local": "^0.2.25",
"serverless-jest-plugin": "^0.1.6",
"serverless-offline": "^3.16.0",
"serverless-plugin-typescript": "^1.1.2",
"ts-jest": "^21.0.1",
"ts-node": "^3.3.0",
"tslint": "^5.7.0",
"typescript": "^2.5.3"
},
"scripts": {
"coverage": "node scripts/test --coverage --silent",
"lint": "tslint --fix --project .",
"prepush": "yarn coverage",
"test": "node scripts/test"
},
"jest": {
"collectCoverageFrom": [
"handlers/**/*.ts"
],
"coveragePathIgnorePatterns": [
"<rootDir>/node_modules"
],
"coverageReporters": [
"json",
"lcov",
"text"
],
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
"transform": {
".ts": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testEnvironment": "node",
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts)$",
"moduleFileExtensions": [
"ts",
"js",
"json"
],
"mapCoverage": true
}
}
// Do this as the first thing so that any code reading it knows the right env.
process.env.NODE_ENV = 'test';
// Set up AWS test settings
process.env.AWS_REGION = 'localRegion';
process.env.IS_OFFLINE = true;
// TODO: Figure out how to run DynamoDB local with test suite
// Makes the script crash on unhandled rejections instead of silently
// ignoring them. In the future, promise rejections that are not handled will
// terminate the Node.js process with a non-zero exit code.
process.on('unhandledRejection', err => {
throw err;
});
const jest = require('jest');
const argv = process.argv.slice(2);
// Watch unless on CI or in coverage mode
if (!process.env.CI && argv.indexOf('--coverage') < 0) {
argv.push('--watch');
}
jest.run(argv);
{
"compilerOptions": {
"preserveConstEnums": true,
"strictNullChecks": true,
"sourceMap": true,
"declaration": true,
"target": "es6",
"module": "commonjs",
"outDir": ".build",
"typeRoots": ["node_modules/@types"],
"lib": ["es6", "es2015"],
"rootDir": "./"
},
"include": ["./database/**/*.ts", "./handlers/**/*.ts", "./__tests__/**/*.ts"],
"exclude": ["node_modules"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment