Scenario(YAML)
May 2, 2024 ยท View on GitHub
You can write tests in YAML, and Echoed will convert them into tests.
Supported frameworks
Framework specific features are documented in the following documents:
Variable
In YAML, there are several predefined variables that you can use:
_: Represents the result ofactin the current step. (inarrange,_is to reference the result of the currentarrange.)_steps: Represents the result ofactin steps.
You can access it by index, for example,_steps[-1]or_steps[2]. A negative index indicates the relative position from the current step, while a positive index signifies the absolute position starting with zero._env: Represents the environment variables defined in the configuration._arranges: Represents the result ofarrangein the current step inarrangesection.
You can access it by index as same as_steps.
Configuration
You can configure the behavior of the scenario by adding configuration like below at .echoed.yml file:
scenario:
compile:
env:
BASE_ENDPOINT: http://localhost:8080 # <- You can enable the lookup of environment variables with/without default values.
plugin:
runners:
- name: fetch
module: echoed/scenario/gen/jest/runner
option:
baseEndpoint: ${_env.BASE_ENDPOINT}/api # <- You can reference environment variables with `_env`.
headers:
content-type: application/json
commons: # <- You can import any module.
- names:
- createSession
module: "@/example/util/session"
This configuration example does following:
- Sets the default value for the environment variable
BASE_ENDPOINT. - Sets the default options for the
fetchrunner. - Imports
createSessionfunction from@/example/util/session.
For more detailed information, refer to configSchema.ts.
Plugin
To extend scenarios with additional functionalities, you can add functions at plugin section in the .echoed.yml file.
There are three types of plugins: Runner, Asserter and Common.
-
Runner
A function that is called insideactandarrange, typically performing actions like an HTTP request.
Its result is stored in the_variable. -
Asserter
A function that is called insideassert, taking two arguments, typically for the expected and actual values. -
Common
A function or variable that is usable anywhere except sections forRunnerandAsserter.
Typically, it is called inside${}.
Built-in Plugins
Some plugins are available by default.
Refer to documents for corresponding framework for a full list of plugins.
- Jest
- Playwright
If the name of built-in plugin collides with other plugin, the non-built-in plugin will take precedence.
Custom Plugin
You can easily create custom plugins by creating function implementing corresponding interface and configure them in the plugin section.
Runner
To create a custom runner, implement a function following theRunnerinterface inechoed/scenario/gen/jest/runner.
Here's an example:const myRunner = async ( ctx: EchoedContext, argument: Argument, option: Option, ): Promise<Response> => { return await myFunction(argument); }Asserter
To create a custom asserter, implement a function following theAsserterinterface inechoed/scenario/gen/jest/asserter.
Here's an example:const myAsserter = ( ctx: unknown, expected: number, actual: number, option: Option, ): void => { expect(actual).toBe(expected); }
To use custom plugins, add them at the plugin section in .echoed.yml file.
For instance, if you've created above plugins in @/plugin, configure them as follows:
scenario:
compile:
plugin:
runner:
- name: myRunner
module: "@/plugin"
asserter:
- name: myAsserter
module: "@/plugin"