ConsoleOutputDisableTemplate.spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 {string} consoleMethod
  36. * @returns {Function}
  37. */
  38. function getFunctionFromTemplate (
  39. templateData: any,
  40. callsControllerFunctionName: string,
  41. consoleMethod: keyof Console
  42. ): Function {
  43. const formattedTemplate: string = format(ConsoleOutputDisableTemplate(), templateData);
  44. return Function(`
  45. var ${callsControllerFunctionName} = (function(){
  46. return function (context, fn){
  47. return function () {
  48. return fn.apply(context, arguments);
  49. };
  50. }
  51. })();
  52. ${formattedTemplate}
  53. console.${consoleMethod}(1);
  54. console.${consoleMethod}.toString();
  55. `);
  56. }
  57. describe('ConsoleOutputDisableTemplate', () => {
  58. const callControllerFunctionName: string = 'callsController';
  59. const consoleLogDisableFunctionName: string = 'consoleLogDisable';
  60. let consoleObjectStubs: sinon.SinonStub[];
  61. let processStdoutWriteSpy: sinon.SinonSpy;
  62. before(() => {
  63. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  64. inversifyContainerFacade.load('', '', {});
  65. });
  66. beforeEach(() => {
  67. processStdoutWriteSpy = sinon.spy();
  68. consoleObjectStubs = consoleMethods.map((methodName: keyof Console) => {
  69. return sinon.stub(console, methodName).callsFake(processStdoutWriteSpy);
  70. });
  71. });
  72. describe('Base behaviour', () => {
  73. consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
  74. describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
  75. beforeEach(() => {
  76. const testFunc = getFunctionFromTemplate(
  77. {
  78. consoleLogDisableFunctionName,
  79. callControllerFunctionName,
  80. globalVariableTemplate: GlobalVariableTemplate1(),
  81. },
  82. callControllerFunctionName,
  83. consoleMethodName
  84. );
  85. testFunc();
  86. });
  87. it(`should replace \`console.${consoleMethodName}\` call with an empty function`, () => {
  88. assert.isNull(processStdoutWriteSpy.getCall(0));
  89. });
  90. });
  91. });
  92. });
  93. describe('Partial `console` object', () => {
  94. consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
  95. describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
  96. beforeEach(() => {
  97. const testFunc = getFunctionFromTemplate(
  98. {
  99. consoleLogDisableFunctionName,
  100. callControllerFunctionName,
  101. globalVariableTemplate: getPartialConsoleObjectGlobalVariableTemplate(),
  102. },
  103. callControllerFunctionName,
  104. consoleMethodName
  105. );
  106. testFunc();
  107. });
  108. it(`should replace \`console.${consoleMethodName}\` call with an empty function`, () => {
  109. assert.isNull(processStdoutWriteSpy.getCall(0));
  110. });
  111. });
  112. });
  113. });
  114. describe('Undefined `console` object', () => {
  115. consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
  116. describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
  117. beforeEach(() => {
  118. const testFunc = getFunctionFromTemplate(
  119. {
  120. consoleLogDisableFunctionName,
  121. callControllerFunctionName,
  122. globalVariableTemplate: getUndefinedConsoleObjectGlobalVariableTemplate(),
  123. },
  124. callControllerFunctionName,
  125. consoleMethodName
  126. );
  127. testFunc();
  128. });
  129. it(`should replace \`console.${consoleMethodName}\` call with an empty function`, () => {
  130. assert.isNull(processStdoutWriteSpy.getCall(0));
  131. });
  132. });
  133. });
  134. });
  135. describe('Wrong global variable template', () => {
  136. consoleMethods.forEach((consoleMethodName: keyof Console, index: number) => {
  137. describe(`Variant #${index + 1}: \`console.${consoleMethodName}\` call`, () => {
  138. beforeEach(() => {
  139. const testFunc = getFunctionFromTemplate(
  140. {
  141. consoleLogDisableFunctionName,
  142. callControllerFunctionName,
  143. globalVariableTemplate: getWrongGlobalVariableTemplate(),
  144. },
  145. callControllerFunctionName,
  146. consoleMethodName
  147. );
  148. testFunc();
  149. });
  150. it(`should not replace \`console.${consoleMethodName}\` call with an empty function`, () => {
  151. assert.isNotNull(processStdoutWriteSpy.getCall(0));
  152. });
  153. });
  154. });
  155. });
  156. afterEach(() => {
  157. consoleObjectStubs.forEach((stub: sinon.SinonStub) => stub.restore());
  158. });
  159. });