|
| 1 | +# frozen_string_literal: true |
| 2 | +require 'minitest/autorun' |
| 3 | +require 'sprockets' |
| 4 | +require 'terser' |
| 5 | + |
| 6 | +class TestTerserCompressor < Minitest::Test |
| 7 | + def setup |
| 8 | + @env = Sprockets::Environment.new |
| 9 | + @env.js_compressor = Terser.new |
| 10 | + @cache = Sprockets::Cache.new |
| 11 | + |
| 12 | + @tmpdir = Dir.mktmpdir |
| 13 | + @env.append_path(@tmpdir) |
| 14 | + end |
| 15 | + |
| 16 | + def teardown |
| 17 | + FileUtils.rm_rf(@tmpdir) if @tmpdir |
| 18 | + end |
| 19 | + |
| 20 | + def test_terser_configured_as_compressor |
| 21 | + assert_equal :js_compressor, @env.js_compressor |
| 22 | + end |
| 23 | + |
| 24 | + def test_compress_javascript_through_sprockets |
| 25 | + js_content = "function foo() {\n return true;\n}" |
| 26 | + file_path = File.join(@tmpdir, 'test.js') |
| 27 | + File.write(file_path, js_content) |
| 28 | + |
| 29 | + asset = @env['test.js'] |
| 30 | + compressed = asset.to_s |
| 31 | + |
| 32 | + assert compressed.length < js_content.length |
| 33 | + assert compressed.include?('function foo(){return!0}') |
| 34 | + refute compressed.include?("\n ") |
| 35 | + end |
| 36 | + |
| 37 | + def test_compress_es6_through_sprockets |
| 38 | + js_content = "const arrow = (x) => {\n return x * 2;\n}\nlet value = 10;" |
| 39 | + file_path = File.join(@tmpdir, 'test_es6.js') |
| 40 | + File.write(file_path, js_content) |
| 41 | + |
| 42 | + asset = @env['test_es6.js'] |
| 43 | + compressed = asset.to_s |
| 44 | + |
| 45 | + assert compressed.length < js_content.length |
| 46 | + assert compressed.include?('const arrow=a=>2*a') |
| 47 | + refute compressed.include?("\n ") |
| 48 | + end |
| 49 | + |
| 50 | + def test_compress_with_comments_through_sprockets |
| 51 | + js_content = <<~JS |
| 52 | + // This is a single line comment |
| 53 | + function calculate(a, b) { |
| 54 | + /* This is a |
| 55 | + multi-line comment */ |
| 56 | + return a + b; |
| 57 | + } |
| 58 | + JS |
| 59 | + |
| 60 | + file_path = File.join(@tmpdir, 'test_comments.js') |
| 61 | + File.write(file_path, js_content) |
| 62 | + |
| 63 | + asset = @env['test_comments.js'] |
| 64 | + compressed = asset.to_s |
| 65 | + |
| 66 | + assert compressed.length < js_content.length |
| 67 | + refute compressed.include?('This is a single line comment') |
| 68 | + refute compressed.include?('multi-line comment') |
| 69 | + assert compressed.include?('function calculate') |
| 70 | + end |
| 71 | + |
| 72 | + def test_preserve_license_comments_through_sprockets |
| 73 | + js_content = <<~JS |
| 74 | + /*! |
| 75 | + * Important License Information |
| 76 | + * Copyright (c) 2024 |
| 77 | + */ |
| 78 | + function licensed() { |
| 79 | + return "code"; |
| 80 | + } |
| 81 | + JS |
| 82 | + |
| 83 | + file_path = File.join(@tmpdir, 'test_license.js') |
| 84 | + File.write(file_path, js_content) |
| 85 | + |
| 86 | + asset = @env['test_license.js'] |
| 87 | + compressed = asset.to_s |
| 88 | + |
| 89 | + assert compressed.length < js_content.length |
| 90 | + assert compressed.include?('function licensed') |
| 91 | + end |
| 92 | + |
| 93 | + def test_multiple_files_compression |
| 94 | + file1_content = "var globalVar = 100;\nfunction first() { return globalVar; }" |
| 95 | + file2_content = "function second() { return globalVar * 2; }" |
| 96 | + |
| 97 | + File.write(File.join(@tmpdir, 'file1.js'), file1_content) |
| 98 | + File.write(File.join(@tmpdir, 'file2.js'), file2_content) |
| 99 | + |
| 100 | + asset1 = @env['file1.js'] |
| 101 | + asset2 = @env['file2.js'] |
| 102 | + |
| 103 | + compressed1 = asset1.to_s |
| 104 | + compressed2 = asset2.to_s |
| 105 | + |
| 106 | + assert compressed1.length < file1_content.length |
| 107 | + assert compressed2.length < file2_content.length |
| 108 | + |
| 109 | + assert compressed1.include?('globalVar') |
| 110 | + assert compressed2.include?('globalVar') |
| 111 | + end |
| 112 | + |
| 113 | + def test_compression_with_syntax_error |
| 114 | + js_content = 'function broken() { this is not valid javascript' |
| 115 | + file_path = File.join(@tmpdir, 'test_error.js') |
| 116 | + File.write(file_path, js_content) |
| 117 | + |
| 118 | + assert_raises(Terser::Error) do |
| 119 | + @env['test_error.js'].to_s |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments