Star

Created With

linkRun Existing Specs Using JS

REST-EZ supports re-using specs & their result via the this.runSpec() method



linkSpec in the Same Suite File

You can re-use an existing spec in your suite with JS:

star-wars-api-using-spec-in-same-file.yml
1linkmeta:

2link name: star wars api using yaml anchors

3linkconfiguration:

4link scheme: http

5link host: swapi.dev

6linkspecs:

7link - name: fetch film by film ID

8link request:

9link path: /api/films/{filmId}/

10link method: get

11link path_params:

12link - name: filmId

13link value: 1

14link response:

15link status_code: 200

16link - name: fetch person for film 2

17link request:

18link path: /api/people/{personId}/

19link method: get

20link before_test:

21link run_type: inline

22link inline:

23link function: !js/asyncFunction >

24link async function() {

25link var response = await this.runSpec('fetch film by film ID');

26link // name: "A New Hope"

27link this.suite.film = JSON.parse(response.body);

28link var characterUrl = this.suite.film.characters[0];

29link var personId = characterUrl.match(/\/api\/people\/(\d+)\//)[1];

30link this.test.path_params = { personId };

31link }

32link response:

33link status_code: 200



linkSpec Within a Different Suite File

Intrasuite dependencies can be specified to allow using specs in a different suite

Use the spec_dependencies suite-level property to enable this. Paths are relative to current working directory of Node process.

ℹ️ NOTE: that these imported specs will only be available when you run them using this.runSpec.



specs/new-suite.yml
1linkmeta:

2link name: Suite Relying on a Dependency

3linkspec_dependencies:

4link - specs/existing-suite.yml

5linkconfiguration:

6link scheme: http

7link host: swapi.dev

8linkspecs:

9link - name: fetch person for film 2

10link request:

11link path: /api/people/{personId}/

12link method: get

13link before_test:

14link run_type: inline

15link inline:

16link function: !js/asyncFunction >

17link async function() {

18link var response = await this.runSpec('fetch film by film ID');

19link // name: "A New Hope"

20link this.suite.film = JSON.parse(response.body);

21link var characterUrl = this.suite.film.characters[0];

22link var personId = characterUrl.match(/\/api\/people\/(\d+)\//)[1];

23link this.test.path_params = { personId };

24link }

25link response:

26link status_code: 200

specs/existing-suite.yml
1linkmeta:

2link name: Suite With an Existing Spec

3linkconfiguration:

4link scheme: http

5link host: swapi.dev

6linkspecs:

7link - name: fetch film by film ID

8link request:

9link path: /api/films/{filmId}/

10link method: get

11link path_params:

12link - name: filmId

13link value: 1

14link response:

15link status_code: 200



💡 To use a path relative to suite's path, use the locate_files_relative parameter of the suite meta section.



See also: Example: Run Specs from External File



linkspec_dependencies

Root-level suite property.

A list of file paths that this suite relies on to use runSpec method.

1linkspec_dependencies:

2link - specs/some-suite.yml

3link - specs/some-other-suite.yml

4link# ...



linkThe runSpec Method

runSpec(name, options)

Returns Promise<Response> The response generated by the spec

1link/**

2link * Run an existing spec in this suite

3link * or an external suite connected via interspec_dependencies suite config

4link *

5link * @param {string} specName Name of spec to run (identified by `name` property of the spec)

6link * @param {object} options Override the original spec

7link * @param {string} options.path The URL

8link * @param {string} options.method HTTP request method

9link * @param {array} options.headers Object of key/value pairs to set as headers

10link * @param {array} options.path_params Object of key/value pairs to set in the path

11link * @param {array} options.query_params Object of key/value pairs to add as query params ?name=value&name2=value2

12link * @param {object} options.payload Request body payload

13link *

14link * @return Promise<Response>

15link */

16linkfunction runSpec(specName, options) { }

example-spec.yml
1link# ...

2link - name: generate auth token

3link request:

4link path: /auth/login

5link method: post

6link payload:

7link body:

8link type: json

9link content:

10link email: foo@bar.com

11link password: F2#@9!mls

12link response:

13link status_code: 201

14link json_data:

15link - name: $.token

16link value: !!js/regexp /^[A-Z0-9]{32}$

17link - name: fetch a person

18link request:

19link path: /user

20link method: get

21link before_test:

22link run_type: inline

23link inline:

24link function: !js/asyncFunction >

25link async function() {

26link var response = await this.runSpec('generate auth token');

27link var { token } = JSON.parse(response.body);

28link this.test.headers = {

29link 'Authorization': `Bearer ${token}`

30link };

31link }

32link response:

33link status_code: 200

Run Existing Specs Using JSSpec in the Same Suite FileSpec Within a Different Suite Filespec_dependenciesThe runSpec Method

Home

Getting Startedchevron_right
Basicschevron_right
Requestchevron_right

Request Basics Headers Cookies Query Parameters Path Parameters

Payload Bodychevron_right

File Uploads Auto-Retry Additional Options

Response Validationchevron_right

Overview

Assertionschevron_right

Status Code Headers & Cookies

Bodychevron_right
Hooks & JSchevron_right

Overview

Function Typeschevron_right
Suite-Level Hookschevron_right
Spec-Level Hookschevron_right
Contextchevron_right
DRY & Reusing Specschevron_right
Command Line Interface (CLI)chevron_right