/test/integration.coffee

https://github.com/pthrasher/snockets · CoffeeScript · 167 lines · 146 code · 20 blank · 1 comment · 21 complexity · aec786fa3e2f664a0cbb9b8197bd9a27 MD5 · raw file

  1. Snockets = require '../lib/snockets'
  2. path = require 'path'
  3. src = '../test/assets'
  4. snockets = new Snockets({src})
  5. testSuite =
  6. 'Independent JS files have no dependencies': (test) ->
  7. snockets.scan 'b.js', (err) ->
  8. throw err if err
  9. test.ok snockets.depGraph.map['b.js']
  10. test.deepEqual snockets.depGraph.getChain('b.js'), []
  11. test.done()
  12. 'Single-step dependencies are correctly recorded': (test) ->
  13. snockets.scan 'a.coffee', (err) ->
  14. throw err if err
  15. test.deepEqual snockets.depGraph.getChain('a.coffee'), ['b.js']
  16. test.done()
  17. 'Dependencies with multiple extensions are accepted': (test) ->
  18. snockets.scan 'testing.js', (err) ->
  19. throw err if err
  20. test.deepEqual snockets.depGraph.getChain('testing.js'), ['1.2.3.coffee']
  21. test.done()
  22. 'Dependencies can have subdirectory-relative paths': (test) ->
  23. snockets.scan 'song/loveAndMarriage.js', (err) ->
  24. throw err if err
  25. test.deepEqual snockets.depGraph.getChain('song/loveAndMarriage.js'), ['song/horseAndCarriage.coffee']
  26. test.done()
  27. 'Multiple dependencies can be declared in one require directive': (test) ->
  28. snockets.scan 'poly.coffee', (err) ->
  29. throw err if err
  30. test.deepEqual snockets.depGraph.getChain('poly.coffee'), ['b.js', 'x.coffee']
  31. test.done()
  32. 'Chained dependencies are correctly recorded': (test) ->
  33. snockets.scan 'z.coffee', (err) ->
  34. throw err if err
  35. test.deepEqual snockets.depGraph.getChain('z.coffee'), ['x.coffee', 'y.js']
  36. test.done()
  37. 'Dependency cycles cause no errors during scanning': (test) ->
  38. snockets.scan 'yin.js', (err) ->
  39. throw err if err
  40. test.throws -> snockets.depGraph.getChain('yin.js')
  41. test.throws -> snockets.depGraph.getChain('yang.coffee')
  42. test.done()
  43. 'require_tree works for same directory': (test) ->
  44. snockets.scan 'branch/center.coffee', (err) ->
  45. throw err if err
  46. chain = snockets.depGraph.getChain('branch/center.coffee')
  47. test.deepEqual chain, ['branch/edge.coffee', 'branch/periphery.js', 'branch/subbranch/leaf.js']
  48. test.done()
  49. 'require works for includes that are relative to orig file using ../': (test) ->
  50. snockets.scan 'first/syblingFolder.js', (err) ->
  51. throw err if err
  52. chain = snockets.depGraph.getChain('first/syblingFolder.js')
  53. test.deepEqual chain, ['sybling/sybling.js']
  54. test.done()
  55. 'require_tree works for nested directories': (test) ->
  56. snockets.scan 'fellowship.js', (err) ->
  57. throw err if err
  58. chain = snockets.depGraph.getChain('fellowship.js')
  59. test.deepEqual chain, ['middleEarth/legolas.coffee', 'middleEarth/shire/bilbo.js', 'middleEarth/shire/frodo.coffee']
  60. test.done()
  61. 'require_tree works for redundant directories': (test) ->
  62. snockets.scan 'trilogy.coffee', (err) ->
  63. throw err if err
  64. chain = snockets.depGraph.getChain('trilogy.coffee')
  65. test.deepEqual chain, ['middleEarth/shire/bilbo.js', 'middleEarth/shire/frodo.coffee', 'middleEarth/legolas.coffee']
  66. test.done()
  67. 'getCompiledChain returns correct .js filenames and code': (test) ->
  68. snockets.getCompiledChain 'z.coffee', (err, chain) ->
  69. throw err if err
  70. test.deepEqual chain, [
  71. {filename: 'x.js', js: '(function() {\n "Double rainbow\\nSO INTENSE";\n\n}).call(this);\n'}
  72. {filename: 'y.js', js: '//= require x'}
  73. {filename: 'z.js', js: '(function() {\n\n\n}).call(this);\n'}
  74. ]
  75. test.done()
  76. 'getCompiledChain returns correct .js filenames and code with ../ in require path': (test) ->
  77. snockets.getCompiledChain 'first/syblingFolder.js', (err, chain) ->
  78. throw err if err
  79. test.deepEqual chain, [
  80. {filename: 'sybling/sybling.js', js: 'var thereWillBeJS = 3;'}
  81. {filename: 'first/syblingFolder.js', js: '//= require ../sybling/sybling.js'}
  82. ]
  83. test.done()
  84. 'getConcatenation returns correct raw JS code with ../ in require path': (test) ->
  85. snockets.getConcatenation 'first/syblingFolder.js', (err, js1, changed) ->
  86. throw err if err
  87. test.equal js1, """
  88. var thereWillBeJS = 3;
  89. //= require ../sybling/sybling.js
  90. """
  91. test.done()
  92. 'getConcatenation returns correct raw JS code': (test) ->
  93. snockets.getConcatenation 'z.coffee', (err, js1, changed) ->
  94. throw err if err
  95. test.equal js1, """
  96. (function() {\n "Double rainbow\\nSO INTENSE";\n\n}).call(this);\n
  97. //= require x
  98. (function() {\n\n\n}).call(this);\n
  99. """
  100. snockets.getConcatenation 'z.coffee', (err, js2, changed) ->
  101. throw err if err
  102. test.ok !changed
  103. test.equal js1, js2
  104. test.done()
  105. 'getConcatenation returns correct minified JS code': (test) ->
  106. snockets.getConcatenation 'z.coffee', minify: true, (err, js) ->
  107. throw err if err
  108. test.equal js, """
  109. (function(){"Double rainbow\\nSO INTENSE"}).call(this);\n\n(function(){}).call(this);
  110. """
  111. test.done()
  112. 'getConcatenation caches minified JS code': (test) ->
  113. flags = minify: true
  114. snockets.getConcatenation 'jquery-1.6.4.js', flags, (err, js, changed) ->
  115. throw err if err
  116. startTime = new Date
  117. snockets.getConcatenation 'jquery-1.6.4.js', flags, (err, js, changed) ->
  118. throw err if err
  119. test.ok !changed
  120. endTime = new Date
  121. test.ok endTime - startTime < 10
  122. test.done()
  123. 'getConcatenation returns correct minified JS code and srcmap': (test) ->
  124. staticRoot = path.resolve snockets.options.src
  125. target = path.resolve staticRoot, 'all.js'
  126. snockets.options.srcmap = true
  127. snockets.options.staticRoot = staticRoot
  128. snockets.options.target = target
  129. snockets.options.staticRootUrl = '/assets/'
  130. snockets.getConcatenation 'z.coffee', minify: true, (err, result) ->
  131. throw err if err
  132. { js, srcmap } = result
  133. test.equal js, """
  134. (function(){"Double rainbow\\nSO INTENSE"}).call(this);\n\n(function(){}).call(this);
  135. """
  136. test.equal srcmap.file, "/assets/all.js"
  137. test.equal srcmap.sources[0], "/assets/x.coffee"
  138. test.equal srcmap.sources[1], "/assets/z.coffee"
  139. test.equal srcmap.mappings, "AAAA;CAAA,CAAA,0BAAA;CAAA;ACGG;CAAA;CAAA"
  140. test.done()
  141. # Every test runs both synchronously and asynchronously.
  142. for name, func of testSuite
  143. do (func) ->
  144. exports[name] = (test) ->
  145. snockets.options.async = true; func(test)
  146. exports[name + ' (sync)'] = (test) ->
  147. snockets.options.async = false; func(test)