Skip to content

How to test your schematic library?

Introduction

After creating your first schematics, you’ll want to test them to ensure they work as expected. There are three main ways to do this:

Watching mode

In your package.json, you’ll notice not just a build script but also a build:watch script. This command compiles your project automatically every time files in the src directory change.

To activate watching mode, execute:

Terminal window
npm run build:watch

Then, you can test your schematics using the CLI with the command: builder exec [collection-location] [schematic-name]. For example, to test it at the root of your project:

Terminal window
builder exec ./dist/collection.json prettier

If you want to test your schematics as if they were a package, you can use npm link. This approach allows you to simulate the installation and use of your package locally without publishing it. Execute the following:

Terminal window
npm run build && cd dist && npm link ./dist

Verdaccio

Verdaccio offers a more comprehensive testing setup with a few extra steps:

  1. Install Verdaccio globally:
Terminal window
npm i -g verdaccio
  1. Start the Verdaccio server:
Terminal window
verdaccio
  1. Publish your package locally:
Terminal window
npm run build && cd dist && npm publish --registry http://localhost:4873

Here’s a comparison to help you decide between Verdaccio and npm link for testing your schematics:

FeatureVerdaccionpm link
Primary UseActs as a private npm proxy registry for package managementCreates a symbolic link for local package development
Use CaseIdeal for managing private packages within teams or organizationsUseful for testing local changes in packages without needing to publish them
Advantages- Private hosting of packages
- Caching of npm packages
- Version control
- Integration with npm registry
- Quick feedback on changes
- Simplifies the development process
- Avoids premature package publication
Ideal ForTeams and organizations requiring shared access to private packagesDevelopers testing local package dependencies
Installation ProcessInvolves setting up a Verdaccio serverA straightforward command line operation
SecurityEnhances package management securityPrimarily for local development, minimal security impact
Network DependencyReduces reliance on the public npm registry by caching packagesNo network dependency, as it links local packages directly

This documentation aims to make the process of testing your schematic library as straightforward as possible, providing you with multiple approaches to suit your development workflow.