or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-4/

Custom Nx Generator with Tests

Create a custom Nx generator that adds a new library to a workspace and write comprehensive tests for it using Nx testing utilities.

Requirements

Generator Implementation

Implement a generator named library that:

  • Accepts a name option (string, required) for the library name
  • Accepts a directory option (string, optional) for the library location
  • Creates a new library project in the workspace at the specified or default location
  • Adds a basic TypeScript file to the library with an exported function
  • Updates the workspace configuration to include the new library project

Test Suite

Write tests for the generator that:

  • Verify the generator creates the expected files in the virtual tree
  • Verify the library name is correctly used in generated files
  • Verify the workspace configuration is updated with the new project
  • Test both with and without the optional directory parameter
  • Use a mock project graph to verify dependencies if any are created

Implementation Details

The generator should create:

  • A src/index.ts file with a basic exported function
  • A project.json file with appropriate configuration
  • Update the workspace tree accordingly

Test Cases

  • It creates a library with the specified name in the default location @test
  • It creates a library in a custom directory when directory option is provided @test
  • It generates the expected index.ts file with correct exports @test
  • It updates the project configuration correctly @test

Implementation

@generates

API

import { Tree } from '@nx/devkit';

export interface LibraryGeneratorSchema {
  name: string;
  directory?: string;
}

export async function libraryGenerator(
  tree: Tree,
  options: LibraryGeneratorSchema
): Promise<void>;

Dependencies { .dependencies }

@nx/devkit { .dependency }

Provides tree manipulation, configuration utilities, and testing utilities for Nx plugin development.