Validation.spec.ts 2.8 KB

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