Skip to content

Commit a58eaab

Browse files
authored
test(terraform-module): add terraform-module tests (#113)
- Adds full coverage for the terraform-module component - Refactor shared types into `/src/types` - Add one additional function into `/utils/string` - Improve Octokit helper to leverage proper types
1 parent 0ff826e commit a58eaab

30 files changed

+1002
-448
lines changed

__tests__/_setup.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ vi.mock('@actions/core');
77
vi.mock('@/config');
88
vi.mock('@/context');
99

10+
// Mock console time/timeEnd to be a no-op
11+
vi.spyOn(console, 'time').mockImplementation(() => {});
12+
vi.spyOn(console, 'timeEnd').mockImplementation(() => {});
13+
1014
const defaultEnvironmentVariables = {
1115
GITHUB_EVENT_NAME: 'pull_request',
1216
GITHUB_REPOSITORY: 'techpivot/terraform-module-releaser',

__tests__/changelog.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { getModuleChangelog, getModuleReleaseChangelog, getPullRequestChangelog } from '@/changelog';
22
import { context } from '@/mocks/context';
3-
import type { TerraformChangedModule, TerraformModule } from '@/terraform-module';
3+
import type { TerraformChangedModule, TerraformModule } from '@/types';
44
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
55

66
describe('changelog', () => {
77
const mockDate = new Date('2024-11-05');
88

99
beforeEach(() => {
10-
vi.useFakeTimers();
10+
//vi.useFakeTimers();
1111
vi.setSystemTime(mockDate);
1212

1313
// Reset context mock before each test

__tests__/config.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { clearConfigForTesting, config, getConfig } from '@/config';
2+
import { booleanConfigKeys, booleanInputs, requiredInputs, stubInputEnv } from '@/tests/helpers/inputs';
23
import { endGroup, getBooleanInput, getInput, info, startGroup } from '@actions/core';
34
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
45

5-
import { booleanConfigKeys, booleanInputs, requiredInputs, stubInputEnv } from '@/tests/helpers/inputs';
6-
76
describe('config', () => {
87
beforeAll(() => {
98
// We globally mock context to facilitate majority of testing; however,

__tests__/helpers/inputs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Config } from '@/config';
1+
import type { Config } from '@/types';
22
import { vi } from 'vitest';
33

44
const INPUT_KEY = 'INPUT_';

__tests__/helpers/octokit.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { OctokitRestApi } from '@/types';
12
import { trimSlashes } from '@/utils/string';
23
import { paginateRest } from '@octokit/plugin-paginate-rest';
34
import { restEndpointMethods } from '@octokit/plugin-rest-endpoint-methods';
@@ -276,7 +277,7 @@ function createPaginatedMockImplementation<K extends keyof EndpointNames, M exte
276277
*
277278
* @returns Mock Octokit instance
278279
*/
279-
export function createDefaultOctokitMock() {
280+
export function createDefaultOctokitMock(): OctokitRestApi {
280281
resetMockStore();
281282

282283
const mockOctokit = {
@@ -325,15 +326,16 @@ export function createDefaultOctokitMock() {
325326
},
326327
};
327328

328-
return mockOctokit;
329+
// For now, we just emulate and return a Partial OctokitRestApi since this is a mocked version
330+
return mockOctokit as unknown as OctokitRestApi;
329331
}
330332

331333
/**
332334
* Creates a real Octokit instance for integration testing
333335
*
334336
* @returns Promise containing configured Octokit instance
335337
*/
336-
export async function createRealOctokit() {
338+
export async function createRealOctokit(): Promise<OctokitRestApi> {
337339
const realOctokit = (await vi.importActual('@octokit/core')) as typeof import('@octokit/core');
338340
const OctokitWithPaginateAndRest = realOctokit.Octokit.plugin(restEndpointMethods, paginateRest);
339341

__tests__/index.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as main from '@/main';
2+
import { describe, expect, it, vi } from 'vitest';
3+
4+
// Mock the main module's run function
5+
vi.spyOn(main, 'run').mockImplementation(async () => {});
6+
7+
describe('index', () => {
8+
it('calls run when imported', async () => {
9+
await import('@/index');
10+
expect(main.run).toHaveBeenCalled();
11+
});
12+
});

__tests__/pull-request.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { config } from '@/mocks/config';
22
import { context } from '@/mocks/context';
33
import { addPostReleaseComment, addReleasePlanComment, getPullRequestCommits, hasReleaseComment } from '@/pull-request';
4-
import type { GitHubRelease } from '@/releases';
5-
import type { TerraformChangedModule } from '@/terraform-module';
64
import { stubOctokitImplementation, stubOctokitReturnData } from '@/tests/helpers/octokit';
5+
import type { GitHubRelease, TerraformChangedModule } from '@/types';
76
import { BRANDING_COMMENT, GITHUB_ACTIONS_BOT_USER_ID, PR_RELEASE_MARKER, PR_SUMMARY_MARKER } from '@/utils/constants';
87
import { WikiStatus } from '@/wiki';
98
import { debug, endGroup, info, startGroup } from '@actions/core';

__tests__/releases.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { config } from '@/mocks/config';
22
import { context } from '@/mocks/context';
33
import { createTaggedRelease, deleteLegacyReleases, getAllReleases } from '@/releases';
4-
import type { GitHubRelease } from '@/releases';
5-
import type { TerraformChangedModule } from '@/terraform-module';
64
import { stubOctokitReturnData } from '@/tests/helpers/octokit';
5+
import type { GitHubRelease, TerraformChangedModule } from '@/types';
76
import { debug, endGroup, info, startGroup } from '@actions/core';
87
import { RequestError } from '@octokit/request-error';
98
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';

__tests__/terraform-docs.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { join } from 'node:path';
55
import { promisify } from 'node:util';
66
import { context } from '@/mocks/context';
77
import { ensureTerraformDocsConfigDoesNotExist, generateTerraformDocs, installTerraformDocs } from '@/terraform-docs';
8-
import type { TerraformModule } from '@/terraform-module';
8+
import type { TerraformModule } from '@/types';
99
import { info } from '@actions/core';
1010
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
1111
import which from 'which';

0 commit comments

Comments
 (0)