From 92221a64a993faf8d35269f4dc1735bc91b4d276 Mon Sep 17 00:00:00 2001 From: Joris Date: Wed, 8 May 2024 15:15:14 +0200 Subject: [PATCH] chore: add test for initial config generation --- src/commands/init.ts | 13 +++++++++---- tests/init-config.test.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 tests/init-config.test.ts diff --git a/src/commands/init.ts b/src/commands/init.ts index d8e615f..dbb2633 100644 --- a/src/commands/init.ts +++ b/src/commands/init.ts @@ -7,15 +7,20 @@ import { defineCommand } from 'citty' import { parseTscOutput } from '../index.js' import { writeTypestepConfig } from '../utils.js' import { CONFIG_FILE_NAME } from '../constants.js' +import type { TypestepConfig } from '../types.js' -async function init(tscOutputFile: string) { +export async function generateInitialConfig(tscOutputFile: string): Promise { const tscOutput = await readFile(tscOutputFile, 'utf8') const parsedTscOutput = parseTscOutput(tscOutput) const ignoredFiles = [...new Set(parsedTscOutput.map(({ path }) => path))] - const configFileContent = writeTypestepConfig({ ignoredFiles }) - return writeFile(CONFIG_FILE_NAME, configFileContent) + return { ignoredFiles } +} + +async function initConfig(tscOutputFile: string) { + const config = await generateInitialConfig(tscOutputFile) + return writeFile(CONFIG_FILE_NAME, writeTypestepConfig(config)) } export default defineCommand({ @@ -37,6 +42,6 @@ export default defineCommand({ if (existsSync(CONFIG_FILE_NAME)) throw new Error('Typestep config file already exists') - init(resolve(process.cwd(), args.tsc_output_file)) + initConfig(resolve(process.cwd(), args.tsc_output_file)) }, }) diff --git a/tests/init-config.test.ts b/tests/init-config.test.ts new file mode 100644 index 0000000..4e2bac9 --- /dev/null +++ b/tests/init-config.test.ts @@ -0,0 +1,31 @@ +import { resolve } from 'node:path' +import { expect, test } from 'vitest' +import { generateInitialConfig } from '../src/commands/init.js' + +const tscOutputPath = resolve(__dirname, 'fixtures', 'tsc-output.log') + +test('init config with ignoredFiles', async () => { + const config = await generateInitialConfig(tscOutputPath) + + expect(config).toStrictEqual({ + ignoredFiles: [ + 'node_modules/.pnpm/@splidejs+vue-splide@0.6.12/node_modules/@splidejs/vue-splide/src/js/plugin/plugin.ts', + 'src/App.vue', + 'src/components/__tests__/SortingButton.spec.ts', + 'src/components/__tests__/MyForm.spec.ts', + 'src/components/__tests__/MyModal.spec.ts', + 'src/components/MyActionsGroup.vue', + 'src/components/Comp1.vue', + 'src/components/Comp2.vue', + 'src/components/MyNav.vue', + 'src/components/MyDocumentUploader.vue', + 'src/components/MyForm2.vue', + 'src/components/MyModal2.vue', + 'src/components/Comp3.vue', + 'src/components/Comp4.vue', + 'src/components/Comp5.vue', + 'src/components/Comp6.vue', + 'src/components/Comp7.vue', + ], + }) +})