UNPKG

6.14 kBJavaScriptView Raw
1/* eslint-disable no-console */
2/**
3 * This file creates a global object that provides a number of useful
4 * tools and utilities for testing. It is loaded, automatically, by Mocha
5 * (via the --require arg) and, therefore, does not need to be required.
6 *
7 * @module mocha-endpoint-test-helper
8 * @author Luke Chavers <luke@c2cschools.com>
9 * @author Kevin Sanders <kevin@c2cschools.com>
10 * @created 2017-09-01
11 */
12
13"use strict";
14
15// Debug for Warning: Possible EventEmitter memory leak detected. 11 exit listeners added.
16// process.on( "warning", e => console.warn( e.stack ) );
17
18// Say hello
19console.log( "[auto-globals] Loading global unit testing tools & utilities... " );
20
21// Init global utilities object
22let _utils = {
23 env : {},
24 libs : {},
25 paths : {},
26};
27
28/** @global */
29global.utils = _utils;
30
31// Load some basic settings from environment variables...
32if ( process.env.ENDPOINT_TEST_TARGET === undefined ) {
33
34 throw new Error(
35 "Error in Mocha Test Helper (_mocha-endpoint-test-helper.js): The " +
36 "variable ENDPOINT_TEST_TARGET is required, but was not provided."
37 );
38}
39
40if ( process.env.ENDPOINT_PROJECT_PATH === undefined ) {
41
42 throw new Error(
43 "Error in Mocha Test Helper (_mocha-endpoint-test-helper.js): The " +
44 "variable ENDPOINT_PROJECT_PATH is required, but was not provided."
45 );
46
47} else {
48
49 _utils.env.endpointProjectPath = process.env.ENDPOINT_PROJECT_PATH;
50}
51
52// FIXME: this file is not based on BaseClass, so we can't use $dep() to get these
53// FIXME: so is it ok to also include these in microservices dependencies?
54
55// Dependencies
56const EYES = require( "eyes" );
57const TIPE = require( "tipe" );
58const LODASH = require( "lodash" );
59const CHAI = require( "chai" );
60const BLUEBIRD = require( "bluebird" );
61const PATH = require( "path" );
62const EndpointTestHarness = require( "./EndpointTestHarness" );
63
64// We'll go ahead and allow some of the
65// dependency modules to exist as globals.
66// global._ = LODASH;
67// global.tipe = TIPE;
68global.expect = CHAI.expect;
69
70// Everything else will be accessible
71// through the global 'utils' object.
72_utils.libs = {
73 eyes : EYES,
74 tipe : TIPE,
75 lodash : LODASH,
76 chai : CHAI,
77 bluebird : BLUEBIRD,
78 path : PATH,
79};
80
81// Resolve a few paths.
82_utils.paths.project = _utils.env.endpointProjectPath;
83_utils.paths.projectLib = PATH.join( _utils.paths.project, "lib" );
84_utils.paths.endpoints = PATH.join( _utils.paths.projectLib, "endpoints" );
85_utils.paths.models = PATH.join( _utils.paths.projectLib, "models" );
86_utils.paths.tests = PATH.join( _utils.paths.project, "test" );
87_utils.paths.coreMS = PATH.join( _utils.paths.project, "node_modules/@corefw/microservices" );
88_utils.paths.toolsLib = PATH.join( _utils.paths.coreMS, "lib" );
89_utils.paths.testLib = PATH.join( _utils.paths.toolsLib, "test" );
90
91// Resolve endpoint test target
92if ( TIPE( process.env.ENDPOINT_TEST_TARGET ) === "string" ) {
93
94 switch ( process.env.ENDPOINT_TEST_TARGET.toLowerCase() ) {
95
96 case "local":
97 console.log( "[auto-globals] Found environment variable 'ENDPOINT_TEST_TARGET'..." );
98 console.log( "[auto-globals] ...setting test target to 'local'." );
99 _utils.env.endpointTestTarget = "local";
100 break;
101
102 case "dev":
103 console.log( "[auto-globals] Found environment variable 'ENDPOINT_TEST_TARGET'..." );
104 console.log( "[auto-globals] ...setting test target to 'dev'." );
105 _utils.env.endpointTestTarget = "dev";
106 break;
107
108 case "production":
109 console.log( "[auto-globals] Found environment variable 'ENDPOINT_TEST_TARGET'..." );
110 console.log( "[auto-globals] ...setting test target to 'production'." );
111 _utils.env.endpointTestTarget = "production";
112 break;
113
114 default:
115 console.log( "[auto-globals] Found environment variable 'ENDPOINT_TEST_TARGET'..." );
116 console.log( "[auto-globals] ...but it had an invalid value..." );
117 console.log( "[auto-globals] ...defaulting test target to 'local'." );
118 _utils.env.endpointTestTarget = "local";
119 break;
120 }
121
122} else {
123
124 console.log( "[auto-globals] Environment variable 'ENDPOINT_TEST_TARGET' not found..." );
125 console.log( "[auto-globals] ...defaulting test target to 'local'." );
126 _utils.env.endpointTestTarget = "local";
127}
128
129// Pad the output log
130console.log( " " );
131
132/**
133 * Reads the contents of a YAML file and returns it as an object.
134 *
135 * @protected
136 * @param {string} absPath - The absolute path of the file to read
137 * @param {?string} [property=null] - When provided, and not null, this
138 * method will return the value of a property from within the loaded
139 * YAML file. If not provided, or NULL, then the root object will be
140 * returned.
141 * @returns {Object} The file contents.
142 */
143_utils.loadYamlFile = function ( absPath, property ) {
144
145 return EndpointTestHarness.loadYamlFile( absPath, property );
146};
147
148/**
149 * Creates and returns an EndpointTestHarness (from @corefw/microservices) that
150 * can be used to test API endpoints.
151 *
152 * @public
153 * @param {Object} cfg - Configuration object.
154 * @param {string} cfg.endpointDirectoryPath - The absolute path of the endpoint
155 * directory.
156 * @param {string} [cfg.relEndpointDirectoryPath] - The path of the endpoint
157 * directory, relative from the "endpoints" directory in the project.
158 * @param {string} cfg.dataDirectoryPath - An absolute path to the
159 * testing data.
160 * @returns {EndpointTestHarness} The endpoint test harness.
161 */
162_utils.createEndpointHarness = function ( cfg ) {
163
164 // Default config
165 if ( TIPE( cfg ) !== "object" ) {
166
167 cfg = {};
168 }
169
170 // Resolve relative path, if provided.
171 if ( cfg.relEndpointDirectoryPath !== undefined ) {
172
173 if ( cfg.endpointDirectoryPath === undefined ) {
174
175 // Resolve path
176 cfg.endpointDirectoryPath = PATH.join(
177 _utils.paths.endpoints,
178 cfg.relEndpointDirectoryPath
179 );
180 }
181
182 // This is not useful to the harness...
183 delete cfg.relEndpointDirectoryPath;
184 }
185
186 // Set the testing mode.
187 if ( cfg.testMode === undefined ) {
188
189 cfg.testMode = _utils.env.endpointTestTarget;
190 }
191
192 // Load the harness
193 return new EndpointTestHarness( cfg );
194};