IsAllowedForObfuscationTarget.spec.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { assert } from 'chai';
  2. import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
  3. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
  4. import { ObfuscationTarget } from '../../../../../src/enums/ObfuscationTarget';
  5. import { SourceMapMode } from '../../../../../src/enums/source-map/SourceMapMode';
  6. describe('`sourceMapMode` IsAllowedForObfuscationTarget validator', () => {
  7. describe('Variant #1: positive validation', () => {
  8. describe('Variant #1: obfuscation target: `browser`', () => {
  9. let testFunc: () => string;
  10. beforeEach(() => {
  11. testFunc = () => JavaScriptObfuscator.obfuscate(
  12. '',
  13. {
  14. ...NO_ADDITIONAL_NODES_PRESET,
  15. sourceMapMode: SourceMapMode.Inline,
  16. target: ObfuscationTarget.Browser
  17. }
  18. ).getObfuscatedCode();
  19. });
  20. it('should pass validation when obfuscation target is `browser`', () => {
  21. assert.doesNotThrow(testFunc);
  22. });
  23. });
  24. describe('Variant #2: obfuscation target: `node` and default value', () => {
  25. let testFunc: () => string;
  26. beforeEach(() => {
  27. testFunc = () => JavaScriptObfuscator.obfuscate(
  28. '',
  29. {
  30. ...NO_ADDITIONAL_NODES_PRESET,
  31. sourceMapMode: SourceMapMode.Separate,
  32. target: ObfuscationTarget.Node
  33. }
  34. ).getObfuscatedCode();
  35. });
  36. it('should pass validation when obfuscation target is `node` and value is default', () => {
  37. assert.doesNotThrow(testFunc);
  38. });
  39. });
  40. });
  41. describe('Variant #2: negative validation', () => {
  42. const expectedError: string = 'This option allowed only for obfuscation targets';
  43. let testFunc: () => string;
  44. beforeEach(() => {
  45. testFunc = () => JavaScriptObfuscator.obfuscate(
  46. '',
  47. {
  48. ...NO_ADDITIONAL_NODES_PRESET,
  49. sourceMapMode: SourceMapMode.Inline,
  50. target: ObfuscationTarget.Node
  51. }
  52. ).getObfuscatedCode();
  53. });
  54. it('should not pass validation when obfuscation target is `node` and value is not default', () => {
  55. assert.throws(testFunc, expectedError);
  56. });
  57. });
  58. });