Skip to content

Commit

Permalink
feat(tests): add context tests and switch to vitest
Browse files Browse the repository at this point in the history
- Switched from Jest to Vitest for faster test execution.
- Improved config and context providers for consistent behavior.
- Enhanced testing with a generic test setup and utilities.
- Added numerous tests to cover context functionality.
  • Loading branch information
virgofx committed Oct 29, 2024
1 parent ece7b85 commit d8bddfc
Show file tree
Hide file tree
Showing 19 changed files with 2,353 additions and 1,980 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"markdown.extension.orderedList.marker": "one",
"remote.SSH.enableAgentForwarding": true,
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
"editor.defaultFormatter": "biomejs.biome"
},
"[markdown]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ jobs:
VALIDATE_ALL_CODEBASE: true
VALIDATE_JAVASCRIPT_STANDARD: false # Using biome
VALIDATE_JAVASCRIPT_PRETTIER: false # Using biome
VALIDATE_JSON_PRETTIER: false # Using biome
VALIDATE_JSCPD: false # Using biome
VALIDATE_TYPESCRIPT_STANDARD: false # Using biome
VALIDATE_TYPESCRIPT_ES: false # Using biome
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name: Test

on:
# Trigger analysis when pushing in master or pull requests, and when creating
# a pull request.
Expand All @@ -7,10 +9,10 @@ on:
pull_request:
types: [opened, synchronize, reopened]

name: Test
jobs:
tests:
runs-on: ubuntu-latest
name: Test
permissions:
contents: read
steps:
Expand Down
51 changes: 51 additions & 0 deletions __tests__/__mocks__/config.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { merge } from 'ts-deepmerge';
import { vi } from 'vitest';
import type { Config } from '../../src/config';

type InputMap = {
[key: string]: string;
};

const defaultInputs: InputMap = {
'major-keywords': 'BREAKING CHANGE,!',
'minor-keywords': 'feat,feature',
'patch-keywords': 'fix,chore',
'default-first-tag': 'v0.1.0',
'terraform-docs-version': 'v0.16.0',
'delete-legacy-tags': 'false',
'disable-wiki': 'false',
'wiki-sidebar-changelog-max': '10',
'disable-branding': 'false',
'module-change-exclude-patterns': '.gitignore,*.md',
'module-asset-exclude-patterns': 'tests/**,examples/**',
github_token: 'test-token',
};

const defaultConfig: Config = {
majorKeywords: ['BREAKING CHANGE', '!'],
minorKeywords: ['feat', 'feature'],
patchKeywords: ['fix', 'chore'],
defaultFirstTag: 'v0.1.0',
terraformDocsVersion: 'v0.19.0',
deleteLegacyTags: false,
disableWiki: false,
wikiSidebarChangelogMax: 10,
disableBranding: false,
moduleChangeExcludePatterns: ['.gitignore', '*.md'],
moduleAssetExcludePatterns: ['tests/**', 'examples/**'],
githubToken: 'ghp_test_token_2c6912E7710c838347Ae178B4',

Check failure

Code scanning / SonarCloud

GitHub tokens should not be disclosed High test

Make sure this Github token gets revoked, changed, and removed from the code. See more on SonarCloud
};

// Create a mock factory function
export const createConfigMock = (overrides: Partial<Config> = {}) => ({
...defaultConfig,
...overrides,
});

// Create a mock inputs factory function
export function createInputsMock(inputs: InputMap = {}): InputMap {
return merge(defaultInputs, inputs);
}

// Create the mock handler
export const configMock = vi.fn(() => defaultConfig);
93 changes: 93 additions & 0 deletions __tests__/__mocks__/context.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { merge } from 'ts-deepmerge';
import { vi } from 'vitest';
import type { Context } from '../../src/context';

// Only mock the methods we need
const octokitMock = {
rest: {
git: {
deleteRef: vi.fn(),
},
issues: {
createComment: vi.fn(),
deleteComment: vi.fn(),
listComments: vi.fn(),
listForRepo: vi.fn().mockResolvedValue({
data: [
{
number: 1,
title: 'issue 1',
body: 'issue 1 body',
},
{
number: 2,
title: 'issue 2',
body: 'issue 2 body',
},
],
}),
},
pulls: {
listCommits: vi.fn().mockResolvedValue({
data: [
{ sha: 'sha1', committer: { name: '<NAME>' } },
{ sha: 'sha2', committer: { name: '<NAME>' } },
],
}),
listFiles: vi.fn().mockResolvedValue({
data: [{ filename: 'file1.txt' }, { filename: 'file2.txt' }],
}),
},
repos: {
getCommit: vi.fn().mockResolvedValue({
data: {
sha: '1234567890',
},
}),
listTags: vi.fn(),
listReleases: vi.fn(),
},
},
paginate: vi.fn(),
};

const defaultContext: Context = {
repo: {
owner: 'techpivot',
repo: 'terraform-module-releaser',
},
repoUrl: 'https://github.com/techpivot/terraform-module-releaser',
octokit: octokitMock as unknown as Context['octokit'],
prNumber: 1,
prTitle: 'Test Pull Request',
prBody: 'This is a test pull request body.',
issueNumber: 1,
workspaceDir: '/path/to/workspace',
isPrMergeEvent: false,
};

const defaultPullRequestPayload = {
action: 'opened',
pull_request: {
number: 123,
title: 'Test PR',
body: 'Test PR body',
merged: false,
},
repository: {
full_name: 'techpivot/terraform-module-releaser',
},
};

// Create a mock context factory function
//export function createContextMock(overrides: Partial<Context> = {}): Context {
// return merge(defaultContext, overrides);
//}

// Create a mock pull request factory function
export function createPullRequestMock(overrides = {}) {
return merge(defaultPullRequestPayload, overrides);
}

// Create the mock handler
export const contextMock = vi.fn(() => defaultContext);
Loading

0 comments on commit d8bddfc

Please sign in to comment.