Sub topic 8
Testing a Version in Scratch Orgs and Checking Code Coverage
This chapter assumes you can already build a package version with sf package version create and that you know scratch orgs, Apex tests, and the sf CLI. The aim is to check a beta version before you promote it.
Why test the built version
Tests that pass in your source scratch org do not prove the package works. A package version is a compiled snapshot of your metadata. Installing it shows problems that source deploys hide, such as missing dependencies, namespace references, and metadata that did not make it into the package directory.
A beta version is the right artifact to test. It can be installed in scratch orgs and sandboxes, but not in production.
Check code coverage
A version must have at least 75% Apex code coverage before you can promote it. Coverage is only calculated if you pass --code-coverage when you create the version. A version built with --skip-validation has no coverage data and cannot be promoted.
sf package version report --package [email protected] --verbose --target-dev-hub devhubWith --verbose, the report includes the coverage percentage and whether the version has passed the coverage check. If coverage is too low, add tests and build a new version. You cannot raise coverage on an existing one.
Install into a scratch org
Create a clean scratch org that does not contain your source. Then install the beta version into it.
sf org create scratch --definition-file config/project-scratch-def.json --alias pkgtest --duration-days 7 --target-dev-hub devhub
sf package install --package [email protected] --target-org pkgtest --wait 10 --no-promptThe definition file should enable the same features and editions your subscribers use. Otherwise you may miss failures that appear only in their orgs.
Run tests against the installed package
Run your Apex tests from the scratch org to see how the package behaves when installed.
sf apex run test --target-org pkgtest --test-level RunLocalTests --code-coverage --result-format human --wait 10Keep in mind that RunLocalTests skips tests that belong to installed managed packages. If your tests live only in the package, run them by class name or use a separate test project. Also do manual checks that tests cannot cover, such as page layouts, permission sets, and the install experience.
Common errors
- No coverage on the version. You omitted
--code-coverageor used--skip-validation. Rebuild the version. - Coverage below 75%. Promotion is blocked. Add tests and create a new version.
- Install fails in the scratch org. A dependency is probably missing. Install the dependent package first, or add it to
dependencies. - Behavior differs from source. Metadata may sit outside the package directory. Check
sfdx-project.json.
Exercise
You built version [email protected] with --skip-validation and the build was fast. Why can't you promote it, and what do you do?
Show answer
Skipping validation means Apex tests did not run and no code coverage was calculated, so the version cannot be promoted. Create a new version with --code-coverage, confirm coverage is at least 75%, test it in a scratch org, and then promote that version.