Repository: elastic/synthetics
Pull request #650 (2022-11-09 03:11:54)

Add lightweight YAML example to `init`
Now that we support lightweight monitors we should show folks how it's done. This is needed to better support docs for the current 8.5 release and an upcoming blog post.

apmmachine: 
## :broken_heart: Tests Failed


<!-- BUILD BADGES-->
> _the below badges are clickable and redirect to their specific view in the CI or DOCS_
[![Pipeline View](https://img.shields.io/badge/pipeline-pipeline%20-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-650/detail/PR-650/1//pipeline) [![Test View](https://img.shields.io/badge/test-test-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-650/detail/PR-650/1//tests) [![Changes](https://img.shields.io/badge/changes-changes-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-650/detail/PR-650/1//changes) [![Artifacts](https://img.shields.io/badge/artifacts-artifacts-yellow)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-650/detail/PR-650/1//artifacts) [![preview](https://img.shields.io/badge/docs-preview-yellowgreen)](http://synthetics_650.docs-preview.app.elstc.co/diff)  [![preview](https://img.shields.io/badge/elastic-observability-blue)](https://ci-stats.elastic.co/app/apm/services/apm-ci/transactions/view?rangeFrom=2022-11-09T03:02:12.177Z&rangeTo=2022-11-09T03:22:12.177Z&transactionName=BUILD+apm-agent-rum%2Felastic-synthetics%2FPR-%7Bnumber%7D&transactionType=job&latencyAggregationType=avg&traceId=ee799580794a7776c59443219c179333&transactionId=120117350399a69c)

<!-- BUILD SUMMARY-->
<details><summary>Expand to view the summary</summary>
<p>

#### Build stats




* Start Time: 2022-11-09T03:12:12.177+0000


* Duration: 8 min 22 sec




#### Test stats :test_tube:

| Test         | Results                         |
| ------------ | :-----------------------------: |
| Failed       | 4  |
| Passed       | 195  |
| Skipped      | 2 |
| Total        | 201   |


</p>
</details>

<!-- TEST RESULTS IF ANY-->

  ### Test errors [![4](https://img.shields.io/badge/4%20-red)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-650/detail/PR-650/1//tests)
<details><summary>Expand to view the tests failures</summary><p>
    
    
##### `Test / Push API handle streamed response – Push API handle streamed response`


<ul>
<details><summary>Expand to view the error details</summary><p>

```
 null 
 ```
</p></details>
<details><summary>Expand to view the stacktrace</summary><p>

```
 Error: expect(received).toMatchSnapshot()

Snapshot name: `Push API handle streamed response 1`

- Snapshot  - 1
+ Received  + 1

  > preparing all monitors
  > creating all monitors
  > journey 1
  > journey 2
  > deleting all stale monitors
- ✓ Pushed
+ ✓ Pushed: http://localhost:54455/stream/app/uptime/manage-monitors/all
  ↵
    at Object.<anonymous> (/var/lib/jenkins/workspace/nt-rum_elastic-synthetics_PR-650/src/github.com/elastic/synthetics/__tests__/push/index.test.ts:305:22)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) 
 ```
</p></details>
</ul>
    
##### `Test / Push API handle sync response – Push API handle sync response`


<ul>
<details><summary>Expand to view the error details</summary><p>

```
 null 
 ```
</p></details>
<details><summary>Expand to view the stacktrace</summary><p>

```
 Error: expect(received).toMatchSnapshot()

Snapshot name: `Push API handle sync response 1`

- Snapshot  - 1
+ Received  + 1

  > preparing all monitors
  > creating all monitors
  > deleting all stale monitors
- ✓ Pushed
+ ✓ Pushed: http://localhost:54455/sync/app/uptime/manage-monitors/all
  ↵
    at Object.<anonymous> (/var/lib/jenkins/workspace/nt-rum_elastic-synthetics_PR-650/src/github.com/elastic/synthetics/__tests__/push/index.test.ts:297:22)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) 
 ```
</p></details>
</ul>
    
##### `Test / network cached resource timings – network cached resource timings`


<ul>
<details><summary>Expand to view the error details</summary><p>

```
 null 
 ```
</p></details>
<details><summary>Expand to view the stacktrace</summary><p>

```
 TypeError: Cannot read properties of null (reading 'wait')
    at /var/lib/jenkins/workspace/nt-rum_elastic-synthetics_PR-650/src/github.com/elastic/synthetics/__tests__/plugins/network.test.ts:277:21
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/var/lib/jenkins/workspace/nt-rum_elastic-synthetics_PR-650/src/github.com/elastic/synthetics/__tests__/plugins/network.test.ts:275:15) 
 ```
</p></details>
</ul>
    
##### `Lint / Fix End of Files – pre_commit.lint`


<ul>
<details><summary>Expand to view the error details</summary><p>

```
 error 
 ```
</p></details>
<details><summary>Expand to view the stacktrace</summary><p>

```
 ![CDATA[- hook id: end-of-file-fixer
- exit code: 1
- files were modified by this hook

Fixing templates/journeys/lightweight-example.yaml

]] 
 ```
</p></details>
</ul>
    
</p></details>


<!-- STEPS ERRORS IF ANY -->














### Steps errors [![1](https://img.shields.io/badge/1%20-red)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-650/detail/PR-650/1//pipeline)
  <details><summary>Expand to view the steps failures</summary>
  <p>


  
  
  
  
##### `Runs the tests`
<ul>
<li>Took 2 min 54 sec . View more details <a href="https://apm-ci.elastic.co//blue/rest/organizations/jenkins/pipelines/apm-agent-rum/pipelines/elastic-synthetics/pipelines/PR-650/runs/1/steps/346/log/?start=0">here</a></li>
<li>Description: <code>./node_modules/.bin/jest --ci --reporters=default --reporters=jest-junit</code></l1>
</ul>
  
  </p>
  </details>



## :robot: GitHub comments

<details><summary>Expand to view the GitHub comments</summary>
<p>

To re-run your PR in the CI, just comment with:



- `/test` : Re-trigger the build.




</p>
</details>

<!--COMMENT_GENERATED_WITH_ID_comment.id-->

Patch:
diff --git a/.gitignore b/.gitignore
index 8399ae0..d83fc53 100644
--- a/.gitignore
+++ b/.gitignore
@@ -119,3 +119,5 @@ __tests__/e2e/junit.xml
 
 .idea/
 seccomp/build
+
+test-projects/
diff --git a/src/generator/utils.ts b/src/generator/utils.ts
index a37bfc0..0231543 100644
--- a/src/generator/utils.ts
+++ b/src/generator/utils.ts
@@ -47,6 +47,7 @@ export function replaceTemplates(input: string, values: Record<string, any>) {
       if (Array.isArray(finalValue)) {
         return String(finalValue)
           .split(',')
+          .filter(Boolean)
           .map(f => `'${f}'`);
       }
       if (typeof finalValue == 'number') {
diff --git a/templates/journeys/lightweight-example.yaml b/templates/journeys/lightweight-example.yaml
new file mode 100644
index 0000000..33381fc
--- /dev/null
+++ b/templates/journeys/lightweight-example.yaml
@@ -0,0 +1,5 @@
+- type: http
+  name: Todos Lightweight
+  id: todos-lightweight
+  urls: "https://elastic.github.io/synthetics-demo/"
+  schedule: '@every 1m'
\ No newline at end of file


Test patch:
diff --git a/__tests__/generator/index.test.ts b/__tests__/generator/index.test.ts
index 698f53f..97dbfac 100644
--- a/__tests__/generator/index.test.ts
+++ b/__tests__/generator/index.test.ts
@@ -44,7 +44,7 @@ describe('Generator', () => {
   });
   it('generate synthetics project - NPM', async () => {
     // In a few development environments, this test may take
-    // a few millisseconds more than the default 15000ms timeout
+    // a few milliseconds more than the default 15000ms timeout
     jest.setTimeout(30000);
     const cli = new CLIMock().args(['init', scaffoldDir]).run({
       env: {
diff --git a/__tests__/generator/utils.test.ts b/__tests__/generator/utils.test.ts
new file mode 100644
index 0000000..56e2f52
--- /dev/null
+++ b/__tests__/generator/utils.test.ts
@@ -0,0 +1,51 @@
+/**
+ * MIT License
+ *
+ * Copyright (c) 2020-present, Elastic NV
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+import { readFileSync } from 'fs';
+import { join } from 'path';
+import { replaceTemplates } from '../../src/generator/utils';
+
+describe('Generator utils', () => {
+  it('does not add empty values in array', async () => {
+    const template = readFileSync(
+      join(__dirname, '..', '..', 'templates/synthetics.config.ts'),
+      'utf-8'
+    );
+    const values = {
+      locations: ['us_east'],
+      privateLocations: [''],
+      schedule: 30,
+      id: 'test',
+      space: 'kbn',
+      url: 'foo:bar',
+    };
+    const finalValue = replaceTemplates(template, values);
+
+    expect(finalValue).toContain("locations: ['us_east'],");
+    expect(finalValue).toContain('privateLocations: [],');
+
+    expect(finalValue).not.toContain("['']");
+  });
+});
diff --git a/__tests__/utils/test-config.ts b/__tests__/utils/test-config.ts
index 99f3b81..a2848dd 100644
--- a/__tests__/utils/test-config.ts
+++ b/__tests__/utils/test-config.ts
@@ -80,7 +80,11 @@ export class CLIMock {
   run(spawnOverrides?: { cwd?: string; env?: NodeJS.ProcessEnv }): CLIMock {
     this.process = spawn(
       'node',
-      [join(__dirname, '..', '..', 'dist', 'cli.js'), ...this.cliArgs],
+      [
+        '--no-warnings',
+        join(__dirname, '..', '..', 'dist', 'cli.js'),
+        ...this.cliArgs,
+      ],
       {
         env: process.env,
         stdio: 'pipe',