Repository: pulumi/actions
Pull request #61 (2021-02-08 20:37:03)

test: Add tests
This pull request improves unit test coverage.



Patch:
diff --git a/.eslintrc.json b/.eslintrc.json
index a885a99..55fbf15 100644
--- a/.eslintrc.json
+++ b/.eslintrc.json
@@ -34,5 +34,15 @@
       { "argsIgnorePattern": "^_", "caughtErrors": "all" }
     ],
     "@typescript-eslint/no-unused-expressions": "error"
-  }
+  },
+  "overrides": [
+    {
+      "files": ["**/*.test.ts"],
+      "rules": {
+        "@typescript-eslint/ban-ts-comment": "off",
+        "@typescript-eslint/no-explicit-any": "off",
+        "@typescript-eslint/no-unused-vars": "off"
+      }
+    }
+  ]
 }
diff --git a/jest.config.js b/jest.config.js
new file mode 100644
index 0000000..7ff1b0a
--- /dev/null
+++ b/jest.config.js
@@ -0,0 +1,13 @@
+module.exports = {
+  preset: 'ts-jest',
+  testEnvironment: 'node',
+  testPathIgnorePatterns: [
+    '.*/dist/.*',
+    '.*dist.*',
+  ],
+  globals: {
+    'ts-jest': {
+      tsconfig: 'tsconfig.json',
+    },
+  },
+};
diff --git a/src/libs/__mocks__/@actions/exec.ts b/src/libs/__mocks__/@actions/exec.ts
new file mode 100644
index 0000000..d594370
--- /dev/null
+++ b/src/libs/__mocks__/@actions/exec.ts
@@ -0,0 +1,11 @@
+import { ExecOptions } from '@actions/exec';
+
+export const exec = jest.fn(
+  (commandLine: string, _?: string[], options?: ExecOptions) => {
+    if (commandLine === 'succeed') {
+      options.listeners.stdout(Buffer.from('hello world'));
+    } else {
+      options.listeners.stderr(Buffer.from('hello world'));
+    }
+  },
+);


Test patch:
diff --git a/src/libs/__tests__/exec.test.ts b/src/libs/__tests__/exec.test.ts
new file mode 100644
index 0000000..7953397
--- /dev/null
+++ b/src/libs/__tests__/exec.test.ts
@@ -0,0 +1,17 @@
+import * as aexec from '@actions/exec';
+import { exec } from '../exec';
+
+describe('exec.ts', () => {
+  it('should return stdout', async () => {
+      const r = await exec('succeed');
+
+      expect(r.stdout).toBe('hello world');
+      expect(aexec.exec).toHaveBeenCalled();
+  })
+  it('should return stderr', async () => {
+    const r = await exec('fail');
+
+    expect(r.stderr).toBe('hello world');
+    expect(aexec.exec).toHaveBeenCalled();
+  });
+})
diff --git a/src/libs/__tests__/pr.test.ts b/src/libs/__tests__/pr.test.ts
new file mode 100644
index 0000000..edf38fe
--- /dev/null
+++ b/src/libs/__tests__/pr.test.ts
@@ -0,0 +1,41 @@
+import * as gh from '@actions/github';
+import { addPullRequestMessage } from '../pr';
+
+const createComment = jest.fn();
+jest.mock('@actions/github', () => ({
+  context: {},
+  getOctokit: jest.fn(() => ({
+    issues: {
+      createComment,
+    },
+  })),
+}));
+
+describe('pr.ts', () => {
+  beforeEach(() => {
+    jest.resetModules();
+  });
+  it('should add pull request message', async () => {
+    // @ts-ignore
+    gh.context = {
+      payload: {
+        pull_request: {
+          number: 123,
+        },
+      },
+    };
+
+    process.env.GITHUB_REPOSITORY = 'pulumi/actions';
+
+    await addPullRequestMessage('test', 'test');
+    expect(createComment).toHaveBeenCalled();
+  });
+  it('should fail if no pull request data', async () => {
+    process.env.GITHUB_REPOSITORY = 'pulumi/actions';
+    // @ts-ignore
+    gh.context = { payload: {} };
+    await expect(addPullRequestMessage('test', 'test')).rejects.toThrowError(
+      'Missing pull request event data',
+    );
+  });
+});