Pārlūkot izejas kodu

refactor(cli): Improve test assertions

Michael Bromley 1 gadu atpakaļ
vecāks
revīzija
321bee7ce3

+ 1 - 1
packages/cli/src/commands/add/entity/add-entity.ts

@@ -35,7 +35,7 @@ export async function addEntity() {
     };
 
     const entitiesDir = path.join(pluginClass.getSourceFile().getDirectory().getPath(), 'entities');
-    const entityTemplatePath = path.join(__dirname, 'scaffold/entity.template.ts');
+    const entityTemplatePath = path.join(__dirname, 'templates/entity.template.ts');
     const entityFile = createSourceFileFromTemplate(project, entityTemplatePath);
     entityFile.move(path.join(entitiesDir, `${context.entity.fileName}.ts`));
     entityFile.getClasses()[0].rename(`${context.entity.className}CustomFields`);

+ 18 - 26
packages/cli/src/commands/add/entity/codemods/add-entity-to-plugin/add-entity-to-plugin.spec.ts

@@ -1,51 +1,43 @@
-import fs from 'fs-extra';
 import path from 'path';
 import { Project } from 'ts-morph';
 import { describe, expect, it } from 'vitest';
 
 import { defaultManipulationSettings } from '../../../../../constants';
 import { createSourceFileFromTemplate, getPluginClasses } from '../../../../../utilities/ast-utils';
+import { expectSourceFileContentToMatch } from '../../../../../utilities/testing-utils';
 
 import { addEntityToPlugin } from './add-entity-to-plugin';
 
 describe('addEntityToPlugin', () => {
-    it('creates entity prop and imports', () => {
+    function testAddEntityToPlugin(options: { fixtureFileName: string; expectedFileName: string }) {
         const project = new Project({
             manipulationSettings: defaultManipulationSettings,
         });
-        project.addSourceFileAtPath(path.join(__dirname, 'fixtures', 'no-entity-prop.fixture.ts'));
+        project.addSourceFileAtPath(path.join(__dirname, 'fixtures', options.fixtureFileName));
         const pluginClasses = getPluginClasses(project);
         expect(pluginClasses.length).toBe(1);
-        const entityTemplatePath = path.join(__dirname, '../../scaffold/entity.template.ts');
+        const entityTemplatePath = path.join(__dirname, '../../templates/entity.template.ts');
         const entityFile = createSourceFileFromTemplate(project, entityTemplatePath);
-        entityFile.move(path.join(__dirname, 'fixtures/entity.ts'));
+        entityFile.move(path.join(__dirname, 'fixtures', 'entity.ts'));
         addEntityToPlugin(pluginClasses[0], entityFile);
 
-        const result = pluginClasses[0].getSourceFile().getText();
-        const expected = fs.readFileSync(
-            path.join(__dirname, 'fixtures', 'no-entity-prop.expected'),
-            'utf-8',
+        expectSourceFileContentToMatch(
+            pluginClasses[0].getSourceFile(),
+            path.join(__dirname, 'fixtures', options.expectedFileName),
         );
-        expect(result).toBe(expected);
+    }
+
+    it('creates entity prop and imports', () => {
+        testAddEntityToPlugin({
+            fixtureFileName: 'no-entity-prop.fixture.ts',
+            expectedFileName: 'no-entity-prop.expected',
+        });
     });
 
     it('adds to existing entity prop and imports', () => {
-        const project = new Project({
-            manipulationSettings: defaultManipulationSettings,
+        testAddEntityToPlugin({
+            fixtureFileName: 'existing-entity-prop.fixture.ts',
+            expectedFileName: 'existing-entity-prop.expected',
         });
-        project.addSourceFileAtPath(path.join(__dirname, 'fixtures', 'existing-entity-prop.fixture.ts'));
-        const pluginClasses = getPluginClasses(project);
-        expect(pluginClasses.length).toBe(1);
-        const entityTemplatePath = path.join(__dirname, '../../scaffold/entity.template.ts');
-        const entityFile = createSourceFileFromTemplate(project, entityTemplatePath);
-        entityFile.move(path.join(__dirname, 'fixtures/entity.ts'));
-        addEntityToPlugin(pluginClasses[0], entityFile);
-
-        const result = pluginClasses[0].getSourceFile().getText();
-        const expected = fs.readFileSync(
-            path.join(__dirname, 'fixtures', 'existing-entity-prop.expected'),
-            'utf-8',
-        );
-        expect(result).toBe(expected);
     });
 });

+ 0 - 0
packages/cli/src/commands/add/entity/scaffold/entity.template.ts → packages/cli/src/commands/add/entity/templates/entity.template.ts


+ 5 - 3
packages/cli/src/commands/add/ui-extensions/codemods/add-ui-extension-static-prop/add-ui-extension-static-prop.spec.ts

@@ -5,6 +5,7 @@ import { describe, expect, it } from 'vitest';
 
 import { defaultManipulationSettings } from '../../../../../constants';
 import { getPluginClasses } from '../../../../../utilities/ast-utils';
+import { expectSourceFileContentToMatch } from '../../../../../utilities/testing-utils';
 
 import { addUiExtensionStaticProp } from './add-ui-extension-static-prop';
 
@@ -18,8 +19,9 @@ describe('addUiExtensionStaticProp', () => {
         expect(pluginClasses.length).toBe(1);
         addUiExtensionStaticProp(pluginClasses[0]);
 
-        const result = pluginClasses[0].getSourceFile().getText();
-        const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'no-ui-prop.expected'), 'utf-8');
-        expect(result).toBe(expected);
+        expectSourceFileContentToMatch(
+            pluginClasses[0].getSourceFile(),
+            path.join(__dirname, 'fixtures', 'no-ui-prop.expected'),
+        );
     });
 });

+ 7 - 7
packages/cli/src/commands/add/ui-extensions/codemods/update-admin-ui-plugin-init/update-admin-ui-plugin-init.spec.ts

@@ -5,6 +5,7 @@ import { describe, expect, it } from 'vitest';
 
 import { defaultManipulationSettings } from '../../../../../constants';
 import { getVendureConfig } from '../../../../../utilities/ast-utils';
+import { expectSourceFileContentToMatch } from '../../../../../utilities/testing-utils';
 
 import { updateAdminUiPluginInit } from './update-admin-ui-plugin-init';
 
@@ -20,9 +21,10 @@ describe('updateAdminUiPluginInit', () => {
             pluginPath: './plugins/test-plugin/test.plugin',
         });
 
-        const result = project.getSourceFiles()[0].getText();
-        const expected = fs.readFileSync(path.join(__dirname, 'fixtures', 'no-app-prop.expected'), 'utf-8');
-        expect(result).toBe(expected);
+        expectSourceFileContentToMatch(
+            project.getSourceFiles()[0],
+            path.join(__dirname, 'fixtures', 'no-app-prop.expected'),
+        );
     });
 
     it('adds to existing ui extensions array', () => {
@@ -36,11 +38,9 @@ describe('updateAdminUiPluginInit', () => {
             pluginPath: './plugins/test-plugin/test.plugin',
         });
 
-        const result = project.getSourceFiles()[0].getText();
-        const expected = fs.readFileSync(
+        expectSourceFileContentToMatch(
+            project.getSourceFiles()[0],
             path.join(__dirname, 'fixtures', 'existing-app-prop.expected'),
-            'utf-8',
         );
-        expect(result).toBe(expected);
     });
 });

+ 1 - 2
packages/cli/src/constants.ts

@@ -1,7 +1,6 @@
-import { ManipulationSettings, NewLineKind, QuoteKind } from 'ts-morph';
+import { ManipulationSettings, QuoteKind } from 'ts-morph';
 
 export const defaultManipulationSettings: Partial<ManipulationSettings> = {
     quoteKind: QuoteKind.Single,
     useTrailingCommas: true,
-    newLineKind: NewLineKind.LineFeed,
 };

+ 13 - 0
packages/cli/src/utilities/testing-utils.ts

@@ -0,0 +1,13 @@
+import fs from 'fs-extra';
+import { SourceFile } from 'ts-morph';
+import { expect } from 'vitest';
+
+export function expectSourceFileContentToMatch(sourceFile: SourceFile, expectedFilePath: string) {
+    const result = sourceFile.getFullText();
+    const expected = fs.readFileSync(expectedFilePath, 'utf-8');
+    expect(normalizeLineFeeds(result)).toBe(normalizeLineFeeds(expected));
+}
+
+function normalizeLineFeeds(text: string): string {
+    return text.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
+}