Skip to content

Commit

Permalink
Merge pull request #19 from StanHannebelle/typing
Browse files Browse the repository at this point in the history
Typing
  • Loading branch information
lukey-aleios authored Jan 24, 2022
2 parents 0fbdd80 + df8c58e commit 70cd5b2
Show file tree
Hide file tree
Showing 28 changed files with 2,607 additions and 1,012 deletions.
113 changes: 92 additions & 21 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,97 @@
module.exports = {
extends: [
"eslint:recommended",
"plugin:import/recommended",
"plugin:prettier/recommended",
],
rules: {
curly: ["error", "all"],
eqeqeq: ["error", "smart"],
"import/no-duplicates": "off",
"import/no-extraneous-dependencies": [
"error",
{
devDependencies: true,
optionalDependencies: false,
peerDependencies: false,
},
],
"no-shadow": [
"error",
{
hoist: "all",
},
],
"prefer-const": "error",
"sort-imports": [
"error",
{
ignoreCase: true,
ignoreDeclarationSort: true,
ignoreMemberSort: false,
memberSyntaxSortOrder: ["none", "all", "multiple", "single"],
},
],
"padding-line-between-statements": [
"error",
{
blankLine: "always",
prev: "*",
next: "return",
},
],
complexity: ["error", 8],
"max-lines": ["error", 200],
"max-depth": ["error", 3],
"max-params": ["error", 2],
},

root: true,
plugins: ["import"],
env: {
node: true,
es6: true,
node: true,
},
extends: ["airbnb-base", "prettier"],
globals: {
Atomics: "readonly",
SharedArrayBuffer: "readonly",
},
parserOptions: {
ecmaVersion: 2018,
sourceType: "module",
},
plugins: ["prettier"],
rules: {
"prettier/prettier": "error",
quotes: ["error", "double"],
"no-console": "off",
"no-plusplus": "off",
"no-param-reassign": "off",
"no-new": "off",
"import/named": "off",
"import/no-cycle": "off",
},
parserOptions: { ecmaVersion: 2021 },
overrides: [
{
files: ["**/*.ts?(x)"],
settings: { "import/resolver": { typescript: {} } },
extends: [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:import/typescript",
"plugin:prettier/recommended",
],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.json",
},
rules: {
"@typescript-eslint/explicit-module-boundary-types": "error",
"@typescript-eslint/prefer-optional-chain": "error",
"no-shadow": "off",
"@typescript-eslint/no-shadow": "error",
"@typescript-eslint/prefer-nullish-coalescing": "error",
"@typescript-eslint/strict-boolean-expressions": "error",
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "error",
"@typescript-eslint/no-unnecessary-condition": "error",
"@typescript-eslint/no-unnecessary-type-arguments": "error",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{ argsIgnorePattern: "^_$", varsIgnorePattern: "^_$" },
],
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/switch-exhaustiveness-check": "error",
"@typescript-eslint/ban-ts-comment": [
"error",
{
"ts-ignore": "allow-with-description",
minimumDescriptionLength: 10,
},
],
},
},
],
};
52 changes: 52 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const defaultPresets = [
["@babel/preset-typescript", { allowNamespaces: true }],
];

const defaultIgnores = [
/.*\/(.*\.|)test\.tsx?/,
/bundle\.ts/,
/node_modules/,
/lib/,
];

const presetsForESM = [
[
"@babel/preset-env",
{
modules: false,
},
],
...defaultPresets,
];
const presetsForCJS = [
[
"@babel/preset-env",
{
modules: "cjs",
},
],
...defaultPresets,
];
const plugins = [
[
"module-resolver",
{
root: ["./src"],
extensions: [".ts"],
},
],
"@babel/plugin-transform-runtime",
];

module.exports = {
env: {
cjs: {
presets: presetsForCJS,
},
esm: {
presets: presetsForESM,
},
},
ignore: defaultIgnores,
plugins,
};
41 changes: 29 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "sls-test-tools",
"version": "1.0.3",
"description": "Custom Jest Assertions for Serverless Projects",
"main": "lib/index.js",
"main": "lib/cjs/index.js",
"module": "lib/esm/index.js",
"types": "lib/types/index.d.ts",
"directories": {
"lib": "lib"
},
Expand All @@ -14,29 +16,44 @@
"author": "Theodo UK",
"license": "MIT",
"scripts": {
"build": "babel ./src --out-dir lib",
"build-maps": "babel ./src --out-dir lib --source-maps",
"dev": "babel ./src --out-dir lib --watch",
"build": "rm -rf lib && yarn package:cjs && yarn package:esm && yarn package:types",
"dev": "rm -rf lib && concurrently 'yarn:package:* --watch'",
"lint": "eslint ./src",
"fix": "yarn lint --fix",
"release": "release-it"
"release": "release-it",
"package:cjs": "NODE_ENV=cjs yarn transpile --out-dir lib/cjs --source-maps",
"package:esm": "NODE_ENV=esm yarn transpile --out-dir lib/esm --source-maps",
"package:types": "ttsc",
"transpile": "babel src --extensions .ts"
},
"dependencies": {
"aws-sdk": "^2.711.0",
"import-all.macro": "^3.1.0"
},
"devDependencies": {
"@babel/cli": "^7.5.5",
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"@babel/cli": "^7.16.0",
"@babel/core": "^7.16.0",
"@babel/plugin-transform-runtime": "^7.16.0",
"@babel/preset-env": "^7.16.0",
"@babel/preset-typescript": "^7.16.0",
"@typescript-eslint/eslint-plugin": "^5.6.0",
"@typescript-eslint/parser": "^5.6.0",
"@zerollup/ts-transform-paths": "^1.7.18",
"babel-plugin-macros": "^3.1.0",
"eslint": "^7.25.0",
"babel-plugin-module-resolver": "^4.1.0",
"concurrently": "^6.0.0",
"eslint": "^8.4.1",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.4.0",
"eslint-import-resolver-typescript": "^2.5.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"prettier": "^2.2.1",
"release-it": "^14.6.1"
"release-it": "^14.6.1",
"ts-migrate": "^0.1.26",
"ts-node": "^10.4.0",
"ttypescript": "^1.5.12",
"typescript": "^4.5.2"
},
"babel": {
"presets": [
Expand Down
7 changes: 0 additions & 7 deletions src/assertions/index.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/assertions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import toExistAsS3Bucket from "./toExistAsS3Bucket";
import toHaveContentEqualTo from "./toHaveContentEqualTo";
import toHaveContentTypeEqualTo from "./toHaveContentTypeEqualTo";
import toHaveEvent from "./toHaveEvent";
import toHaveEventWithSource from "./toHaveEventWithSource";
import toHaveObjectWithNameEqualTo from "./toHaveObjectWithNameEqualTo";
import toExistInDynamoTable from "./toExistInDynamoTable";

export default {
...toExistAsS3Bucket,
...toHaveContentEqualTo,
...toHaveContentTypeEqualTo,
...toHaveEvent,
...toHaveEventWithSource,
...toHaveObjectWithNameEqualTo,
...toExistInDynamoTable,
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";
import { AWSClient } from "helpers/general";
import { testResult, TestResultOutput } from "utils/testResult";
import { is404Error } from "../utils";

export default {
async toExistAsS3Bucket(bucketName) {
async toExistAsS3Bucket(bucketName: string): Promise<TestResultOutput> {
const s3 = new AWSClient.S3();
const params = {
Bucket: bucketName,
Expand All @@ -12,10 +13,12 @@ export default {
try {
await s3.headBucket(params).promise();
message = `expected S3 bucket to exist with name ${bucketName}`;

return testResult(message, true);
} catch (error) {
if (error.statusCode === 404) {
if (is404Error(error)) {
message = `expected S3 bucket to exist with name ${bucketName} - not found`;

return testResult(message, false);
}
throw error;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
import { AWSClient } from "../../helpers/general";
import { TestResultOutput } from "utils/testResult";
import { AWSClient } from "helpers/general";

export default {
async toExistInDynamoTable({ PK, SK }, tableName) {
async toExistInDynamoTable(
{ PK, SK }: { PK: string; SK?: string },
tableName: string
): Promise<TestResultOutput> {
const docClient = new AWSClient.DynamoDB.DocumentClient();

if (!docClient) {
return {
message: () => "expected table to contain document client",
pass: false,
};
}

if (!SK) {
if (SK === undefined) {
const queryParams = {
TableName: tableName,
KeyConditionExpression: "#pk = :pk",
ExpressionAttributeNames: {
"#pk": "PK",
},
ExpressionAttributeValues: {
":pk": PK,
":pk": "PK",
},
Limit: 1,
};

const result = await docClient.query(queryParams).promise();

return {
message: () => `expected to find ${PK} in ${tableName}`,
pass: result.Count === 1,
};
}

const getParams = {
TableName: tableName,
Key: {
Expand All @@ -39,6 +34,7 @@ export default {
},
};
const result = await docClient.get(getParams).promise();

return {
message: () => `expected to find ${PK} in ${tableName}`,
pass: result.Item !== undefined,
Expand Down
33 changes: 0 additions & 33 deletions src/assertions/toHaveContentEqualTo/index.js

This file was deleted.

Loading

0 comments on commit 70cd5b2

Please sign in to comment.