From 1f3d2a6328a5a8718224e53468396a4ca6008400 Mon Sep 17 00:00:00 2001 From: marocchino Date: Sat, 14 Aug 2021 09:48:10 +0900 Subject: [PATCH] test: noImplicitAny --- __tests__/comment.test.ts | 84 +++++++++++++++------------------------ tsconfig.json | 14 +++---- 2 files changed, 39 insertions(+), 59 deletions(-) diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts index aab41df..7b926b1 100644 --- a/__tests__/comment.test.ts +++ b/__tests__/comment.test.ts @@ -1,12 +1,13 @@ -import { - findPreviousComment, - createComment, - updateComment, - deleteComment -} from "../src/comment" - +import {getOctokit} from "@actions/github" import * as core from "@actions/core" +import { + createComment, + deleteComment, + findPreviousComment, + updateComment +} from "../src/comment" + jest.mock("@actions/core", () => ({ warning: jest.fn() })) @@ -48,28 +49,17 @@ it("findPreviousComment", async () => { body: "previous message\n" } ] - const octokit: any = { - graphql: jest.fn(() => - Promise.resolve({ - viewer: authenticatedUser - }) - ), - rest: { - issues: { - listComments: jest.fn(() => - Promise.resolve({ - data: [ - commentWithCustomHeader, - otherUserComment, - comment, - headerFirstComment, - ...otherComments - ] - }) - ) - } - } - } + const octokit = getOctokit("github-token") + jest.spyOn(octokit, "graphql").mockResolvedValue({viewer: authenticatedUser}) + jest.spyOn(octokit.rest.issues, "listComments").mockResolvedValue({ + data: [ + commentWithCustomHeader, + otherUserComment, + comment, + headerFirstComment, + ...otherComments + ] + } as any) expect(await findPreviousComment(octokit, repo, 123, "")).toBe(comment) expect(await findPreviousComment(octokit, repo, 123, "TypeA")).toBe( @@ -86,16 +76,12 @@ it("findPreviousComment", async () => { }) describe("updateComment", () => { - let octokit + const octokit = getOctokit("github-token") beforeEach(() => { - octokit = { - rest: { - issues: { - updateComment: jest.fn(() => Promise.resolve()) - } - } - } + jest + .spyOn(octokit.rest.issues, "updateComment") + .mockResolvedValue("") }) it("with comment body", async () => { @@ -143,16 +129,12 @@ describe("updateComment", () => { }) describe("createComment", () => { - let octokit + const octokit = getOctokit("github-token") beforeEach(() => { - octokit = { - rest: { - issues: { - createComment: jest.fn(() => Promise.resolve()) - } - } - } + jest + .spyOn(octokit.rest.issues, "createComment") + .mockResolvedValue("") }) it("with comment body or previousBody", async () => { @@ -183,13 +165,11 @@ describe("createComment", () => { }) it("deleteComment", async () => { - const octokit: any = { - rest: { - issues: { - deleteComment: jest.fn(() => Promise.resolve()) - } - } - } + const octokit = getOctokit("github-token") + + jest + .spyOn(octokit.rest.issues, "deleteComment") + .mockReturnValue(undefined as any) expect(await deleteComment(octokit, repo, 456)).toBeUndefined() expect(octokit.rest.issues.deleteComment).toBeCalledWith({ comment_id: 456, diff --git a/tsconfig.json b/tsconfig.json index 89b9d03..4a6c661 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,12 +1,12 @@ { "compilerOptions": { - "target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */ - "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ - "outDir": "./lib", /* Redirect output structure to the directory. */ - "rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ - "strict": true, /* Enable all strict type-checking options. */ - "noImplicitAny": false, /* Raise error on expressions and declarations with an implied 'any' type. */ - "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + "target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */, + "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, + "outDir": "./lib" /* Redirect output structure to the directory. */, + "rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */, + "strict": true /* Enable all strict type-checking options. */, + "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */, + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ }, "exclude": ["node_modules", "**/*.test.ts"] }