EXAMPLES/test-plugin-example

Test Plugin Example

Demonstrates the test plugin with command execution and assertions. Tests verify script behavior including exit codes and output content.

meta-yml9 FILEStestingassertionscommands

Test Plugin Example

This example demonstrates the @functional-examples/test plugin, which allows you to define and run tests for your examples.

Usage

bash
1# From the repo root
2npx functional-examples test examples/test-plugin-example

Sub-Examples

Hello Script

A simple script that prints a greeting:

const args = process.argv.slice(2);

if (args.includes('--fail')) {
  console.error('Error: intentional failure');
  process.exit(1);
}

console.log('Hello from example!');
text
1Test definition:
2
3```json title="examples/hello/package.json#hello-test"
4  "functional-examples": {
5    "title": "Hello Script",
6    "test": [
7      {
8        "name": "runs hello script",
9        "options": { "command": "node hello.js" },
10        "assertions": {
11          "exitCode": 0,
12          "stdout": { "contains": "Hello from example" }
13        }
14      }
15    ]
16  },

Error Handling

A script that demonstrates failure assertions:

const args = process.argv.slice(2);

if (args.includes('--fail')) {
  console.error('Error: intentional failure');
  process.exit(1);
}

console.log('No failure triggered');
text
1Test definition:
2
3```json title="examples/error-handling/package.json#error-test"
4  "functional-examples": {
5    "title": "Error Handling",
6    "test": [
7      {
8        "name": "fails with bad args",
9        "options": { "command": "node fail.js --fail" },
10        "assertions": {
11          "exitCode": 1,
12          "stderr": { "contains": "Error" }
13        }
14      }
15    ]
16  },

Assertions

Available assertions:

  • exitCode — Expected exit code (number)
  • stdout.contains — String that should appear in stdout
  • stderr.contains — String that should appear in stderr

Use Cases

  • Verify examples actually run
  • Ensure expected output is produced
  • Catch regressions in example code

All Example Files

FILE EXPLORER
README.md
1# Test Plugin Example
2
3This example demonstrates the `@functional-examples/test` plugin, which allows you to define and run tests for your examples.
4
5## Usage
6
7```bash
8# From the repo root
9npx functional-examples test examples/test-plugin-example
10```
11
12## Sub-Examples
13
14### Hello Script
15
16A simple script that prints a greeting:
17
18<%= region('hello-script') %>
19
20Test definition:
21
22<%= region('hello-test') %>
23
24### Error Handling
25
26A script that demonstrates failure assertions:
27
28<%= region('error-script') %>
29
30Test definition:
31
32<%= region('error-test') %>
33
34## Assertions
35
36Available assertions:
37- `exitCode` — Expected exit code (number)
38- `stdout.contains` — String that should appear in stdout
39- `stderr.contains` — String that should appear in stderr
40
41## Use Cases
42
43- Verify examples actually run
44- Ensure expected output is produced
45- Catch regressions in example code
46