UNPKG

2.93 kBJavaScriptView Raw
1/* eslint-disable no-console */
2/**
3 * @file Defines the MochaDevelopmentExecutionContext class.
4 *
5 * @author Luke Chavers <luke@c2cschools.com>
6 * @author Kevin Sanders <kevin@c2cschools.com>
7 * @since 5.0.0
8 * @license See LICENSE.md for details about licensing.
9 * @copyright 2017 C2C Schools, LLC
10 */
11
12"use strict";
13
14// Important Note
15// --------------
16// This module only loads a single dependency, directly, which is the
17// parent class for the class defined within. This is intended to force
18// dependency loading through the parent class, by way of the `$dep()`
19// method, in order to centralize dependency definition and loading.
20
21const BaseMochaExecutionContext = require(
22 "./abstract/BaseMochaExecutionContext"
23);
24
25/**
26 * Represents an execution context whereby an endpoint is executed
27 * in the local environment by way of Mocha during development (BDD).
28 *
29 * @memberOf ExecutionContext
30 * @extends ExecutionContext.BaseMochaExecutionContext
31 */
32class MochaDevelopmentExecutionContext extends BaseMochaExecutionContext {
33
34 // noinspection JSUnusedGlobalSymbols
35 /**
36 * Called whenever the endpoint execution operation fails. This method
37 * overrides the default behavior by showing a stack trace whenever an
38 * error is thrown.
39 *
40 * @private
41 * @param {error} err - An error object (usually one that stems from
42 * {@link Errors.BaseError} ).
43 * @returns {Promise} resolved with a {@link Response.ErrorResponse} object.
44 */
45 _fail( err ) {
46
47 // In local Mocha/BDD development, it is useful to see
48 // the stacktrace whenever an error occurs.
49 console.log( "\n \n \n " );
50 console.log( "Error Stack (Output from MochaDevelopmentExecutionContext.js):" );
51 console.log( "\n " );
52 console.log( err.stack );
53 console.log( "\n \n \n " );
54
55 // Pass through
56 return super._fail( err );
57 }
58
59 // noinspection JSMethodCanBeStatic
60 /**
61 * The name of the current "API Stage" (e.g. "v5", "v5-dev", etc). This
62 * getter overrides the default behavior by returning a static string
63 * ("mochaDev").
64 *
65 * @public
66 * @readonly
67 * @type {string}
68 */
69 get apiStage() {
70
71 // Mocha won't have the usual 'Api Stage' information,
72 // so we'll force a value for it.
73
74 // FIXME: This breaks local vs CI testing because the stage is used
75 // to build the error URL in ErrorResponse.js
76
77 return "mochaDev";
78 }
79
80 /**
81 * Generates a fake requestId that is prefixed with four 4's.
82 *
83 * @private
84 * @returns {string} A fake request ID.
85 */
86 _createFakeRequestId() {
87
88 const me = this;
89
90 // Although we want to generate a valid requestId (for realism),
91 // we're going to set the first block of characters to something
92 // that identifies the UUID as being unusual and, further, as
93 // being generated by this line of execution contexts.
94
95 // Dependencies
96 const uuidUtils = me.$dep( "util/uuid" );
97
98 return uuidUtils.generate( "4444" );
99 }
100}
101
102module.exports = MochaDevelopmentExecutionContext;