Skip to content

Testing

Darren Cooney edited this page Aug 28, 2023 · 1 revision

When developing a React package for distribution on npm, you must confirm that the package is compatible for use in other applications.

NPM Link creates a global symlink for a dependency. A symlink, short for symbolic link, is a shortcut that points to another directory or file on your system.

NPM Link allows for local development and testing of components without having to continuously rebuild and publish the dependent package.

Alternative Testing

Another method of testing an npm package is to export the package as a Tarball(.tgz) and import it into another project as a dependency using NPM Pack.

This testing method provides the most realistic experience with regards to how a package will be installed and used in a real-world application.

Export Package

Run npm pack from the project root to create a .tgz export.

Note: The name of the exported file will be <your-package-name>-<version>.tgz

Install Package

Copy the generated .tgz file to the root of another project or create a new CRA project for testing purposes.

Open your terminal in the test project and run the following command to install the package as a dependency in package.json.

npm i file:<your-package-name>.tgz

⚠️ Notice the file: in the dependency path. It is critical that you reference the absolute path to the .tgz export.

After the package has been installed you can import the package into the app as you would any other npm package and begin testing.

import Example from 'example-package'
Clone this wiki locally