Skip to content

Commit

Permalink
feat: add plugin options
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Sep 18, 2024
1 parent 2c0c594 commit f781c5e
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 118 deletions.
210 changes: 162 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,91 +12,180 @@ This plugin provides a zeros configuration setup to run e2e tests in a package m
{
"plugin": "@push-based/build-env",
"options": {
"environmentsDir": "tmp/environments" // Optional
"environments": {
"environmentsDir": "tmp/environments" // Optional
"targetNames": ["e2e"] // Optional
}
}
}
]
}
```

Now you can configure the project you want to e2e test as published package.
> [!NOTE]
> Your configured targets now have a new dependency configured:
>
> ```jsonc
> {
> "name": "utils-e2e",
> "targets": {
> "e2e-special": {
> "dependsOn": [
> // dynamically aded
> { "projects": "self", "target": "setup-env", "params": "forward" }
> ]
> // ...
> }
> }
> // ...
> }
> ```
2. Add the package under test as `implicitDependency` to your e2e project.
The plugin will detect implicit dependencies and use them for the environment setup.
```jsonc
// projects/utils-e2e/project.json
{
"name": "utils-e2e",
"implicitDependency": ["utils"]
}
```
Now you are ready to go.

3. Run your e2e test with `nx run utils-e2e:e2e`

Tadaaaa! 🎉

## Options

| Name | type | description |
| -------------------------------- | ------------------------------------- | -------------------------------------------------------------------------------------------- |
| **environments.environmentsDir** | `string` (DEFAULT 'tmp/environments') | The folder name of the generated environments |
| **environments.targetNames** | `string[]` (REQUIRED) | The target names of projects depending on environments |
| **environments.filterByTag** | `string[]` (REQUIRED) | The tag names a projects needs to have to be considered for a environments (match is one of) |
| **publishable.filterByTag** | `string[]` (REQUIRED) | The tag names a projects needs to have to be considered for publishing (match is one of) |

### Fine-grained selection of publishable projects

By default, all projects with type `library` get automatically the following targets applied:

- `build-env--npm-publish`
- `build-env--npm-install`

You can configure the plugin to only add those targets to projects with one or many specific tags.

2. Add a `publishable` tag to the package under test to tell the plugin which projects it should consider as publishable
1. Configure the plugin with a tag to act as filter

```jsonc
// projects/my-lib/project.json
{
"name": "my-lib",
"targets": ["publish", "nx-release-publish"]
"tags": ["publishable"] // Optionally filter projects by tags for a more finegrained control
"plugins": [
{
"plugin": "@push-based/build-env",
"options": {
"publishable": {
"filterByTags": ["publishable"]
}
}
}
]
}
```

2. Add a `publishable` tag to the projects considered in test environments

```jsonc
// projects/utils/project.json
{
"name": "utils",
//
"tags": ["publishable"]
// ...
}
```

Next you need to configure the e2e project that uses the package under test.
### Fine-grained selection of projects that need a test environment set up

3. Add the package under test as `implicitDependency` to your e2e project. The plugin will detect implicit dependencies and use them for the environment setup.
#### Filter by target names

You can configure the plugin to select only specific projects for environment creation.

1. Configure the plugin with a targets that will be configured with a dependency to the test environment

```jsonc
// projects/my-lib-e2e/project.json
{
"name": "my-lib-e2e",
"implicitDependency": ["my-lib"]
"plugins": [
{
"plugin": "@push-based/build-env",
"options": {
"environments": {
"targetNames": ["e2e", "e2e-static"]
}
}
}
]
}
```

4. Configure the `setup-env` target as dependent target in your e2e test project by using `dependsOn`
### Filter by tags

You can configure the plugin to only add those targets to projects with one or many specific tags.

1. Configure the plugin with a tag to act as filter

```jsonc
{
"name": "my-lib-e2e",
"targets": {
"e2e": {
"dependsOn": [
{
"projects": "self",
"target": "setup-env",
"params": "forward"
"plugins": [
{
"plugin": "@push-based/build-env",
"options": {
"environments": {
"filterByTags": ["npm-env"]
}
]
// ...
}
}
}
// ...
]
}
```

Now you are ready to go.

5. Run your e2e test with `nx run my-lib-e2e:e2e`
2. Add a `environments` tag to the projects considered in test environments

Tadaaaa! 🎉
```jsonc
// projects/utils/project.json
{
"name": "lib-e2e",
//
"tags": ["npm-env"]
// ...
}
```

**Example usage:**

- `nx run cli-e2e:e2e` - setup environment and then run E2E tests for `cli-e2e`
- `nx run cli-static-e2e:e2e --environmentRoot static-environments/user-lists` - setup NPM in existing environment and then run E2E tests for `cli-static-e2e`
- `nx run utils-e2e:e2e` - setup environment and then run E2E tests for `utils-e2e`
- `nx run utils-static-e2e:e2e --environmentRoot static-environments/user-lists` - setup NPM in existing environment and then run E2E tests for `utils-static-e2e`

## DX while debugging e2e tests

Debug full environment in 1 setup:

- `nx run cli-e2e:setup-env` - setup environment for `cli-e2e`
- `nx run cli-e2e:setup-env --keepServerRunning` - keeps Verdaccio running after setup
- `nx run cli-e2e:stop-verdaccio` - stops the Verdaccio server for `cli-e2e`
- `nx run utils-e2e:setup-env` - setup environment for `utils-e2e`
- `nx run utils-e2e:setup-env --keepServerRunning` - keeps Verdaccio running after setup
- `nx run utils-e2e:stop-verdaccio` - stops the Verdaccio server for `utils-e2e`

Debug full environment in 2 steps:

- `nx run cli-e2e:bootstrap-env` - setup folders and starts Verdaccio for `cli-e2e`
- `nx run cli-e2e:install-env` - bootstraps and installs all dependencies for `cli-e2e`
- `nx run cli-e2e:stop-verdaccio` - stops the Verdaccio server for `cli-e2e`
- `nx run utils-e2e:bootstrap-env` - setup folders and starts Verdaccio for `utils-e2e`
- `nx run utils-e2e:install-env` - bootstraps and installs all dependencies for `utils-e2e`
- `nx run utils-e2e:stop-verdaccio` - stops the Verdaccio server for `utils-e2e`

Debug packages:

- `nx run cli-e2e:bootstrap-env` - setup folders and starts Verdaccio for `cli-e2e`
- `nx run utils:npm-publish --environmentProject cli-e2e` - publishes `utils` and `models` to the Verdaccio registry configured for `cli-e2e`
- `nx run utils:npm-install --environmentProject cli-e2e` - installs `utils` and `models` from the Verdaccio registry configured for `cli-e2e`
- `nx run cli-e2e:stop-verdaccio` - stops the Verdaccio server for `cli-e2e`
- `nx run utils-e2e:bootstrap-env` - setup folders and starts Verdaccio for `utils-e2e`
- `nx run utils:npm-publish --environmentProject utils-e2e` - publishes `utils` and `models` to the Verdaccio registry configured for `utils-e2e`
- `nx run utils:npm-install --environmentProject utils-e2e` - installs `utils` and `models` from the Verdaccio registry configured for `utils-e2e`
- `nx run utils-e2e:stop-verdaccio` - stops the Verdaccio server for `utils-e2e`

## Benefits in depth

Expand All @@ -120,11 +209,11 @@ Root/
│ └── e2e/
│ └── <project-name>/
│ ├── storage/... # npm publish/unpublish
│ │ └── @my-org
│ │ └── my-lib/...
│ ├── node_modules/
│ │ └── <org>
│ │ └── <package-name>/... # npm install/uninstall
│ │ └── <package-name>/...
│ ├── node_modules/ # npm install/uninstall
│ │ └── <org>
│ │ └── <package-name>/...
│ ├── __test__/...
│ │ └── <test-file-name>/...
│ │ └── <it-block-setup>/...
Expand Down Expand Up @@ -219,14 +308,39 @@ This is a first draft of how the benchmarks will look. ATM the data set it not b
> Work on the real benchmark data in progress
| cli:e2e | Common | Optimized |
|:----------------:|:---------:|:---------:|
| :--------------: | :-------: | :-------: |
| Execution Time | 110 s | 13 s |
| Download Volume | 381.68 MB | 381.68 MB |
| Cacheable | | |
| Cacheable |||
| Graph Nodes | 1 | 13 |
| Can run parallel | | |
| Can run parallel |||

## Connect with us!

- [Check out our services](https://push-based.io)
- [Follow us on Twitter](https://twitter.com/pushbased)

<!--
```mermaid
flowchart TB
UE["utils:e2e"] -->USE["utils:setup-env"]

USE["utils:setup-env"] --> UIE["utils:install-env"]
UIE["utils:install-env"] --> utils:install-env

subgraph utils:install-env
UI["utils:install"] --> UP["utils:publish"]
UI["utils:install"] --> MI["models:install"]
UP["utils:publish"] --> MP["models:publish"]
MI["models:install"] --> MP["models:publish"]
end

utils:install-env --> UB["utils:build"]
UB["utils:build"] --> MB["models:build"]

```
-->
```
7 changes: 0 additions & 7 deletions examples/e2e/cli-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
"targets": {
"lint": {},
"e2e": {
"dependsOn": [
{
"projects": "self",
"target": "setup-env",
"params": "forward"
}
],
"executor": "@nx/vite:test",
"inputs": ["default", "^production"],
"outputs": ["{options.reportsDirectory}"],
Expand Down
9 changes: 1 addition & 8 deletions examples/e2e/cli-static-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@
"implicitDependencies": ["cli"],
"targets": {
"lint": {},
"e2e": {
"dependsOn": [
{
"projects": "self",
"target": "setup-env",
"params": "forward"
}
],
"e2e-static": {
"executor": "@nx/vite:test",
"inputs": ["default", "^production"],
"outputs": ["{options.reportsDirectory}"],
Expand Down
7 changes: 0 additions & 7 deletions examples/e2e/core-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
"targets": {
"lint": {},
"e2e": {
"dependsOn": [
{
"projects": "self",
"target": "setup-env",
"params": "forward"
}
],
"executor": "@nx/vite:test",
"inputs": ["default", "^production"],
"outputs": ["{options.reportsDirectory}"],
Expand Down
7 changes: 0 additions & 7 deletions examples/e2e/models-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
"targets": {
"lint": {},
"e2e": {
"dependsOn": [
{
"projects": "self",
"target": "setup-env",
"params": "forward"
}
],
"executor": "@nx/vite:test",
"inputs": ["default", "^production"],
"outputs": ["{options.reportsDirectory}"],
Expand Down
7 changes: 0 additions & 7 deletions examples/e2e/playground-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
"targets": {
"lint": {},
"e2e": {
"dependsOn": [
{
"projects": "self",
"target": "setup-env",
"params": "forward"
}
],
"executor": "@nx/vite:test",
"inputs": ["default", "^production"],
"outputs": ["{options.reportsDirectory}"],
Expand Down
7 changes: 0 additions & 7 deletions examples/e2e/utils-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@
"targets": {
"lint": {},
"e2e": {
"dependsOn": [
{
"projects": "self",
"target": "setup-env",
"params": "forward"
}
],
"executor": "@nx/vite:test",
"inputs": ["default", "^production"],
"outputs": ["{options.reportsDirectory}"],
Expand Down
2 changes: 1 addition & 1 deletion examples/projects/cli/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "cli",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "examples/projects/cli/src",
"projectType": "application",
"projectType": "library",
"release": {
"version": {
"generatorOptions": {
Expand Down
12 changes: 11 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,17 @@
},
"plugins": [
"./examples/e2e/cli-e2e-original/tooling/original.plugin.ts",
"@push-based/build-env",
{
"plugin": "@push-based/build-env",
"options": {
"publishable": {
"filterByTags": ["publishable"]
},
"environments": {
"targetNames": ["e2e", "e2e-static"]
}
}
},
{
"plugin": "@nx/eslint/plugin",
"options": {
Expand Down
Loading

0 comments on commit f781c5e

Please sign in to comment.