Skip to content

Commit 8b735cb

Browse files
authored
Merge pull request #7 from worksome/feature/exclude-nodes
feat: add support for excluding nodes
2 parents 315ab09 + c2a2570 commit 8b735cb

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ php artisan test -p --gql-coverage
2828
```
2929

3030
### Setting coverage limits
31+
3132
By adding the argument `--gql-min=<percentage>`, we can limit to have a min coverage of x.
3233

3334
```bash
@@ -44,13 +45,32 @@ php artisan test --gql-coverage --gql-untested-count=25
4445
```
4546

4647
### Changing default schema fetching command
48+
4749
By default, it will fetch the schema using `php artisan lighthouse:print-schema`, however if you have a
4850
custom command for fetching the schema, that can be used instead by adding `--schema-command` argument
4951

5052
```bash
5153
php artisan test --gql-coverage --schema-command="php artisan lighthouse:print-schema-v2"
5254
```
5355

56+
### Excluding nodes from total coverage
57+
58+
By default, all nodes will be included when calculating coverage. However, if you have nodes such as the built-in
59+
Lighthouse pagination types that you do not want to be covered, you can configure ignored fields from your `Pest.php` configuration file.
60+
61+
```php
62+
<?php
63+
64+
declare(strict_types=1);
65+
66+
use Worksome\PestGraphqlCoverage\Config;
67+
68+
Config::new()->ignore([
69+
'PaginatorInfo.count',
70+
// ...
71+
]);
72+
```
73+
5474
### Native Pest usage
5575

5676
This also works natively with Pest (without using Artisan), as it is a Pest plugin.

src/Config.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Worksome\PestGraphqlCoverage;
6+
7+
final class Config
8+
{
9+
/** @var array<string, mixed> */
10+
private static array $ignoredNodes = [];
11+
12+
public static function new(): self
13+
{
14+
return new self();
15+
}
16+
17+
/** @param array<string> $ignoredNodes */
18+
public function ignore(array $ignoredNodes): self
19+
{
20+
self::$ignoredNodes = array_merge(self::$ignoredNodes, array_flip($ignoredNodes));
21+
22+
return $this;
23+
}
24+
25+
/** @return array<string, mixed> */
26+
public static function ignoredNodes(): array
27+
{
28+
return self::$ignoredNodes;
29+
}
30+
}

src/Plugin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ public function addOutput(int $testReturnCode): int
138138
// Create an array of all untested nodes.
139139
$untested = array_diff_key($dottedNodes, $dottedTestedNodes);
140140

141+
// Remove ignored nodes
142+
$untested = array_diff_key($untested, Config::ignoredNodes());
143+
141144
// Count the nodes and calculate the percentage of tested nodes.
142145
$totalNodes = count($dottedNodes);
143146
$totalTestedNodes = $totalNodes - count($untested);

0 commit comments

Comments
 (0)