Skip to content

Commit

Permalink
Merge pull request #7 from worksome/feature/exclude-nodes
Browse files Browse the repository at this point in the history
feat: add support for excluding nodes
  • Loading branch information
owenvoke authored Apr 5, 2023
2 parents 315ab09 + c2a2570 commit 8b735cb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ php artisan test -p --gql-coverage
```

### Setting coverage limits

By adding the argument `--gql-min=<percentage>`, we can limit to have a min coverage of x.

```bash
Expand All @@ -44,13 +45,32 @@ php artisan test --gql-coverage --gql-untested-count=25
```

### Changing default schema fetching command

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

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

### Excluding nodes from total coverage

By default, all nodes will be included when calculating coverage. However, if you have nodes such as the built-in
Lighthouse pagination types that you do not want to be covered, you can configure ignored fields from your `Pest.php` configuration file.

```php
<?php

declare(strict_types=1);

use Worksome\PestGraphqlCoverage\Config;

Config::new()->ignore([
'PaginatorInfo.count',
// ...
]);
```

### Native Pest usage

This also works natively with Pest (without using Artisan), as it is a Pest plugin.
Expand Down
30 changes: 30 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Worksome\PestGraphqlCoverage;

final class Config
{
/** @var array<string, mixed> */
private static array $ignoredNodes = [];

public static function new(): self
{
return new self();
}

/** @param array<string> $ignoredNodes */
public function ignore(array $ignoredNodes): self
{
self::$ignoredNodes = array_merge(self::$ignoredNodes, array_flip($ignoredNodes));

return $this;
}

/** @return array<string, mixed> */
public static function ignoredNodes(): array
{
return self::$ignoredNodes;
}
}
3 changes: 3 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ public function addOutput(int $testReturnCode): int
// Create an array of all untested nodes.
$untested = array_diff_key($dottedNodes, $dottedTestedNodes);

// Remove ignored nodes
$untested = array_diff_key($untested, Config::ignoredNodes());

// Count the nodes and calculate the percentage of tested nodes.
$totalNodes = count($dottedNodes);
$totalTestedNodes = $totalNodes - count($untested);
Expand Down

0 comments on commit 8b735cb

Please sign in to comment.