Skip to content

Commit 8c116d0

Browse files
authored
[Feat] Add tests (#3)
1 parent c54b2cf commit 8c116d0

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import calculateValues from '../calculate-values';
2+
3+
jest.mock('node:fs/promises', () => {
4+
return {
5+
readFile: (fileName: string) => {
6+
const mocks: Record<string, string> = {
7+
'hello.js': 'alert(42);\n\nconst str = "hello";',
8+
'42.ts': 'const str = "hello ts";\nconst n: number = 1;\nconst a: number = n;',
9+
'component.tsx': 'const App = () => <div>Hello 1</div>',
10+
'legacy.jsx': 'const Legacy = () => <span>Hello 2</span>',
11+
};
12+
return Promise.resolve(mocks[fileName]);
13+
},
14+
};
15+
});
16+
17+
describe('Calculate values', () => {
18+
test(`works when ignoreEmptyLines: false`, async () => {
19+
const result = await calculateValues(
20+
['hello.js', 'component.tsx', '42.ts', 'legacy.jsx'],
21+
false,
22+
['.ts', '.tsx'],
23+
);
24+
25+
expect(result).toEqual({
26+
migratedPercentage: 50,
27+
totalFilesAll: 4,
28+
totalLinesAll: 8,
29+
totalFilesTS: 2,
30+
totalLinesTS: 4,
31+
});
32+
});
33+
34+
test(`works when ignoreEmptyLines: true`, async () => {
35+
const result = await calculateValues(['hello.js', '42.ts'], true, ['.ts']);
36+
37+
expect(result).toEqual({
38+
migratedPercentage: 60,
39+
totalFilesAll: 2,
40+
totalLinesAll: 5,
41+
totalFilesTS: 1,
42+
totalLinesTS: 3,
43+
});
44+
});
45+
});

src/__tests__/e2e.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { exec } from 'node:child_process';
2+
import util from 'node:util';
3+
4+
const execAsync = util.promisify(exec);
5+
const testTimeoutMs = 30000;
6+
7+
const stripFormatting = (str: string) => {
8+
return (
9+
str
10+
// Remove bold and reset
11+
.replace(/\x1b\[1m/g, '')
12+
.replace(/\x1b\[22m/g, '')
13+
// Remove underline and reset
14+
.replace(/\x1b\[4m/g, '')
15+
.replace(/\x1b\[24m/g, '')
16+
);
17+
};
18+
19+
describe('E2E Test on src code', () => {
20+
test(
21+
'should work with no config and give correct output',
22+
async () => {
23+
const { stdout: rawStdout, stderr: rawStderr } = await execAsync('pnpm e2e');
24+
25+
const stdout = stripFormatting(rawStdout);
26+
const stderr = stripFormatting(rawStderr);
27+
28+
expect(stderr).toContain('created dist/es, dist/executable.js');
29+
expect(stderr).toContain(`Couldn't find type-coverage.config.json file, using defaults`);
30+
31+
expect(stdout).toContain('Including files from: src/**/*.{ts,tsx,js,jsx}');
32+
expect(stdout).toContain('Excluding files from: **/__tests__/**');
33+
expect(stdout).toContain(
34+
'The state of the TypeScript migration: we are currently at 100.00%',
35+
);
36+
37+
expect(stdout).toContain('Total Typescript files: 4 (.ts,.tsx)');
38+
expect(stdout).toContain('Total files found: 4 (all types)');
39+
},
40+
testTimeoutMs,
41+
);
42+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import normalizeConfig, { defaultSettings } from '../normalize-config';
2+
3+
describe('Handle incorrect values', () => {
4+
[null, undefined, '', 'hello', true, false].forEach(value => {
5+
test(`returns default settings when ${value} used as an input`, () => {
6+
expect(normalizeConfig(value as any)).toEqual(defaultSettings);
7+
});
8+
});
9+
});
10+
11+
describe('Handle correct values', () => {
12+
test(`combines default settings with user override for include`, () => {
13+
expect(normalizeConfig('{ "include": "src/**/*.*" }')).toEqual({
14+
...defaultSettings,
15+
include: 'src/**/*.*',
16+
});
17+
});
18+
test(`combines default settings with user override for exclude`, () => {
19+
expect(normalizeConfig('{ "exclude": ["**/mocks/**", "**/*.cy.{js,jsx,ts,tsx}"] }')).toEqual({
20+
...defaultSettings,
21+
exclude: ['**/mocks/**', '**/*.cy.{js,jsx,ts,tsx}'],
22+
});
23+
});
24+
test(`combines default settings with user override for ignoreEmptyLines`, () => {
25+
expect(normalizeConfig('{ "ignoreEmptyLines": false }')).toEqual({
26+
...defaultSettings,
27+
ignoreEmptyLines: false,
28+
});
29+
});
30+
test(`combines default settings with user override for tsExtensions`, () => {
31+
expect(normalizeConfig('{ "tsExtensions": [".ts", ".tsx", ".mts", ".cts"] }')).toEqual({
32+
...defaultSettings,
33+
tsExtensions: ['.ts', '.tsx', '.mts', '.cts'],
34+
});
35+
});
36+
});

0 commit comments

Comments
 (0)