Skip to content

Commit

Permalink
fix: remove most any types
Browse files Browse the repository at this point in the history
  • Loading branch information
StanHannebelle committed Jan 21, 2022
1 parent c48c64b commit df8c58e
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 84 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module.exports = {
overrides: [
{
files: ["**/*.ts?(x)"],
settings: { "import/resolver": { typescript: {} } },
extends: [
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
Expand Down
3 changes: 2 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const plugins = [
[
"module-resolver",
{
root: ["."],
root: ["./src"],
extensions: [".ts"],
},
],
"@babel/plugin-transform-runtime",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"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": {
Expand Down
2 changes: 2 additions & 0 deletions src/assertions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import toHaveContentTypeEqualTo from "./toHaveContentTypeEqualTo";
import toHaveEvent from "./toHaveEvent";
import toHaveEventWithSource from "./toHaveEventWithSource";
import toHaveObjectWithNameEqualTo from "./toHaveObjectWithNameEqualTo";
import toExistInDynamoTable from "./toExistInDynamoTable";

export default {
...toExistAsS3Bucket,
Expand All @@ -12,4 +13,5 @@ export default {
...toHaveEvent,
...toHaveEventWithSource,
...toHaveObjectWithNameEqualTo,
...toExistInDynamoTable,
};
6 changes: 3 additions & 3 deletions src/assertions/toExistAsS3Bucket/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +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: any) {
async toExistAsS3Bucket(bucketName: string): Promise<TestResultOutput> {
const s3 = new AWSClient.S3();
const params = {
Bucket: bucketName,
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
27 changes: 22 additions & 5 deletions src/assertions/toHaveContentEqualTo/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";
import { AWSClient } from "helpers/general";
import { testResult, TestResultOutput } from "utils/testResult";
import { isNoSuchBucketError, isNoSuchKeyError } from "../utils";

export default {
async toHaveContentEqualTo({ bucketName, objectName }: any, content: any) {
// Import & use s3 type ?
async toHaveContentEqualTo(
{ bucketName, objectName }: { bucketName: string; objectName: string },
content: Record<string, unknown> | string
): Promise<TestResultOutput> {
const s3 = new AWSClient.S3();
const params = {
Bucket: bucketName,
Expand All @@ -14,11 +18,24 @@ export default {
try {
const object = await s3.getObject(params).promise();
if (JSON.stringify(object.Body) === JSON.stringify(content)) {
message = `expected ${objectName} to have content ${content}`;
message = `expected ${objectName} to have content ${JSON.stringify(
content
)}`;

return testResult(message, true);
}
message = `expected ${objectName} to have content ${content}, but content found was ${object.Body}`;
const stringifiedObjectBody = object.Body?.toString();
if (stringifiedObjectBody === undefined) {
message = `expected ${objectName} to have content ${JSON.stringify(
content
)}, but content found was undefined`;

return testResult(message, false);
}

message = `expected ${objectName} to have content ${JSON.stringify(
content
)}, but content found was ${stringifiedObjectBody}`;

return testResult(message, false);
} catch (error) {
Expand Down
14 changes: 8 additions & 6 deletions src/assertions/toHaveContentTypeEqualTo/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";
import { AWSClient } from "helpers/general";
import { testResult, TestResultOutput } from "utils/testResult";
import { isNoSuchBucketError, isNoSuchKeyError } from "../utils";

export default {
async toHaveContentTypeEqualTo(
{ bucketName, objectName }: any,
contentType: any
) {
{ bucketName, objectName }: { bucketName: string; objectName: string },
contentType: string
): Promise<TestResultOutput> {
const s3 = new AWSClient.S3();
const params = {
Bucket: bucketName,
Expand All @@ -21,7 +21,9 @@ export default {

return testResult(message, true);
}
message = `expected ${objectName} to have content type ${contentType}, but content type found was ${object.ContentType}`;
message = `expected ${objectName} to have content type ${contentType}, but content type found was ${
object.ContentType ?? "undefined"
}`;

return testResult(message, false);
} catch (error) {
Expand Down
19 changes: 10 additions & 9 deletions src/assertions/toHaveEvent/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { testResult } from "../../utils/testResult";
import { SQS } from "aws-sdk";
import { testResult, TestResultOutput } from "utils/testResult";

export default {
toHaveEvent(eventBridgeEvents: any) {
let message;
if (eventBridgeEvents) {
message = "expected to have message in EventBridge Bus";

return testResult(message, true);
toHaveEvent(eventBridgeEvents?: SQS.ReceiveMessageResult): TestResultOutput {
if (
eventBridgeEvents === undefined ||
eventBridgeEvents.Messages === undefined ||
eventBridgeEvents.Messages.length === 0
) {
return testResult("no message intercepted from EventBridge Bus", false);
}
message = "no message intercepted from EventBridge Bus";

return testResult(message, false);
return testResult("expected to have message in EventBridge Bus", true);
},
};
17 changes: 11 additions & 6 deletions src/assertions/toHaveEventWithSource/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { testResult } from "../../utils/testResult";
import { testResult, TestResultOutput } from "utils/testResult";

export default {
toHaveEventWithSource(eventBridgeEvents: any, expectedSourceName: any) {
toHaveEventWithSource(
{ Messages }: { Messages: [{ Body: string }] },
expectedSourceName: string
): TestResultOutput {
let message;
const receivedSource = JSON.parse(eventBridgeEvents.Messages[0].Body)
.source;
if (receivedSource === expectedSourceName) {

const parsedBody = JSON.parse(Messages[0].Body) as { source?: string };
if (parsedBody.source === expectedSourceName) {
message = `expected sent event to have source ${expectedSourceName}`;

return testResult(message, true);
}
message = `sent event source "${receivedSource}" does not match expected source "${expectedSourceName}"`;
message = `sent event source "${
parsedBody.source ?? "undefined"
}" does not match expected source "${expectedSourceName}"`;

return testResult(message, false);
},
Expand Down
9 changes: 6 additions & 3 deletions src/assertions/toHaveObjectWithNameEqualTo/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { AWSClient } from "../../helpers/general";
import { testResult } from "../../utils/testResult";
import { AWSClient } from "helpers/general";
import { testResult, TestResultOutput } from "utils/testResult";
import { isNoSuchKeyError } from "../utils";

export default {
async toHaveS3ObjectWithNameEqualTo(bucketName: any, objectName: any) {
async toHaveS3ObjectWithNameEqualTo(
bucketName: string,
objectName: string
): Promise<TestResultOutput> {
const s3 = new AWSClient.S3();
const params = {
Bucket: bucketName,
Expand Down
14 changes: 8 additions & 6 deletions src/assertions/utils/errorTypesCheckers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
type Error404 = { statusCode: 404 };
export const is404Error = (error: any): error is Error404 =>
error.statusCode === 404;
export const is404Error = (error: unknown): error is Error404 =>
(error as { statusCode?: number }).statusCode === 404;

type ErrorNoSuchKey = { code: "NoSuchKey" };
export const isNoSuchKeyError = (error: any): error is ErrorNoSuchKey =>
error.code === "NoSuchKey";
export const isNoSuchKeyError = (error: unknown): error is ErrorNoSuchKey =>
(error as { code?: string }).code === "NoSuchKey";

type ErrorNoSuchBucket = { code: "NoSuchBucket" };
export const isNoSuchBucketError = (error: any): error is ErrorNoSuchBucket =>
error.code === "NoSuchBucket";
export const isNoSuchBucketError = (
error: unknown
): error is ErrorNoSuchBucket =>
(error as { code?: string }).code === "NoSuchBucket";
2 changes: 2 additions & 0 deletions src/assertions/utils/globalTypeChecker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
type GlobalWithExpectKey = { expect: any };
export const isGlobalWithExpectKey = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
global: any
): global is GlobalWithExpectKey => "expect" in global;
Loading

0 comments on commit df8c58e

Please sign in to comment.