ConsoleOutputDisableTemplate.spec.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import 'reflect-metadata';
  2. import format from 'string-template';
  3. import { assert } from 'chai';
  4. import * as sinon from 'sinon';
  5. import { IInversifyContainerFacade } from '../../../../../src/interfaces/container/IInversifyContainerFacade';
  6. import { ConsoleOutputDisableTemplate } from '../../../../../src/custom-code-helpers/console-output/templates/ConsoleOutputDisableTemplate';
  7. import { GlobalVariableTemplate1 } from '../../../../../src/custom-code-helpers/common/templates/GlobalVariableTemplate1';
  8. import { InversifyContainerFacade } from '../../../../../src/container/InversifyContainerFacade';
  9. const consoleMethods: (keyof Console)[] = ['log', 'warn', 'info', 'error', 'table', 'trace'];
  10. /**
  11. * @returns {string}
  12. */
  13. const getWrongGlobalVariableTemplate: () => string = () => `
  14. var that = {};
  15. `;
  16. /**
  17. * @returns {string}
  18. */
  19. const getUndefinedConsoleObjectGlobalVariableTemplate: () => string = () => `
  20. ${GlobalVariableTemplate1()}
  21. that.console = undefined;
  22. `;
  23. /**
  24. * @returns {string}
  25. */
  26. const getPartialConsoleObjectGlobalVariableTemplate: () => string = () => `
  27. ${GlobalVariableTemplate1()}
  28. that.console = {
  29. log: that.console.log
  30. };
  31. `;
  32. /**
  33. * @param templateData
  34. * @param {string} callsControllerFunctionName
  35. * @param {keyof Console} consoleMethod
  36. * @param {string} additionalCode
  37. * @returns {Function}
  38. */
  39. function getFunctionFromTemplate (
  40. templateData: any,
  41. callsControllerFunctionName: string,
  42. consoleMethod: keyof Console,
  43. additionalCode: string = ''
  44. ): Function {
  45. const formattedTemplate: string = format(ConsoleOutputDisableTemplate(), templateData);
  46. return Function(`
  47. var ${callsControllerFunctionName} = (function(){
  48. return function (context, fn){
  49. return function () {
  50. return fn.apply(context, arguments);
  51. };
  52. }
  53. })();
  54. ${formattedTemplate}
  55. console.${consoleMethod}(1);
  56. console.${consoleMethod}.toString();
  57. ${additionalCode}
  58. `);
  59. }
  60. describe('ConsoleOutputDisableTemplate', () => {
  61. const callControllerFunctionName: string = 'callsController';
  62. const consoleLogDisableFunctionName: string = 'consoleLogDisable';
  63. let consoleObjectStubs: sinon.SinonStub[];
  64. let processStdoutWriteSpy: sinon.SinonSpy;
  65. before(() => {
  66. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  67. inversifyContainerFacade.load('', '', {});
  68. });
  69. beforeEach(() => {
  70. processStdoutWriteSpy = sinon.spy();
  71. consoleObjectStubs = consoleMethods.map((methodName: keyof Console) => {
  72. return sinon.stub(console, methodName).callsFake(processStdoutWriteSpy);
  73. });
  74. });
  75. describe('Base behaviour', () => {
  76. consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
  77. describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
  78. beforeEach(() => {
  79. const testFunc = getFunctionFromTemplate(
  80. {
  81. consoleLogDisableFunctionName,
  82. callControllerFunctionName,
  83. globalVariableTemplate: GlobalVariableTemplate1(),
  84. },
  85. callControllerFunctionName,
  86. consoleMethodName
  87. );
  88. testFunc();
  89. });
  90. it(`should replace \`console.${consoleMethodName}\` call with an empty function`, () => {
  91. assert.isNull(processStdoutWriteSpy.getCall(0));
  92. });
  93. });
  94. });
  95. });
  96. describe('Partial `console` object', () => {
  97. consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
  98. describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
  99. beforeEach(() => {
  100. const testFunc = getFunctionFromTemplate(
  101. {
  102. consoleLogDisableFunctionName,
  103. callControllerFunctionName,
  104. globalVariableTemplate: getPartialConsoleObjectGlobalVariableTemplate(),
  105. },
  106. callControllerFunctionName,
  107. consoleMethodName
  108. );
  109. testFunc();
  110. });
  111. it(`should replace \`console.${consoleMethodName}\` call with an empty function`, () => {
  112. assert.isNull(processStdoutWriteSpy.getCall(0));
  113. });
  114. });
  115. });
  116. });
  117. describe('Undefined `console` object', () => {
  118. consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
  119. describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
  120. beforeEach(() => {
  121. const testFunc = getFunctionFromTemplate(
  122. {
  123. consoleLogDisableFunctionName,
  124. callControllerFunctionName,
  125. globalVariableTemplate: getUndefinedConsoleObjectGlobalVariableTemplate(),
  126. },
  127. callControllerFunctionName,
  128. consoleMethodName
  129. );
  130. testFunc();
  131. });
  132. it(`should replace \`console.${consoleMethodName}\` call with an empty function`, () => {
  133. assert.isNull(processStdoutWriteSpy.getCall(0));
  134. });
  135. });
  136. });
  137. });
  138. describe('Wrong global variable template', () => {
  139. consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
  140. describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
  141. beforeEach(() => {
  142. const testFunc = getFunctionFromTemplate(
  143. {
  144. consoleLogDisableFunctionName,
  145. callControllerFunctionName,
  146. globalVariableTemplate: getWrongGlobalVariableTemplate(),
  147. },
  148. callControllerFunctionName,
  149. consoleMethodName
  150. );
  151. testFunc();
  152. });
  153. it(`should not replace \`console.${consoleMethodName}\` call with an empty function`, () => {
  154. assert.isNotNull(processStdoutWriteSpy.getCall(0));
  155. });
  156. });
  157. });
  158. });
  159. describe('`bind` call', () => {
  160. consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
  161. describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
  162. let testFunc: () => void;
  163. beforeEach(() => {
  164. testFunc = () => getFunctionFromTemplate(
  165. {
  166. consoleLogDisableFunctionName,
  167. callControllerFunctionName,
  168. globalVariableTemplate: GlobalVariableTemplate1(),
  169. },
  170. callControllerFunctionName,
  171. consoleMethodName,
  172. `console.${consoleMethodName}.bind();`
  173. )();
  174. });
  175. it(`should does not throw error during \`console.${consoleMethodName}.bind\` call`, () => {
  176. assert.doesNotThrow(testFunc);
  177. });
  178. });
  179. });
  180. });
  181. afterEach(() => {
  182. consoleObjectStubs.forEach((stub: sinon.SinonStub) => stub.restore());
  183. });
  184. });