diff --git a/src/__tests__/calculate-values.spec.ts b/src/__tests__/calculate-values.spec.ts new file mode 100644 index 0000000..0ba6d1c --- /dev/null +++ b/src/__tests__/calculate-values.spec.ts @@ -0,0 +1,45 @@ +import calculateValues from '../calculate-values'; + +jest.mock('node:fs/promises', () => { + return { + readFile: (fileName: string) => { + const mocks: Record = { + '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 = () =>
Hello 1
', + 'legacy.jsx': 'const Legacy = () => Hello 2', + }; + 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, + }); + }); +}); diff --git a/src/__tests__/e2e.spec.ts b/src/__tests__/e2e.spec.ts new file mode 100644 index 0000000..54b2604 --- /dev/null +++ b/src/__tests__/e2e.spec.ts @@ -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, + ); +}); diff --git a/src/__tests__/normalize-config.spec.ts b/src/__tests__/normalize-config.spec.ts new file mode 100644 index 0000000..f39688a --- /dev/null +++ b/src/__tests__/normalize-config.spec.ts @@ -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'], + }); + }); +});