JavaScriptObfuscatorInternal.spec.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { IObfuscationResult } from "../../src/interfaces/IObfuscationResult";
  2. import { JavaScriptObfuscatorInternal } from "../../src/JavaScriptObfuscatorInternal";
  3. import { NO_CUSTOM_NODES_PRESET } from "../../src/preset-options/NoCustomNodesPreset";
  4. const assert: Chai.AssertStatic = require('chai').assert;
  5. describe('JavaScriptObfuscatorInternal', () => {
  6. describe(`setSourceMapUrl (url: string)`, () => {
  7. let javaScriptObfuscator: JavaScriptObfuscatorInternal,
  8. obfuscationResult: IObfuscationResult,
  9. sourceMapUrl: string;
  10. it('should link obfuscated code with source map', () => {
  11. sourceMapUrl = 'test.map.js';
  12. javaScriptObfuscator = new JavaScriptObfuscatorInternal(
  13. `var test = 1;`,
  14. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  15. sourceMap: true
  16. })
  17. );
  18. javaScriptObfuscator.obfuscate();
  19. javaScriptObfuscator.setSourceMapUrl(sourceMapUrl);
  20. obfuscationResult = javaScriptObfuscator.getObfuscationResult();
  21. assert.match(
  22. obfuscationResult.getObfuscatedCode(),
  23. new RegExp(`sourceMappingURL=${sourceMapUrl}`))
  24. ;
  25. assert.isOk(JSON.parse(obfuscationResult.getSourceMap()).mappings);
  26. });
  27. it('should properly add source map import to the obfuscated code if `sourceMapBaseUrl` is set', () => {
  28. sourceMapUrl = 'http://localhost:9000/';
  29. javaScriptObfuscator = new JavaScriptObfuscatorInternal(
  30. `var test = 1;`,
  31. Object.assign({}, NO_CUSTOM_NODES_PRESET, {
  32. sourceMap: true,
  33. sourceMapBaseUrl: sourceMapUrl
  34. })
  35. );
  36. javaScriptObfuscator.obfuscate();
  37. obfuscationResult = javaScriptObfuscator.getObfuscationResult();
  38. assert.match(
  39. obfuscationResult.getObfuscatedCode(),
  40. new RegExp(`sourceMappingURL=${sourceMapUrl}$`))
  41. ;
  42. assert.isOk(JSON.parse(obfuscationResult.getSourceMap()).mappings);
  43. });
  44. });
  45. });