Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
30 changes: 16 additions & 14 deletions Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test_configuration:
- SwiftUI
testable_imports:
- Prefire
draw_hierarchy_in_key_window_default_enabled: true

playbook_configuration:
preview_default_enabled: true
Expand All @@ -39,20 +40,21 @@ playbook_configuration:

### 🧾 Configuration Keys

| Key | Description |
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `target` | Target name used for snapshot generation. Default: *FirstTarget* |
| `test_target_path` | Path to unit test directory. Snapshots will be written to its `__Snapshots__` folder. Default: target name folder |
| `test_file_path` | Output file path for generated tests. Default: DerivedData or resolved via plugin |
| `template_file_path` | Custom template path relative to target. Optional. Defaults:β€£ *PreviewTests.stencil* for test pluginβ€£ *PreviewModels.stencil* for playbook plugin|
| `simulator_device` | Device identifier used to run tests (e.g. `iPhone15,2`). Optional |
| `required_os` | Minimal iOS version required for preview rendering. Optional |
| `snapshot_devices` | List of logical snapshot "targets" (used as trait collections). Each will snapshot separately. Optional |
| `preview_default_enabled` | Should all detected previews be included by default? Set `false` if you want to require `.prefireEnabled()` manually. Default: `true` |
| `use_grouped_snapshots` | Generate a single test file with all previews (`true`) or separate test files per source file (`false`). When `false`, use `{PREVIEW_FILE_NAME}` placeholder in `test_file_path`. Default: `true` |
| `sources` | List of Swift files or folders to scan for previews. Defaults to inferred from the target |
| `imports` | Extra imports added to the generated test or playbook file |
| `testable_imports` | Extra `@testable` imports added to allow test visibility |
| Key | Description |
| ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `target` | Target name used for snapshot generation. Default: _FirstTarget_ |
| `test_target_path` | Path to unit test directory. Snapshots will be written to its `__Snapshots__` folder. Default: target name folder |
| `test_file_path` | Output file path for generated tests. Default: DerivedData or resolved via plugin |
| `template_file_path` | Custom template path relative to target. Optional. Defaults:β€£ _PreviewTests.stencil_ for test pluginβ€£ _PreviewModels.stencil_ for playbook plugin |
| `simulator_device` | Device identifier used to run tests (e.g. `iPhone15,2`). Optional |
| `required_os` | Minimal iOS version required for preview rendering. Optional |
| `snapshot_devices` | List of logical snapshot "targets" (used as trait collections). Each will snapshot separately. Optional |
| `preview_default_enabled` | Should all detected previews be included by default? Set `false` if you want to require `.prefireEnabled()` manually. Default: `true` |
| `use_grouped_snapshots` | Generate a single test file with all previews (`true`) or separate test files per source file (`false`). When `false`, use `{PREVIEW_FILE_NAME}` placeholder in `test_file_path`. Default: `true` |
| `sources` | List of Swift files or folders to scan for previews. Defaults to inferred from the target |
| `imports` | Extra imports added to the generated test or playbook file |
| `testable_imports` | Extra `@testable` imports added to allow test visibility |
| `draw_hierarchy_in_key_window_default_enabled` | Specifies whether to use the simulator's key window to snapshot the UI, rendering `UIAppearance` and `UIVisualEffect`. This option requires a host application for testing and does not work with framework test targets. Default: `true` |
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't backward compatibility break off here?
It is important to understand what the default parameter is in SwiftSnapshotTesting.


---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ import SnapshotTesting
as: .wait(
for: preferences.delay,
on: .image(
drawHierarchyInKeyWindow: {{ argument.drawHierarchyInKeyWindowDefaultEnabled }},
precision: preferences.precision,
perceptualPrecision: preferences.perceptualPrecision,
layout: prefireSnapshot.isScreen ? .device(config: prefireSnapshot.device.imageConfig) : .sizeThatFits,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct GeneratedTestsOptions {
var imports: [String]?
var testableImports: [String]?
var useGroupedSnapshots: Bool
var drawHierarchyInKeyWindowDefaultEnabled: Bool

init(
target: String?,
Expand Down Expand Up @@ -57,6 +58,7 @@ struct GeneratedTestsOptions {
snapshotDevices = config?.tests.snapshotDevices
imports = config?.tests.imports
testableImports = config?.tests.testableImports
drawHierarchyInKeyWindowDefaultEnabled = config?.tests.drawHierarchyInKeyWindowDefaultEnabled ?? false
}
}

Expand All @@ -77,6 +79,7 @@ enum GenerateTestsCommand {
static let testableImports = "testableImports"
static let previewsMacros = "previewsMacros"
static let previewsMacrosDict = "previewsMacrosDict"
static let drawHierarchyInKeyWindowDefaultEnabled = "drawHierarchyInKeyWindowDefaultEnabled"
}

static func run(_ options: GeneratedTestsOptions) async throws {
Expand Down Expand Up @@ -104,6 +107,7 @@ enum GenerateTestsCommand {
➜ Generated test path: \(options.output)
➜ Snapshot resources path: \(snapshotOutput ?? "nil")
➜ Preview default enabled: \(options.prefireEnabledMarker)
➜ drawHierarchyInKeyWindow default enabled: \(options.drawHierarchyInKeyWindowDefaultEnabled)
"""
)

Expand All @@ -115,6 +119,7 @@ enum GenerateTestsCommand {
Keys.testableImports: options.testableImports as? NSArray,
Keys.mainTarget: options.target as? NSString,
Keys.file: snapshotOutput?.string as? NSString,
Keys.drawHierarchyInKeyWindowDefaultEnabled: String(options.drawHierarchyInKeyWindowDefaultEnabled) as NSString,
].filter({ $0.value != nil }) as? [String: NSObject] ?? [:]
}
}
2 changes: 2 additions & 0 deletions PrefireExecutable/Sources/prefire/Config/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct TestsConfig {
var imports: [String]?
var testableImports: [String]?
var useGroupedSnapshots: Bool?
var drawHierarchyInKeyWindowDefaultEnabled: Bool?

enum CodingKeys: String, CodingKey {
case target = "target"
Expand All @@ -38,6 +39,7 @@ struct TestsConfig {
case imports = "imports"
case testableImports = "testable_imports"
case useGroupedSnapshots = "use_grouped_snapshots"
case drawHierarchyInKeyWindowDefaultEnabled = "draw_hierarchy_in_key_window_default_enabled"
}
}

Expand Down
2 changes: 2 additions & 0 deletions PrefireExecutable/Sources/prefire/Config/ConfigDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ final class ConfigDecoder {
config.tests.testTargetPath = getValue(from: components.last, env: env)
case .useGroupedSnapshots:
config.tests.useGroupedSnapshots = getValue(from: components.last, env: env) == "true"
case .drawHierarchyInKeyWindowDefaultEnabled:
config.tests.drawHierarchyInKeyWindowDefaultEnabled = getValue(from: components.last, env: env) == "true"
}
}

Expand Down
2 changes: 2 additions & 0 deletions PrefireExecutable/Tests/PrefireTests/ConfigDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ConfigDecoderTests: XCTestCase {
- SwiftUI
- testable_imports:
- Prefire
- draw_hierarchy_in_key_window_default_enabled: true
playbook_configuration:
- template_file_path: CustomModels.stencil
- imports:
Expand All @@ -45,6 +46,7 @@ class ConfigDecoderTests: XCTestCase {
XCTAssertEqual(config.tests.previewDefaultEnabled, true)
XCTAssertEqual(config.tests.imports, ["UIKit", "SwiftUI"])
XCTAssertEqual(config.tests.testableImports, ["Prefire"])
XCTAssertEqual(config.tests.drawHierarchyInKeyWindowDefaultEnabled, true)
XCTAssertEqual(config.playbook.imports, ["UIKit", "Foundation"])
XCTAssertEqual(config.playbook.testableImports, ["SwiftUI"])
XCTAssertEqual(config.playbook.template, "CustomModels.stencil")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class GenerateTestsCommandTests: XCTestCase {
let expectedArguments = [
"mainTarget": options.target! as NSString,
"file": options.testTargetPath.flatMap({ $0 + "PreviewTests.generated.swift"})!.string as NSString,
"drawHierarchyInKeyWindowDefaultEnabled": "false" as NSString,
] as [String: NSObject]

let arguments = await GenerateTestsCommand.makeArguments(for: options)
Expand All @@ -42,6 +43,21 @@ class GenerateTestsCommandTests: XCTestCase {
"mainTarget": "\(options.target ?? "")" as NSString,
"file": options.testTargetPath.flatMap({ $0 + "PreviewTests.generated.swift"})!.string as NSString,
"snapshotDevices": "iPhone 15|iPad" as NSString,
"drawHierarchyInKeyWindowDefaultEnabled": "false" as NSString,
] as [String: NSObject]

let arguments = await GenerateTestsCommand.makeArguments(for: options)

XCTAssertEqual(YAMLParser().string(from: arguments), YAMLParser().string(from: expectedArguments))
}

func test_makeArguments_drawHierarchyInKeyWindowDefaultEnabled() async {
options.drawHierarchyInKeyWindowDefaultEnabled = true

let expectedArguments = [
"mainTarget": "\(options.target ?? "")" as NSString,
"file": options.testTargetPath.flatMap({ $0 + "PreviewTests.generated.swift"})!.string as NSString,
"drawHierarchyInKeyWindowDefaultEnabled": "true" as NSString
] as [String: NSObject]

let arguments = await GenerateTestsCommand.makeArguments(for: options)
Expand Down