Angular Modernizer
    Preparing search index...

    Test helper that creates a temporary real directory and a ts-morph project on it, as an alternative to the in-memory file system when a test must reflect real file system behavior (for example schematics tests). RealFsTestHelper.cleanup removes the directory.

    describe('NgModuleManager (Real FS)', () => {
    let testFs: RealFsTestHelper;

    beforeEach(async () => {
    testFs = await RealFsTestHelper.create();
    });

    afterEach(async () => {
    await testFs.cleanup();
    });

    it('extracts imports', async () => {
    const file = await testFs.createFile('/module.ts', `
    @NgModule({ imports: [CommonModule, FormsModule] })
    `);

    const imports = manager.getNgModuleImports(file);
    expect(imports).toEqual(['CommonModule', 'FormsModule']);
    });
    });
    Index

    Methods

    • Clean up temp directory and all created files.

      ⚠️ IMPORTANT: Always call this in afterEach to prevent temp file accumulation.

      Returns Promise<void>

      afterEach(async () => {
      await testFs.cleanup();
      });
    • Create a directory in the temp directory.

      Parameters

      • relativePath: string

        Path relative to temp directory

      Returns Promise<void>

      await testFs.createDirectory('/src/app/modules');
      
    • Create a new file in the temp directory.

      Parameters

      • relativePath: string

        Path relative to temp directory (e.g., '/module.ts' or 'src/app/module.ts')

      • content: string

        File content

      Returns Promise<SourceFile>

      SourceFile instance from ts-morph

      const file = await testFs.createFile('/test.module.ts', `
      @NgModule({ imports: [CommonModule] })
      export class TestModule {}
      `);
    • Delete a file from the temp directory.

      Parameters

      • relativePath: string

        Path relative to temp directory

      Returns Promise<void>

      await testFs.deleteFile('/old-module.ts');
      
    • Check if a file exists in the temp directory.

      Parameters

      • relativePath: string

        Path relative to temp directory

      Returns Promise<boolean>

      True if file exists

      const exists = await testFs.fileExists('/module.ts');
      expect(exists).toBe(true);
    • Get all created files.

      Returns Set<string>

      Set of absolute file paths

    • Get the absolute path for a relative file path.

      Parameters

      • relativePath: string

        Path relative to temp directory

      Returns string

      Absolute path

      const fullPath = testFs.getFullPath('/module.ts');
      
    • Get the ts-morph Project instance.

      Returns Project

      ts-morph Project

      const project = testFs.getProject();
      const sourceFiles = project.getSourceFiles();
    • Get statistics about the test helper (for debugging/validation).

      Returns { filesCreated: number; sourceFilesInProject: number; tempDir: string }

      Statistics object

      • filesCreated: number

        Number of files created through this helper.

      • sourceFilesInProject: number

        Number of source files in the ts-morph project.

      • tempDir: string

        Path of the temporary directory.

    • Get the absolute path to the temp directory.

      Returns string

      Absolute path to temp directory

      const tempDir = testFs.getTempDir();
      console.info(`Test files in: ${tempDir}`);
    • List all files in a directory.

      Parameters

      • relativePath: string = '/'

        Path relative to temp directory

      Returns Promise<string[]>

      Array of file names

      const files = await testFs.listFiles('/src');
      expect(files).toContain('app.module.ts');
    • Read a file from the temp directory.

      Parameters

      • relativePath: string

        Path relative to temp directory

      Returns Promise<string>

      File content as string

      const content = await testFs.readFile('/module.ts');
      expect(content).toContain('CommonModule');
    • Save all modified source files.

      Returns Promise<void>

      // Modify files
      sourceFile.replaceText([0, 10], 'new content');

      // Save all changes
      await testFs.saveAll();
    • Factory method to create and initialize a new RealFsTestHelper.

      Returns Promise<RealFsTestHelper>

      Initialized test helper instance

      const testFs = await RealFsTestHelper.create();