Combining duplicate step definitions

In a similar vein, we can introduce parameters into our step definitions to help us avoid duplicated code. With string patterns, parameters can be specified using curly braces ({}), inside of which the type of the variable is indicated.

For example, our Then our API should respond with a <statusCode> HTTP status code step definition can be redefined as follows:

Then('our API should respond with a {int} HTTP status code', function (statusCode) {  assert.equal(this.response.statusCode, statusCode);});

Here, we've replaced the hardcoded 400 HTTP status code with a placeholder, {int}, which indicates that the pattern should match an integer. Then, we are passing the value of the placeholder into the code ...

Get Building Enterprise JavaScript Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.