test.node.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. "use strict";
  2. /* NOTE: I've used supervisor to auto-restart mocha, because mocha --watch
  3. didn't work for selenium tests (not reloading them)...
  4. ------------------------------------------------------------------ */
  5. require ('chai').should ()
  6. /* ------------------------------------------------------------------------ */
  7. describe ('get-source', () => {
  8. const getSource = require ('../get-source'),
  9. fs = require ('fs'),
  10. path = require ('path')
  11. it ('cache sanity check', () => {
  12. getSource ('./get-source.js').should.equal (getSource ('./get-source.js'))
  13. getSource ('./get-source.js').should.not.equal (getSource ('./package.json'))
  14. })
  15. it ('reads sources (not sourcemapped)', () => {
  16. const original = getSource ('./test/files/original.js')
  17. original.path.should.equal (path.resolve ('./test/files/original.js')) // resolves input paths
  18. original.text.should.equal (fs.readFileSync ('./test/files/original.js', { encoding: 'utf-8' }))
  19. original.lines.should.deep.equal ([
  20. '/*\tDummy javascript file\t*/',
  21. '',
  22. 'function hello () {',
  23. '\treturn \'hello world\' }'
  24. ])
  25. const resolved = original.resolve ({ line: 4, column: 1 })
  26. resolved.line.should.equal (4)
  27. resolved.column.should.equal (1)
  28. resolved.sourceFile.should.equal (original)
  29. resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
  30. })
  31. it ('reads sources (sourcemapped, with external links)', () => {
  32. const uglified = getSource ('./test/files/original.uglified.js')
  33. uglified.path.should.equal (path.resolve ('./test/files/original.uglified.js'))
  34. uglified.lines.should.deep.equal ([
  35. 'function hello(){return"hello world"}',
  36. '//# sourceMappingURL=original.uglified.js.map',
  37. ''
  38. ])
  39. // uglified.sourceMap.should.not.equal (undefined)
  40. // uglified.sourceMap.should.equal (uglified.sourceMap) // memoization should work
  41. const resolved = uglified.resolve ({ line: 1, column: 18 }) // should be tolerant to column omission
  42. resolved.line.should.equal (4)
  43. resolved.column.should.equal (2)
  44. resolved.sourceFile.should.equal (getSource ('./test/files/original.js'))
  45. resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
  46. })
  47. it ('reads sources (sourcemapped, with external links) — ASYNC', () => {
  48. const uglified = getSource.async ('./test/files/original.uglified.js')
  49. return uglified.then (uglified => {
  50. uglified.path.should.equal (path.resolve ('./test/files/original.uglified.js'))
  51. uglified.lines.should.deep.equal ([
  52. 'function hello(){return"hello world"}',
  53. '//# sourceMappingURL=original.uglified.js.map',
  54. ''
  55. ])
  56. // uglified.sourceMap.should.not.equal (undefined)
  57. // uglified.sourceMap.should.equal (uglified.sourceMap) // memoization should work
  58. return uglified.resolve ({ line: 1, column: 18 }).then (resolved => {
  59. return getSource.async ('./test/files/original.js').then (originalFile => {
  60. resolved.line.should.equal (4)
  61. resolved.column.should.equal (2)
  62. resolved.sourceFile.should.equal (originalFile)
  63. resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
  64. })
  65. })
  66. })
  67. })
  68. it ('reads sources (sourcemapped, with embedded sources)', () => {
  69. const uglified = getSource ('./test/files/original.uglified.with.sources.js')
  70. uglified.path.should.equal (path.resolve ('./test/files/original.uglified.with.sources.js'))
  71. uglified.lines.should.deep.equal ([
  72. 'function hello(){return"hello world"}',
  73. '//# sourceMappingURL=original.uglified.with.sources.js.map',
  74. ''
  75. ])
  76. // uglified.sourceMap.should.not.equal (undefined)
  77. const resolved = uglified.resolve ({ line: 1, column: 18 })
  78. resolved.line.should.equal (4)
  79. resolved.column.should.equal (2)
  80. resolved.sourceFile.path.should.equal (path.resolve ('./test/files') + '/## embedded ##') // I've changed the filename manually, by editing .map file
  81. resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
  82. })
  83. it ('reads sources (sourcemapped, with inline base64 sourcemaps)', () => {
  84. const babeled = getSource ('./test/files/original.babeled.with.inline.sourcemap.js')
  85. // babeled.sourceMap.should.not.equal (undefined)
  86. // babeled.sourceMap.file.path.should.equal (babeled.path)
  87. const resolved = babeled.resolve ({ line: 6, column: 1 })
  88. resolved.line.should.equal (4)
  89. resolved.column.should.equal (2)
  90. resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
  91. })
  92. it ('supports even CHAINED sourcemaps!', () => {
  93. /* original.js → original.uglified.js → original.uglified.beautified.js */
  94. const beautified = getSource ('./test/files/original.uglified.beautified.js')
  95. beautified.path.should.equal (path.resolve ('./test/files/original.uglified.beautified.js'))
  96. beautified.text.should.equal (fs.readFileSync ('./test/files/original.uglified.beautified.js', { encoding: 'utf-8' }))
  97. // beautified.sourceMap.should.not.equal (undefined)
  98. const resolved = beautified.resolve ({ line: 2, column: 4 })
  99. resolved.line.should.equal (4)
  100. resolved.column.should.equal (2)
  101. resolved.sourceFile.path.should.equal (path.resolve ('./test/files/original.js'))
  102. resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
  103. })
  104. it ('adheres to async interface', () => {
  105. return getSource.async ('./get-source.js').then (result => {
  106. ;(result.resolve ({ line: 1, column: 0 }) instanceof Promise).should.equal (true)
  107. })
  108. })
  109. it ('supports even CHAINED sourcemaps! — ASYNC', () => {
  110. /* original.js → original.uglified.js → original.uglified.beautified.js */
  111. return getSource.async ('./test/files/original.uglified.beautified.js').then (beautified => {
  112. beautified.text.should.equal (fs.readFileSync ('./test/files/original.uglified.beautified.js', { encoding: 'utf-8' }))
  113. beautified.path.should.equal (path.resolve ('./test/files/original.uglified.beautified.js'))
  114. return beautified.resolve ({ line: 2, column: 4 }).then (resolved => {
  115. resolved.line.should.equal (4)
  116. resolved.column.should.equal (2)
  117. resolved.sourceFile.path.should.equal (path.resolve ('./test/files/original.js'))
  118. resolved.sourceLine.should.equal ('\treturn \'hello world\' }')
  119. })
  120. })
  121. })
  122. it ('does some error handling', () => {
  123. const nonsense = getSource ('abyrvalg')
  124. nonsense.text.should.equal ('')
  125. nonsense.error.should.be.an.instanceof (Error)
  126. const resolved = nonsense.resolve ({ line: 5, column: 0 })
  127. resolved.error.should.equal (nonsense.error)
  128. resolved.sourceLine.should.equal ('')
  129. resolved.sourceFile.path.should.equal ('abyrvalg')
  130. })
  131. it ('does some error handling - ASYNC', () => {
  132. return getSource.async ('abyrvalg').then (x => { console.log (x) }).catch (error => {
  133. error.should.be.an.instanceof (Error)
  134. })
  135. })
  136. it ('allows absolute paths', () => {
  137. getSource (require ('path').resolve ('./get-source.js')).should.equal (getSource ('./get-source.js'))
  138. })
  139. it ('caching works', () => {
  140. const files =
  141. [ './get-source.js',
  142. './package.json',
  143. './test/files/original.js',
  144. './test/files/original.uglified.js',
  145. './test/files/original.uglified.js.map',
  146. './test/files/original.uglified.with.sources.js',
  147. './test/files/original.uglified.with.sources.js.map',
  148. './test/files/original.babeled.with.inline.sourcemap.js',
  149. './test/files/original.uglified.beautified.js',
  150. './test/files/original.uglified.beautified.js.map',
  151. './abyrvalg' ]
  152. Object.keys (getSource.getCache ()).should.deep.equal (files.map (x => path.resolve (x)))
  153. getSource.resetCache ()
  154. Object.keys (getSource.getCache ()).length.should.equal (0)
  155. })
  156. })