Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/__tests__/calculate-values.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import calculateValues from '../calculate-values';

jest.mock('node:fs/promises', () => {
return {
readFile: (fileName: string) => {
const mocks: Record<string, string> = {
'hello.js': 'alert(42);\n\nconst str = "hello";',
'42.ts': 'const str = "hello ts";\nconst n: number = 1;\nconst a: number = n;',
'component.tsx': 'const App = () => <div>Hello 1</div>',
'legacy.jsx': 'const Legacy = () => <span>Hello 2</span>',
};
return Promise.resolve(mocks[fileName]);
},
};
});

describe('Calculate values', () => {
test(`works when ignoreEmptyLines: false`, async () => {
const result = await calculateValues(
['hello.js', 'component.tsx', '42.ts', 'legacy.jsx'],
false,
['.ts', '.tsx'],
);

expect(result).toEqual({
migratedPercentage: 50,
totalFilesAll: 4,
totalLinesAll: 8,
totalFilesTS: 2,
totalLinesTS: 4,
});
});

test(`works when ignoreEmptyLines: true`, async () => {
const result = await calculateValues(['hello.js', '42.ts'], true, ['.ts']);

expect(result).toEqual({
migratedPercentage: 60,
totalFilesAll: 2,
totalLinesAll: 5,
totalFilesTS: 1,
totalLinesTS: 3,
});
});
});
42 changes: 42 additions & 0 deletions src/__tests__/e2e.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { exec } from 'node:child_process';
import util from 'node:util';

const execAsync = util.promisify(exec);
const testTimeoutMs = 30000;

const stripFormatting = (str: string) => {
return (
str
// Remove bold and reset
.replace(/\x1b\[1m/g, '')
.replace(/\x1b\[22m/g, '')
// Remove underline and reset
.replace(/\x1b\[4m/g, '')
.replace(/\x1b\[24m/g, '')
);
};

describe('E2E Test on src code', () => {
test(
'should work with no config and give correct output',
async () => {
const { stdout: rawStdout, stderr: rawStderr } = await execAsync('pnpm e2e');

const stdout = stripFormatting(rawStdout);
const stderr = stripFormatting(rawStderr);

expect(stderr).toContain('created dist/es, dist/executable.js');
expect(stderr).toContain(`Couldn't find type-coverage.config.json file, using defaults`);

expect(stdout).toContain('Including files from: src/**/*.{ts,tsx,js,jsx}');
expect(stdout).toContain('Excluding files from: **/__tests__/**');
expect(stdout).toContain(
'The state of the TypeScript migration: we are currently at 100.00%',
);

expect(stdout).toContain('Total Typescript files: 4 (.ts,.tsx)');
expect(stdout).toContain('Total files found: 4 (all types)');
},
testTimeoutMs,
);
});
36 changes: 36 additions & 0 deletions src/__tests__/normalize-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import normalizeConfig, { defaultSettings } from '../normalize-config';

describe('Handle incorrect values', () => {
[null, undefined, '', 'hello', true, false].forEach(value => {
test(`returns default settings when ${value} used as an input`, () => {
expect(normalizeConfig(value as any)).toEqual(defaultSettings);
});
});
});

describe('Handle correct values', () => {
test(`combines default settings with user override for include`, () => {
expect(normalizeConfig('{ "include": "src/**/*.*" }')).toEqual({
...defaultSettings,
include: 'src/**/*.*',
});
});
test(`combines default settings with user override for exclude`, () => {
expect(normalizeConfig('{ "exclude": ["**/mocks/**", "**/*.cy.{js,jsx,ts,tsx}"] }')).toEqual({
...defaultSettings,
exclude: ['**/mocks/**', '**/*.cy.{js,jsx,ts,tsx}'],
});
});
test(`combines default settings with user override for ignoreEmptyLines`, () => {
expect(normalizeConfig('{ "ignoreEmptyLines": false }')).toEqual({
...defaultSettings,
ignoreEmptyLines: false,
});
});
test(`combines default settings with user override for tsExtensions`, () => {
expect(normalizeConfig('{ "tsExtensions": [".ts", ".tsx", ".mts", ".cts"] }')).toEqual({
...defaultSettings,
tsExtensions: ['.ts', '.tsx', '.mts', '.cts'],
});
});
});