Skip to content

Commit 9b0bb2e

Browse files
travzhang9aoy
andauthored
feat: add istanbul coverage provider (#399)
Co-authored-by: gaoyuan.1226 <[email protected]>
1 parent b9f508d commit 9b0bb2e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1063
-85
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ test-results/
3434
fixtures-test/
3535
fixtures-test-*/
3636
.rslib/
37+
38+
!packages/core/src/coverage

cspell.config.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
'node_modules',
1919
'pnpm-lock.yaml',
2020
'LICENSE.md',
21+
'fixtures',
2122
],
2223
flagWords: banWords,
2324
dictionaries: ['dictionary'],
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"private": true,
3+
"name": "@rstest/tests-coverage",
4+
"main": "index.js",
5+
"type": "module",
6+
"sideEffects": true,
7+
"version": "1.0.0",
8+
"devDependencies": {
9+
"@rstest/core": "workspace:*",
10+
"@rstest/coverage-istanbul": "workspace:*"
11+
}
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { defineConfig } from '@rstest/core';
2+
3+
export default defineConfig({
4+
coverage: {
5+
enabled: true,
6+
provider: 'istanbul',
7+
},
8+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export const removeDuplicates = <T>(arr: T[]): T[] => {
2+
return [...new Set(arr)];
3+
};
4+
5+
export const chunk = <T>(arr: T[], size: number): T[][] => {
6+
if (size <= 0) {
7+
throw new Error('Chunk size must be greater than 0');
8+
}
9+
const result: T[][] = [];
10+
for (let i = 0; i < arr.length; i += size) {
11+
result.push(arr.slice(i, i + size));
12+
}
13+
return result;
14+
};
15+
16+
export const flatten = <T>(arr: (T | T[])[]): T[] => {
17+
return arr.reduce<T[]>((acc, val) => {
18+
return acc.concat(Array.isArray(val) ? flatten(val) : val);
19+
}, []);
20+
};
21+
22+
export const findMax = (arr: number[]): number => {
23+
if (arr.length === 0) {
24+
throw new Error('Array cannot be empty');
25+
}
26+
return Math.max(...arr);
27+
};
28+
29+
export const shuffle = <T>(arr: T[]): T[] => {
30+
const shuffled = [...arr];
31+
for (let i = shuffled.length - 1; i > 0; i--) {
32+
const j = Math.floor(Math.random() * (i + 1));
33+
const temp = shuffled[i]!;
34+
shuffled[i] = shuffled[j]!;
35+
shuffled[j] = temp;
36+
}
37+
return shuffled;
38+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export const formatDate = (date: Date, format = 'YYYY-MM-DD'): string => {
2+
const year = date.getFullYear();
3+
const month = String(date.getMonth() + 1).padStart(2, '0');
4+
const day = String(date.getDate()).padStart(2, '0');
5+
6+
return format
7+
.replace('YYYY', year.toString())
8+
.replace('MM', month)
9+
.replace('DD', day);
10+
};
11+
12+
export const addDays = (date: Date, days: number): Date => {
13+
const result = new Date(date);
14+
result.setDate(result.getDate() + days);
15+
return result;
16+
};
17+
18+
export const getDaysBetween = (date1: Date, date2: Date): number => {
19+
const timeDiff = Math.abs(date2.getTime() - date1.getTime());
20+
return Math.ceil(timeDiff / (1000 * 3600 * 24));
21+
};
22+
23+
export const isWeekend = (date: Date): boolean => {
24+
const day = date.getDay();
25+
return day === 0 || day === 6; // Sunday = 0, Saturday = 6
26+
};
27+
28+
export const getQuarter = (date: Date): number => {
29+
return Math.floor((date.getMonth() + 3) / 3);
30+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const sayHi = () => 'hi';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export const capitalize = (str: string): string => {
2+
if (!str) return str;
3+
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
4+
};
5+
6+
export const reverseString = (str: string): string => {
7+
return str.split('').reverse().join('');
8+
};
9+
10+
export const isPalindrome = (str: string): boolean => {
11+
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, '');
12+
return cleaned === cleaned.split('').reverse().join('');
13+
};
14+
15+
export const countWords = (str: string): number => {
16+
return str
17+
.trim()
18+
.split(/\s+/)
19+
.filter((word) => word.length > 0).length;
20+
};
21+
22+
export const truncate = (str: string, maxLength: number): string => {
23+
if (str.length <= maxLength) return str;
24+
return str.slice(0, maxLength - 3) + '...';
25+
};
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { describe, expect, it } from '@rstest/core';
2+
import {
3+
chunk,
4+
findMax,
5+
flatten,
6+
removeDuplicates,
7+
shuffle,
8+
} from '../src/array';
9+
10+
describe('Array Utils', () => {
11+
describe('removeDuplicates', () => {
12+
it('should remove duplicate numbers', () => {
13+
expect(removeDuplicates([1, 2, 2, 3, 3, 4])).toEqual([1, 2, 3, 4]);
14+
});
15+
16+
it('should remove duplicate strings', () => {
17+
expect(removeDuplicates(['a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']);
18+
});
19+
20+
it('should handle empty array', () => {
21+
expect(removeDuplicates([])).toEqual([]);
22+
});
23+
});
24+
25+
describe('chunk', () => {
26+
it('should chunk array into specified size', () => {
27+
expect(chunk([1, 2, 3, 4, 5, 6], 2)).toEqual([
28+
[1, 2],
29+
[3, 4],
30+
[5, 6],
31+
]);
32+
});
33+
34+
it('should handle remainder elements', () => {
35+
expect(chunk([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
36+
});
37+
38+
it('should throw error for invalid chunk size', () => {
39+
expect(() => chunk([1, 2, 3], 0)).toThrow(
40+
'Chunk size must be greater than 0',
41+
);
42+
});
43+
});
44+
45+
describe('flatten', () => {
46+
it('should flatten nested arrays', () => {
47+
expect(flatten([1, [2, 3], [4, [5, 6]]])).toEqual([1, 2, 3, 4, 5, 6]);
48+
});
49+
50+
it('should handle empty arrays', () => {
51+
expect(flatten([])).toEqual([]);
52+
});
53+
});
54+
55+
describe('findMax', () => {
56+
it('should find maximum number', () => {
57+
expect(findMax([1, 5, 3, 9, 2])).toBe(9);
58+
});
59+
60+
it('should throw error for empty array', () => {
61+
expect(() => findMax([])).toThrow('Array cannot be empty');
62+
});
63+
});
64+
65+
describe('shuffle', () => {
66+
it('should return array with same length', () => {
67+
const original = [1, 2, 3, 4, 5];
68+
const shuffled = shuffle(original);
69+
expect(shuffled).toHaveLength(original.length);
70+
});
71+
72+
it('should contain all original elements', () => {
73+
const original = [1, 2, 3, 4, 5];
74+
const shuffled = shuffle(original);
75+
expect(shuffled.sort()).toEqual(original.sort());
76+
});
77+
});
78+
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { describe, expect, it } from '@rstest/core';
2+
import {
3+
addDays,
4+
formatDate,
5+
getDaysBetween,
6+
getQuarter,
7+
isWeekend,
8+
} from '../src/date';
9+
10+
describe('Date Utils', () => {
11+
describe('formatDate', () => {
12+
it('should format date with default format', () => {
13+
const date = new Date('2023-12-25');
14+
expect(formatDate(date)).toBe('2023-12-25');
15+
});
16+
17+
it('should format date with custom format', () => {
18+
const date = new Date('2023-12-25');
19+
expect(formatDate(date, 'DD/MM/YYYY')).toBe('25/12/2023');
20+
});
21+
});
22+
23+
describe('addDays', () => {
24+
it('should add days to date', () => {
25+
const date = new Date('2023-12-25');
26+
const result = addDays(date, 5);
27+
expect(result.getDate()).toBe(30);
28+
});
29+
30+
it('should subtract days from date', () => {
31+
const date = new Date('2023-12-25');
32+
const result = addDays(date, -5);
33+
expect(result.getDate()).toBe(20);
34+
});
35+
});
36+
37+
describe('getDaysBetween', () => {
38+
it('should calculate days between two dates', () => {
39+
const date1 = new Date('2023-12-25');
40+
const date2 = new Date('2023-12-30');
41+
expect(getDaysBetween(date1, date2)).toBe(5);
42+
});
43+
44+
it('should handle reversed dates', () => {
45+
const date1 = new Date('2023-12-30');
46+
const date2 = new Date('2023-12-25');
47+
expect(getDaysBetween(date1, date2)).toBe(5);
48+
});
49+
});
50+
51+
describe('isWeekend', () => {
52+
it('should return true for Saturday', () => {
53+
const saturday = new Date('2023-12-23'); // Saturday
54+
expect(isWeekend(saturday)).toBe(true);
55+
});
56+
57+
it('should return true for Sunday', () => {
58+
const sunday = new Date('2023-12-24'); // Sunday
59+
expect(isWeekend(sunday)).toBe(true);
60+
});
61+
62+
it('should return false for weekday', () => {
63+
const monday = new Date('2023-12-25'); // Monday
64+
expect(isWeekend(monday)).toBe(false);
65+
});
66+
});
67+
68+
describe('getQuarter', () => {
69+
it('should return correct quarter for different months', () => {
70+
expect(getQuarter(new Date('2023-01-15'))).toBe(1);
71+
expect(getQuarter(new Date('2023-04-15'))).toBe(2);
72+
expect(getQuarter(new Date('2023-07-15'))).toBe(3);
73+
expect(getQuarter(new Date('2023-10-15'))).toBe(4);
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)