Sub topic 8
Testing in Pre-Release and Preview Scratch Orgs
This chapter assumes you know what a scratch org is, have the Salesforce CLI (sf) installed, and have a Dev Hub enabled. It also assumes you have read the earlier chapters on the release timeline and on choosing preview sandboxes.
Why use a scratch org for release testing
A preview sandbox gives you a copy of your real configuration on the upcoming release. A scratch org gives you something different: a disposable org built from source, on whichever release you choose. You can create one in a minute or two, run a test, and delete it. That makes scratch orgs a good fit for checking that your code and metadata work on the new release, without waiting for a sandbox refresh.
Scratch orgs are created from a Dev Hub. Their release version is controlled by the release option in the scratch org definition file.
The release option
The release option accepts two values:
| Value | Result |
|---|---|
| `Current` | The org is created on the release that production is running now. This is the default. |
| `Preview` | The org is created on the upcoming release, when a preview is available. |
Preview is only available during the preview window before the production upgrade. Outside that window, Preview gives you the same release as Current.
{
"orgName": "Release Preview Org",
"edition": "Developer",
"release": "Preview"
}Save this as config/project-scratch-def.json, or keep a second file such as config/preview-scratch-def.json so your normal definition stays on Current.
Create the preview scratch org
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias preview-org \
--duration-days 7 \
--set-defaultThe command uses the definition file, names the org preview-org, and sets it to expire after 7 days. --set-default makes it the default target org for later commands. A short lifetime keeps old preview orgs from piling up against your Dev Hub's active scratch org limit.
Deploy and test your code
Once the org exists, push your source and run your tests:
sf project deploy start --target-org preview-org
sf apex run test --target-org preview-org --test-level RunLocalTests --result-format human --wait 20Scratch orgs start nearly empty, so your tests must create their own data. Tests that depend on real org data pass in a sandbox but may fail here. A failure on the preview release that does not occur on the current release is the signal you are looking for.
Common errors
- No difference from Current. You created the org outside the preview window, so
Previewresolved to the current release. Check the org's release in the org's About information or withsf org display. - Deploy fails on features. The edition or
featureslist in the definition file does not enable something your source needs. Add the feature and recreate the org. - Tests pass here but fail in the sandbox. The scratch org lacks your production data, settings, or integrations. Treat scratch results as a code-level check, not a full regression.
- Hitting the scratch org limit. Delete finished orgs with
sf org delete scratch --target-org preview-org.
Compare against a Current org
To tell a release regression from a plain test failure, run the same tests in a second scratch org created with "release": "Current". If a test fails only on the preview org, the cause is a release change. If it fails in both, the cause is your code or test data.
Exercise
Your Apex tests fail in a scratch org created with "release": "Preview". How do you find out whether the new release caused the failure?
Show answer
Create a second scratch org from the same source with "release": "Current", deploy, and run the same tests. If the tests fail only in the preview org, the release change is the likely cause. If they fail in both, the cause is your code or test data.
Exercise
You create a scratch org with "release": "Preview" in a month when no preview is available. Which release does it run on?
Show answer
The current release, because the Preview value only gives the upcoming release while a preview window is open.