-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add istanbul coverage provider #399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
travzhang
wants to merge
9
commits into
web-infra-dev:main
Choose a base branch
from
travzhang:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2b6156c
feat: add coverage configuration and support for Istanbul integration
travzhang e96163e
feat: istanbul instrument with rspack
travzhang 023bd6e
feat: add examples
travzhang c0db91c
feat: support both JavaScript and TypeScript files in Istanbul coverage
travzhang d12ff69
feat: replace Babel with SWC for coverage instrumentation
travzhang 840693a
fix: add optional chaining for coverage configuration checks
travzhang e851be1
Merge branch 'web-infra-dev:main' into main
travzhang 81c17c5
feat: add @rstest/coverage-istanbul package
travzhang fa61e02
fix: merge main
travzhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,5 @@ test-temp-* | |
test-results/ | ||
fixtures-test/ | ||
.rslib/ | ||
|
||
!packages/core/src/coverage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
import { defineConfig } from '@rstest/core'; | ||
|
||
export default defineConfig({}); | ||
export default defineConfig({ | ||
coverage: { | ||
enabled: true, | ||
provider: 'istanbul', | ||
}, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
export const removeDuplicates = <T>(arr: T[]): T[] => { | ||
return [...new Set(arr)]; | ||
}; | ||
|
||
export const chunk = <T>(arr: T[], size: number): T[][] => { | ||
if (size <= 0) { | ||
throw new Error('Chunk size must be greater than 0'); | ||
} | ||
const result: T[][] = []; | ||
for (let i = 0; i < arr.length; i += size) { | ||
result.push(arr.slice(i, i + size)); | ||
} | ||
return result; | ||
}; | ||
|
||
export const flatten = <T>(arr: (T | T[])[]): T[] => { | ||
return arr.reduce<T[]>((acc, val) => { | ||
return acc.concat(Array.isArray(val) ? flatten(val) : val); | ||
}, []); | ||
}; | ||
|
||
export const findMax = (arr: number[]): number => { | ||
if (arr.length === 0) { | ||
throw new Error('Array cannot be empty'); | ||
} | ||
return Math.max(...arr); | ||
}; | ||
|
||
export const shuffle = <T>(arr: T[]): T[] => { | ||
const shuffled = [...arr]; | ||
for (let i = shuffled.length - 1; i > 0; i--) { | ||
const j = Math.floor(Math.random() * (i + 1)); | ||
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; | ||
} | ||
return shuffled; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export const formatDate = (date: Date, format = 'YYYY-MM-DD'): string => { | ||
const year = date.getFullYear(); | ||
const month = String(date.getMonth() + 1).padStart(2, '0'); | ||
const day = String(date.getDate()).padStart(2, '0'); | ||
|
||
return format | ||
.replace('YYYY', year.toString()) | ||
.replace('MM', month) | ||
.replace('DD', day); | ||
}; | ||
|
||
export const addDays = (date: Date, days: number): Date => { | ||
const result = new Date(date); | ||
result.setDate(result.getDate() + days); | ||
return result; | ||
}; | ||
|
||
export const getDaysBetween = (date1: Date, date2: Date): number => { | ||
const timeDiff = Math.abs(date2.getTime() - date1.getTime()); | ||
return Math.ceil(timeDiff / (1000 * 3600 * 24)); | ||
}; | ||
|
||
export const isWeekend = (date: Date): boolean => { | ||
const day = date.getDay(); | ||
return day === 0 || day === 6; // Sunday = 0, Saturday = 6 | ||
}; | ||
|
||
export const getQuarter = (date: Date): number => { | ||
return Math.floor((date.getMonth() + 3) / 3); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export const capitalize = (str: string): string => { | ||
if (!str) return str; | ||
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(); | ||
}; | ||
|
||
export const reverseString = (str: string): string => { | ||
return str.split('').reverse().join(''); | ||
}; | ||
|
||
export const isPalindrome = (str: string): boolean => { | ||
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, ''); | ||
return cleaned === cleaned.split('').reverse().join(''); | ||
}; | ||
|
||
export const countWords = (str: string): number => { | ||
return str | ||
.trim() | ||
.split(/\s+/) | ||
.filter((word) => word.length > 0).length; | ||
}; | ||
|
||
export const truncate = (str: string, maxLength: number): string => { | ||
if (str.length <= maxLength) return str; | ||
return str.slice(0, maxLength - 3) + '...'; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { describe, expect, it } from '@rstest/core'; | ||
import { | ||
chunk, | ||
findMax, | ||
flatten, | ||
removeDuplicates, | ||
shuffle, | ||
} from '../src/array'; | ||
|
||
describe('Array Utils', () => { | ||
describe('removeDuplicates', () => { | ||
it('should remove duplicate numbers', () => { | ||
expect(removeDuplicates([1, 2, 2, 3, 3, 4])).toEqual([1, 2, 3, 4]); | ||
}); | ||
|
||
it('should remove duplicate strings', () => { | ||
expect(removeDuplicates(['a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']); | ||
}); | ||
|
||
it('should handle empty array', () => { | ||
expect(removeDuplicates([])).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('chunk', () => { | ||
it('should chunk array into specified size', () => { | ||
expect(chunk([1, 2, 3, 4, 5, 6], 2)).toEqual([ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
]); | ||
}); | ||
|
||
it('should handle remainder elements', () => { | ||
expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]); | ||
}); | ||
|
||
it('should throw error for invalid chunk size', () => { | ||
expect(() => chunk([1, 2, 3], 0)).toThrow( | ||
'Chunk size must be greater than 0', | ||
); | ||
}); | ||
}); | ||
|
||
describe('flatten', () => { | ||
it('should flatten nested arrays', () => { | ||
expect(flatten([1, [2, 3], [4, [5, 6]]])).toEqual([1, 2, 3, 4, 5, 6]); | ||
}); | ||
|
||
it('should handle empty arrays', () => { | ||
expect(flatten([])).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('findMax', () => { | ||
it('should find maximum number', () => { | ||
expect(findMax([1, 5, 3, 9, 2])).toBe(9); | ||
}); | ||
|
||
it('should throw error for empty array', () => { | ||
expect(() => findMax([])).toThrow('Array cannot be empty'); | ||
}); | ||
}); | ||
|
||
describe('shuffle', () => { | ||
it('should return array with same length', () => { | ||
const original = [1, 2, 3, 4, 5]; | ||
const shuffled = shuffle(original); | ||
expect(shuffled).toHaveLength(original.length); | ||
}); | ||
|
||
it('should contain all original elements', () => { | ||
const original = [1, 2, 3, 4, 5]; | ||
const shuffled = shuffle(original); | ||
expect(shuffled.sort()).toEqual(original.sort()); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { describe, expect, it } from '@rstest/core'; | ||
import { | ||
addDays, | ||
formatDate, | ||
getDaysBetween, | ||
getQuarter, | ||
isWeekend, | ||
} from '../src/date'; | ||
|
||
describe('Date Utils', () => { | ||
describe('formatDate', () => { | ||
it('should format date with default format', () => { | ||
const date = new Date('2023-12-25'); | ||
expect(formatDate(date)).toBe('2023-12-25'); | ||
}); | ||
|
||
it('should format date with custom format', () => { | ||
const date = new Date('2023-12-25'); | ||
expect(formatDate(date, 'DD/MM/YYYY')).toBe('25/12/2023'); | ||
}); | ||
}); | ||
|
||
describe('addDays', () => { | ||
it('should add days to date', () => { | ||
const date = new Date('2023-12-25'); | ||
const result = addDays(date, 5); | ||
expect(result.getDate()).toBe(30); | ||
}); | ||
|
||
it('should subtract days from date', () => { | ||
const date = new Date('2023-12-25'); | ||
const result = addDays(date, -5); | ||
expect(result.getDate()).toBe(20); | ||
}); | ||
}); | ||
|
||
describe('getDaysBetween', () => { | ||
it('should calculate days between two dates', () => { | ||
const date1 = new Date('2023-12-25'); | ||
const date2 = new Date('2023-12-30'); | ||
expect(getDaysBetween(date1, date2)).toBe(5); | ||
}); | ||
|
||
it('should handle reversed dates', () => { | ||
const date1 = new Date('2023-12-30'); | ||
const date2 = new Date('2023-12-25'); | ||
expect(getDaysBetween(date1, date2)).toBe(5); | ||
}); | ||
}); | ||
|
||
describe('isWeekend', () => { | ||
it('should return true for Saturday', () => { | ||
const saturday = new Date('2023-12-23'); // Saturday | ||
expect(isWeekend(saturday)).toBe(true); | ||
}); | ||
|
||
it('should return true for Sunday', () => { | ||
const sunday = new Date('2023-12-24'); // Sunday | ||
expect(isWeekend(sunday)).toBe(true); | ||
}); | ||
|
||
it('should return false for weekday', () => { | ||
const monday = new Date('2023-12-25'); // Monday | ||
expect(isWeekend(monday)).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('getQuarter', () => { | ||
it('should return correct quarter for different months', () => { | ||
expect(getQuarter(new Date('2023-01-15'))).toBe(1); | ||
expect(getQuarter(new Date('2023-04-15'))).toBe(2); | ||
expect(getQuarter(new Date('2023-07-15'))).toBe(3); | ||
expect(getQuarter(new Date('2023-10-15'))).toBe(4); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { describe, expect, it } from '@rstest/core'; | ||
import { | ||
capitalize, | ||
countWords, | ||
isPalindrome, | ||
reverseString, | ||
truncate, | ||
} from '../src/string'; | ||
|
||
describe('String Utils', () => { | ||
describe('capitalize', () => { | ||
it('should capitalize first letter', () => { | ||
expect(capitalize('hello')).toBe('Hello'); | ||
}); | ||
|
||
it('should handle empty string', () => { | ||
expect(capitalize('')).toBe(''); | ||
}); | ||
|
||
it('should handle single character', () => { | ||
expect(capitalize('a')).toBe('A'); | ||
}); | ||
}); | ||
|
||
describe('reverseString', () => { | ||
it('should reverse string', () => { | ||
expect(reverseString('hello')).toBe('olleh'); | ||
}); | ||
|
||
it('should handle empty string', () => { | ||
expect(reverseString('')).toBe(''); | ||
}); | ||
|
||
it('should handle palindrome', () => { | ||
expect(reverseString('racecar')).toBe('racecar'); | ||
}); | ||
}); | ||
|
||
describe('isPalindrome', () => { | ||
it('should return true for palindrome', () => { | ||
expect(isPalindrome('racecar')).toBe(true); | ||
}); | ||
|
||
it('should return true for palindrome with spaces', () => { | ||
expect(isPalindrome('A man a plan a canal Panama')).toBe(true); | ||
}); | ||
|
||
it('should return false for non-palindrome', () => { | ||
expect(isPalindrome('hello')).toBe(false); | ||
}); | ||
|
||
it('should handle empty string', () => { | ||
expect(isPalindrome('')).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('countWords', () => { | ||
it('should count words correctly', () => { | ||
expect(countWords('hello world')).toBe(2); | ||
}); | ||
|
||
it('should handle multiple spaces', () => { | ||
expect(countWords('hello world test')).toBe(3); | ||
}); | ||
|
||
it('should handle empty string', () => { | ||
expect(countWords('')).toBe(0); | ||
}); | ||
|
||
it('should handle single word', () => { | ||
expect(countWords('hello')).toBe(1); | ||
}); | ||
}); | ||
|
||
describe('truncate', () => { | ||
it('should truncate long string', () => { | ||
expect(truncate('This is a very long string', 10)).toBe('This is...'); | ||
}); | ||
|
||
it('should not truncate short string', () => { | ||
expect(truncate('Short', 10)).toBe('Short'); | ||
}); | ||
|
||
it('should handle exact length', () => { | ||
expect(truncate('Exactly10!', 10)).toBe('Exactly10!'); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Builtin istanbul coverage directly into Rstest is too heavy. Rstest can provide a separate package for users to install as needed. When users set
provider: 'istanbul'
, rstest can import the relevant transform plugin and provider from@rstest/coverage-istanbul
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌