SKILL.blade.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ---
  2. name: pest-testing
  3. description: "Use this skill for Pest PHP testing in Laravel projects only. Trigger whenever any test is being written, edited, fixed, or refactored — including fixing tests that broke after a code change, adding assertions, converting PHPUnit to Pest, adding datasets, and TDD workflows. Always activate when the user asks how to write something in Pest, mentions test files or directories (tests/Feature, tests/Unit, tests/Browser), or needs browser testing, smoke testing multiple pages for JS errors, or architecture tests. Covers: test()/it()/expect() syntax, datasets, mocking, browser testing (visit/click/fill), smoke testing, arch(), Livewire component tests, RefreshDatabase, and all Pest 4 features. Do not use for factories, seeders, migrations, controllers, models, or non-test PHP code."
  4. license: MIT
  5. metadata:
  6. author: laravel
  7. ---
  8. @php
  9. /** @var \Laravel\Boost\Install\GuidelineAssist $assist */
  10. @endphp
  11. # Pest Testing 4
  12. ## Documentation
  13. Use `search-docs` for detailed Pest 4 patterns and documentation.
  14. ## Basic Usage
  15. ### Creating Tests
  16. All tests must be written using Pest. Use `{{ $assist->artisanCommand('make:test --pest {name}') }}`.
  17. The `{name}` argument should include only the path and test name, but should not include the test suite.
  18. - Incorrect: `{{ $assist->artisanCommand('make:test --pest Feature/SomeFeatureTest') }}` will generate `tests/Feature/Feature/SomeFeatureTest.php`
  19. - Correct: `{{ $assist->artisanCommand('make:test --pest SomeControllerTest') }}` will generate `tests/Feature/SomeControllerTest.php`
  20. - Incorrect: `{{ $assist->artisanCommand('make:test --pest --unit Unit/SomeServiceTest') }}` will generate `tests/Unit/Unit/SomeServiceTest.php`
  21. - Correct: `{{ $assist->artisanCommand('make:test --pest --unit SomeServiceTest') }}` will generate `tests/Unit/SomeServiceTest.php`
  22. ### Test Organization
  23. - Unit/Feature tests: `tests/Feature` and `tests/Unit` directories.
  24. - Browser tests: `tests/Browser/` directory.
  25. - Do NOT remove tests without approval - these are core application code.
  26. ### Basic Test Structure
  27. Pest supports both `test()` and `it()` functions. Before writing new tests, check existing test files in the same directory to match the project's convention. Use `test()` if existing tests use `test()`, or `it()` if they use `it()`.
  28. @boostsnippet("Basic Pest Test Example", "php")
  29. it('is true', function () {
  30. expect(true)->toBeTrue();
  31. });
  32. @endboostsnippet
  33. ### Running Tests
  34. - Run minimal tests with filter before finalizing: `{{ $assist->artisanCommand('test --compact --filter=testName') }}`.
  35. - Run all tests: `{{ $assist->artisanCommand('test --compact') }}`.
  36. - Run file: `{{ $assist->artisanCommand('test --compact tests/Feature/ExampleTest.php') }}`.
  37. ## Assertions
  38. Use specific assertions (`assertSuccessful()`, `assertNotFound()`) instead of `assertStatus()`:
  39. @boostsnippet("Pest Response Assertion", "php")
  40. it('returns all', function () {
  41. $this->postJson('/api/docs', [])->assertSuccessful();
  42. });
  43. @endboostsnippet
  44. | Use | Instead of |
  45. |-----|------------|
  46. | `assertSuccessful()` | `assertStatus(200)` |
  47. | `assertNotFound()` | `assertStatus(404)` |
  48. | `assertForbidden()` | `assertStatus(403)` |
  49. ## Mocking
  50. Import mock function before use: `use function Pest\Laravel\mock;`
  51. ## Datasets
  52. Use datasets for repetitive tests (validation rules, etc.):
  53. @boostsnippet("Pest Dataset Example", "php")
  54. it('has emails', function (string $email) {
  55. expect($email)->not->toBeEmpty();
  56. })->with([
  57. 'james' => 'james@laravel.com',
  58. 'taylor' => 'taylor@laravel.com',
  59. ]);
  60. @endboostsnippet
  61. ## Pest 4 Features
  62. | Feature | Purpose |
  63. |---------|---------|
  64. | Browser Testing | Full integration tests in real browsers |
  65. | Smoke Testing | Validate multiple pages quickly |
  66. | Visual Regression | Compare screenshots for visual changes |
  67. | Test Sharding | Parallel CI runs |
  68. | Architecture Testing | Enforce code conventions |
  69. ### Browser Test Example
  70. Browser tests run in real browsers for full integration testing:
  71. - Browser tests live in `tests/Browser/`.
  72. - Use Laravel features like `Event::fake()`, `assertAuthenticated()`, and model factories.
  73. - Use `RefreshDatabase` for clean state per test.
  74. - Interact with page: click, type, scroll, select, submit, drag-and-drop, touch gestures.
  75. - Test on multiple browsers (Chrome, Firefox, Safari) if requested.
  76. - Test on different devices/viewports (iPhone 14 Pro, tablets) if requested.
  77. - Switch color schemes (light/dark mode) when appropriate.
  78. - Take screenshots or pause tests for debugging.
  79. @boostsnippet("Pest Browser Test Example", "php")
  80. it('may reset the password', function () {
  81. Notification::fake();
  82. $this->actingAs(User::factory()->create());
  83. $page = visit('/sign-in');
  84. $page->assertSee('Sign In')
  85. ->assertNoJavaScriptErrors()
  86. ->click('Forgot Password?')
  87. ->fill('email', 'nuno@laravel.com')
  88. ->click('Send Reset Link')
  89. ->assertSee('We have emailed your password reset link!');
  90. Notification::assertSent(ResetPassword::class);
  91. });
  92. @endboostsnippet
  93. ### Smoke Testing
  94. Quickly validate multiple pages have no JavaScript errors:
  95. @boostsnippet("Pest Smoke Testing Example", "php")
  96. $pages = visit(['/', '/about', '/contact']);
  97. $pages->assertNoJavaScriptErrors()->assertNoConsoleLogs();
  98. @endboostsnippet
  99. ### Visual Regression Testing
  100. Capture and compare screenshots to detect visual changes.
  101. ### Test Sharding
  102. Split tests across parallel processes for faster CI runs.
  103. ### Architecture Testing
  104. Pest 4 includes architecture testing (from Pest 3):
  105. @boostsnippet("Architecture Test Example", "php")
  106. arch('controllers')
  107. ->expect('App\Http\Controllers')
  108. ->toExtendNothing()
  109. ->toHaveSuffix('Controller');
  110. @endboostsnippet
  111. ## Common Pitfalls
  112. - Not importing `use function Pest\Laravel\mock;` before using mock
  113. - Using `assertStatus(200)` instead of `assertSuccessful()`
  114. - Forgetting datasets for repetitive validation tests
  115. - Deleting tests without approval
  116. - Forgetting `assertNoJavaScriptErrors()` in browser tests
  117. - Prefixing `Feature/` or `Unit/` in `{name}` when using `make:test`