Skip to content

Commit

Permalink
test: Use spyOn instead of mock.module for logger
Browse files Browse the repository at this point in the history
  • Loading branch information
angelmadames committed Jun 10, 2024
1 parent a70f2ee commit 88d2113
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 92 deletions.
2 changes: 1 addition & 1 deletion src/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ export function toUpperSnakeCase(str: string): string {
// Replace hyphens with underscores
.replace(/-/g, '_')
.toUpperCase()
);
)
}
19 changes: 0 additions & 19 deletions test/config/cli.config.test.ts

This file was deleted.

16 changes: 0 additions & 16 deletions test/config/project.config.test.ts

This file was deleted.

19 changes: 6 additions & 13 deletions test/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { afterEach, beforeAll, jest, mock } from 'bun:test'
import { afterEach, beforeAll, beforeEach, jest, mock, spyOn } from 'bun:test'
import logger from '../src/utils/log'

beforeAll(() => {
mock.module('node:fs', () => ({
Expand All @@ -22,21 +23,13 @@ beforeAll(() => {
mock.module('node:child_process', () => ({
execSync: mock(),
}))

mock.module('../src/utils/log', () => ({
default: {
info: mock(),
warn: mock(),
error: mock(),
},
}))
})

/*
beforeEach(() => {
spyOn(console, 'log').mockImplementation(() => {})
})
*/
spyOn(logger, 'info').mockImplementation(() => {})
spyOn(logger, 'warn').mockImplementation(() => {})
spyOn(logger, 'error').mockImplementation(() => {})
})

afterEach(() => {
jest.clearAllMocks()
Expand Down
36 changes: 18 additions & 18 deletions test/utils/log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ describe('log', () => {
})

test('should trim leading and trailing whitespace', () => {
const result = formatLog(' Hello World ');
expect(result).toBe('Hello World');
});
const result = formatLog(' Hello World ')
expect(result).toBe('Hello World')
})

test('should replace multiple spaces with a single space', () => {
const result = formatLog('Hello World');
expect(result).toBe('Hello World');
});
const result = formatLog('Hello World')
expect(result).toBe('Hello World')
})

test('should handle strings with only whitespace', () => {
const result = formatLog(' ');
expect(result).toBe('');
});
const result = formatLog(' ')
expect(result).toBe('')
})

test('should handle empty strings', () => {
const result = formatLog('');
expect(result).toBe('');
});
const result = formatLog('')
expect(result).toBe('')
})

test('should handle strings with mixed whitespace characters', () => {
const result = formatLog('Hello \t World \n');
expect(result).toBe('Hello World');
});
const result = formatLog('Hello \t World \n')
expect(result).toBe('Hello World')
})

test('should not change a properly formatted string', () => {
const result = formatLog('Hello World');
expect(result).toBe('Hello World');
});
const result = formatLog('Hello World')
expect(result).toBe('Hello World')
})
})
50 changes: 25 additions & 25 deletions test/utils/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,43 +67,43 @@ describe('Utils: string', () => {

describe('toUpperSnakeCase', () => {
test('converts camelCase to UPPER_SNAKE_CASE', () => {
const result = toUpperSnakeCase('camelCaseString');
expect(result).toBe('CAMEL_CASE_STRING');
});
const result = toUpperSnakeCase('camelCaseString')
expect(result).toBe('CAMEL_CASE_STRING')
})

test('converts kebab-case to UPPER_SNAKE_CASE', () => {
const result = toUpperSnakeCase('kebab-case-string');
expect(result).toBe('KEBAB_CASE_STRING');
});
const result = toUpperSnakeCase('kebab-case-string')
expect(result).toBe('KEBAB_CASE_STRING')
})

test('converts mixed camelCase and kebab-case to UPPER_SNAKE_CASE', () => {
const result = toUpperSnakeCase('camelCase-kebab-case');
expect(result).toBe('CAMEL_CASE_KEBAB_CASE');
});
const result = toUpperSnakeCase('camelCase-kebab-case')
expect(result).toBe('CAMEL_CASE_KEBAB_CASE')
})

test('converts single word to UPPER_SNAKE_CASE', () => {
const result = toUpperSnakeCase('word');
expect(result).toBe('WORD');
});
const result = toUpperSnakeCase('word')
expect(result).toBe('WORD')
})

test('handles empty string input', () => {
const result = toUpperSnakeCase('');
expect(result).toBe('');
});
const result = toUpperSnakeCase('')
expect(result).toBe('')
})

test('handles strings with underscores correctly', () => {
const result = toUpperSnakeCase('already_snake_case');
expect(result).toBe('ALREADY_SNAKE_CASE');
});
const result = toUpperSnakeCase('already_snake_case')
expect(result).toBe('ALREADY_SNAKE_CASE')
})

test('handles strings with multiple uppercase letters in sequence', () => {
const result = toUpperSnakeCase('HTTPResponseCode');
expect(result).toBe('HTTP_RESPONSE_CODE');
});
const result = toUpperSnakeCase('HTTPResponseCode')
expect(result).toBe('HTTP_RESPONSE_CODE')
})

test('handles non-alphabetic characters correctly', () => {
const result = toUpperSnakeCase('special@#Characters');
expect(result).toBe('SPECIAL@#CHARACTERS');
});
});
const result = toUpperSnakeCase('special@#Characters')
expect(result).toBe('SPECIAL@#CHARACTERS')
})
})
})

0 comments on commit 88d2113

Please sign in to comment.