Skip to content

Commit

Permalink
fix: move config and context mocks into __mocks__ (#118)
Browse files Browse the repository at this point in the history
This move will still keep existing functionality the same; however,
it will ensure that SonarCloud doesn't pick up the src/__mocks__
as code that needs to contain tests.
  • Loading branch information
virgofx authored Nov 26, 2024
1 parent 2571785 commit 6c03498
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 8 deletions.
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions __tests__/_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { afterEach, beforeEach, vi } from 'vitest';
vi.mock('@actions/core');

// Mocked internal modules
vi.mock('@/config');
vi.mock('@/context');
vi.mock('@/config', () => import('@/mocks/config'));
vi.mock('@/context', () => import('@/mocks/context'));

// Mock console time/timeEnd to be a no-op
vi.spyOn(console, 'time').mockImplementation(() => {});
Expand All @@ -24,6 +24,9 @@ beforeEach(() => {
for (const [key, value] of Object.entries(defaultEnvironmentVariables)) {
vi.stubEnv(key, value);
}

// Clear all mocked functions usage data and state
vi.clearAllMocks();
});

afterEach(() => {
Expand Down
5 changes: 2 additions & 3 deletions __tests__/wiki.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ describe('wiki', async () => {
);

beforeEach(() => {
// Reset mocks and context
vi.clearAllMocks();

tmpDir = mkdtempSync(join(tmpdir(), 'wiki-test-'));
wikiDir = join(tmpDir, '.wiki');
mkdirSync(wikiDir);
Expand All @@ -62,6 +59,7 @@ describe('wiki', async () => {
workspaceDir: tmpDir,
prBody: 'Test PR body',
prNumber: 123,
issueNumber: 123,
prTitle: 'Test PR title',
});
});
Expand Down Expand Up @@ -181,6 +179,7 @@ describe('wiki', async () => {
});

it('should generate all required wiki files', async () => {
vi.clearAllMocks();
const files = await generateWikiFiles(terraformModules);

// Get all expected file basenames from fixtures
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"newLine": "lf",
"paths": {
"@/*": ["./src/*"],
"@/mocks/*": ["./src/__mocks__/*"],
"@/mocks/*": ["./__mocks__/*"],
"@/tests/*": ["./__tests__/*"]
}
},
Expand Down
4 changes: 2 additions & 2 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export default defineConfig({
provider: 'v8',
reporter: ['json-summary', 'text', 'lcov'],
include: ['src'],
exclude: ['__tests__', '__mocks__', 'src/__mocks__', 'src/types'],
exclude: ['__tests__', '__mocks__', 'src/types'],
},
setupFiles: ['__tests__/_setup'],
include: ['__tests__/**/*.test.ts'],
forceRerunTriggers: ['**/vitest.config.*/**', '**/__mocks__/**/*', '__tests__/_setup.ts'],
alias: {
'@/tests/': `${resolve(__dirname, '__tests__')}/`,
'@/mocks/': `${resolve(__dirname, 'src/__mocks__')}/`,
'@/mocks/': `${resolve(__dirname, '__mocks__')}/`,
'@/': `${resolve(__dirname, 'src')}/`,
},
},
Expand Down

0 comments on commit 6c03498

Please sign in to comment.