GlobalVariableNoEvalTemplate.spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import 'reflect-metadata';
  2. import format from 'string-template';
  3. import { assert } from 'chai';
  4. import { GlobalVariableNoEvalTemplate } from '../../../../../src/custom-code-helpers/common/templates/GlobalVariableNoEvalTemplate';
  5. describe('GlobalVariableNoEvalTemplate', () => {
  6. describe('Variant #1: simple', () => {
  7. const expectedGlobalObject: NodeJS.Global = global;
  8. let globalObject: NodeJS.Global;
  9. before(() => {
  10. const globalVariableNoEvalTemplate: string = format(GlobalVariableNoEvalTemplate());
  11. globalObject = Function(`
  12. ${globalVariableNoEvalTemplate}
  13. return that;
  14. `)();
  15. });
  16. it('should correctly return global object', () => {
  17. assert.deepEqual(globalObject, expectedGlobalObject);
  18. });
  19. });
  20. describe('Variant #2: call inside function', () => {
  21. const expectedGlobalObject: NodeJS.Global = global;
  22. let globalObject: NodeJS.Global;
  23. before(() => {
  24. const globalVariableNoEvalTemplate: string = format(GlobalVariableNoEvalTemplate());
  25. globalObject = Function(`
  26. return (function () {
  27. ${globalVariableNoEvalTemplate}
  28. return that;
  29. })();
  30. `)();
  31. });
  32. it('should correctly return global object', () => {
  33. assert.deepEqual(globalObject, expectedGlobalObject);
  34. });
  35. });
  36. describe('Variant #3: return `window`', () => {
  37. const expectedGlobalObject: {} = {
  38. document: {}
  39. };
  40. let globalObject: NodeJS.Global;
  41. before(() => {
  42. const globalVariableNoEvalTemplate: string = format(GlobalVariableNoEvalTemplate());
  43. globalObject = Function(`
  44. this.window = {
  45. document: {}
  46. };
  47. ${globalVariableNoEvalTemplate}
  48. return that;
  49. `)();
  50. // for some reason it couldn't correctly compare global objects without JSON.stringify/JSON.parse
  51. globalObject = JSON.parse(JSON.stringify(globalObject));
  52. });
  53. it('should correctly return `window` object', () => {
  54. assert.deepEqual(globalObject, expectedGlobalObject);
  55. });
  56. });
  57. });