Skip to content

Commit

Permalink
DEV: Support for running theme test with Ember CLI
Browse files Browse the repository at this point in the history
This is quite complex as it means that in production we have to build
Ember CLI test files and allow them to be used by our Rails application.

There is a fair bit of glue we can remove in the future once we move to
Ember CLI completely.
  • Loading branch information
eviltrout committed Jan 11, 2022
1 parent 4afe47f commit ea84a82
Show file tree
Hide file tree
Showing 26 changed files with 227 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ lib/highlight_js/
plugins/**/lib/javascripts/locale
public/
vendor/
app/assets/javascripts/discourse/tests/test_helper.js
app/assets/javascripts/discourse/tests/test-boot-rails.js
app/assets/javascripts/discourse/tests/fixtures
node_modules/
dist/
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ lib/highlight_js/
plugins/**/lib/javascripts/locale
public/
vendor/
app/assets/javascripts/discourse/tests/test_helper.js
app/assets/javascripts/discourse/tests/test-boot-rails.js
app/assets/javascripts/discourse/tests/fixtures
node_modules/
dist/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ let testingFunc = isLegacyEmber() ? run : next;

export default function () {
if (isTesting()) {
return testingFunc(...arguments);
// Don't include the time argument (in ms)
let args = [].slice.call(arguments, 0, -1);
return testingFunc.apply(void 0, args);
} else {
return debounce(...arguments);
}
Expand Down
58 changes: 58 additions & 0 deletions app/assets/javascripts/discourse/ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,66 @@ module.exports = function (defaults) {
// We don't use SRI in Rails. Disable here to match:
enabled: false,
},

"ember-cli-terser": {
enabled: true,
exclude: [
"**/test-*.js",
"**/core-tests*.js",
"**/highlightjs/*",
"**/javascripts/*",
],
},

// We need to build tests in prod for theme tests
tests: true,
});

// Patching a private method is not great, but there's no other way for us to tell
// Ember CLI that we want the tests alone in a package without helpers/fixtures, since
// we re-use those in the theme tests.
app._defaultPackager.packageApplicationTests = function (tree) {
let appTestTrees = []
.concat(
this.packageEmberCliInternalFiles(),
this.packageTestApplicationConfig(),
tree
)
.filter(Boolean);

appTestTrees = mergeTrees(appTestTrees, {
overwrite: true,
annotation: "TreeMerger (appTestTrees)",
});

let tests = concat(appTestTrees, {
inputFiles: [
"**/tests/acceptance/*.js",
"**/tests/integration/*.js",
"**tests/unit/*.js",
],
headerFiles: ["vendor/ember-cli/tests-prefix.js"],
footerFiles: ["vendor/ember-cli/app-config.js"],
outputFile: "/assets/core-tests.js",
annotation: "Concat: Core Tests",
sourceMapConfig: false,
});

let testHelpers = concat(appTestTrees, {
inputFiles: [
"**/tests/test-boot-ember-cli.js",
"**/tests/helpers/**/*.js",
"**/tests/fixtures/**/*.js",
"**/tests/setup-tests.js",
],
outputFile: "/assets/test-helpers.js",
annotation: "Concat: Test Helpers",
sourceMapConfig: false,
});

return mergeTrees([tests, testHelpers]);
};

// WARNING: We should only import scripts here if they are not in NPM.
// For example: our very specific version of bootstrap-modal.
app.import(vendorJs + "bootbox.js");
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/discourse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"discourse-common": "^1.0.0",
"discourse-hbr": "^1.0.0",
"discourse-widget-hbs": "^1.0.0",
"ember-auto-import": "^1.10.1",
"ember-auto-import": "^1.12.0",
"ember-buffered-proxy": "^2.0.0-beta.0",
"ember-cli": "~3.25.3",
"ember-cli-app-version": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
document.write(
"<style>#ember-testing-container { position: fixed; background: white; bottom: 0; right: 0; width: 640px; height: 384px; overflow: auto; z-index: 9999; border: 1px solid #ccc; transform: translateZ(0)} #ember-testing { width: 200%; height: 200%; transform: scale(0.5); transform-origin: top left; }</style>"
);
require('discourse/tests/test-boot-ember-cli');
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,4 @@
require_asset(f)
end
end

Discourse.plugins.each do |p|
root_path = "#{File.dirname(p.path)}/test/javascripts"

to_glob = [root_path + '/**/**.es6']
to_glob << (root_path + '/**/**.js') if p.transpile_js

Dir.glob(to_glob) { |f| require_asset(f) }
end
%>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//= require_tree ./acceptance
//= require_tree ./integration
//= require_tree ./unit
//= require ./plugin_tests
9 changes: 7 additions & 2 deletions app/assets/javascripts/discourse/tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@
<script src="{{rootURL}}assets/test-support.js"></script>
<script src="{{rootURL}}assets/discourse.js"></script>
<script src="{{rootURL}}assets/discourse-markdown.js"></script>
<script src="{{rootURL}}assets/discourse/tests/core_plugins_tests.js"></script>
<script src="{{rootURL}}assets/discourse/tests/active-plugins.js"></script>
<script src="{{rootURL}}assets/admin.js"></script>
<script src="{{rootURL}}assets/tests.js"></script>
<script src="{{rootURL}}assets/test-helpers.js"></script>
<script src="{{rootURL}}assets/core-tests.js"></script>
<script src="{{rootURL}}assets/discourse/tests/plugin-tests.js"></script>
<script>
require('discourse/tests/test-boot-ember-cli');
</script>
<script src="{{rootURL}}assets/scripts/discourse-boot.js"></script>

{{content-for "body-footer"}}
Expand Down
10 changes: 10 additions & 0 deletions app/assets/javascripts/discourse/tests/plugin-tests.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%
Discourse.plugins.each do |p|
root_path = "#{File.dirname(p.path)}/test/javascripts"

to_glob = [root_path + '/**/**.es6']
to_glob << (root_path + '/**/**.js') if p.transpile_js

Dir.glob(to_glob) { |f| require_asset(f) }
end
%>
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ document.addEventListener("discourse-booted", () => {
setupEmberOnerrorValidation: !skippingCore,
});
});
window.EmberENV.TESTS_FILE_LOADED = true;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
//= require_tree ./helpers
//= require_tree ./fixtures
//= require ./setup-tests
//= require test-shims
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
//= require fake_xml_http_request
//= require route-recognizer
//= require pretender

// These are not loaded in prod or development
// But we need them for testing handlebars templates in qunit
//= require handlebars
//= require ember-template-compiler

//= require sinon
//= require break_string
//= require test-shims
//= require jquery.magnific-popup.min.js
//= require handlebars
//= require ember-template-compiler
//= require markdown-it-bundle
38 changes: 0 additions & 38 deletions app/assets/javascripts/discourse/tests/test_helper.js

This file was deleted.

30 changes: 0 additions & 30 deletions app/assets/javascripts/discourse/tests/theme_qunit_vendor.js

This file was deleted.

35 changes: 35 additions & 0 deletions app/assets/javascripts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4810,6 +4810,41 @@ ember-auto-import@^1.10.1, ember-auto-import@^1.5.3:
walk-sync "^0.3.3"
webpack "^4.43.0"

ember-auto-import@^1.12.0:
version "1.12.0"
resolved "https://registry.yarnpkg.com/ember-auto-import/-/ember-auto-import-1.12.0.tgz#52246b04891090e2608244e65c4c6af7710df12b"
integrity sha512-fzMGnyHGfUNFHchpLbJ98Vs/c5H2wZBMR9r/XwW+WOWPisZDGLUPPyhJQsSREPoUQ+o8GvyLaD/rkrKqW8bmgw==
dependencies:
"@babel/core" "^7.1.6"
"@babel/preset-env" "^7.10.2"
"@babel/traverse" "^7.1.6"
"@babel/types" "^7.1.6"
"@embroider/core" "^0.33.0"
babel-core "^6.26.3"
babel-loader "^8.0.6"
babel-plugin-syntax-dynamic-import "^6.18.0"
babylon "^6.18.0"
broccoli-debug "^0.6.4"
broccoli-node-api "^1.7.0"
broccoli-plugin "^4.0.0"
broccoli-source "^3.0.0"
debug "^3.1.0"
ember-cli-babel "^7.0.0"
enhanced-resolve "^4.0.0"
fs-extra "^6.0.1"
fs-tree-diff "^2.0.0"
handlebars "^4.3.1"
js-string-escape "^1.0.1"
lodash "^4.17.19"
mkdirp "^0.5.1"
resolve-package-path "^3.1.0"
rimraf "^2.6.2"
semver "^7.3.4"
symlink-or-copy "^1.2.0"
typescript-memoize "^1.0.0-alpha.3"
walk-sync "^0.3.3"
webpack "^4.43.0"

ember-buffered-proxy@^2.0.0-beta.0:
version "2.0.0-beta.0"
resolved "https://registry.yarnpkg.com/ember-buffered-proxy/-/ember-buffered-proxy-2.0.0-beta.0.tgz#65be4e2d0dcf40a5a2dab548c84a21aa332555a2"
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/bootstrap_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def index
locale_script: locale,
stylesheets: @stylesheets,
plugin_js: plugin_js,
plugin_test_js: [script_asset_path("plugin_tests")],
plugin_test_js: [script_asset_path("plugin-tests")],
setup_data: client_side_setup_data,
preloaded: @preloaded,
html: create_html,
Expand Down
15 changes: 14 additions & 1 deletion app/controllers/qunit_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,28 @@ class QunitController < ApplicationController
}
layout false

def is_ember_cli_proxy?
request.headers["HTTP_X_DISCOURSE_EMBER_CLI"] == "true"
end

# only used in test / dev
def index
raise Discourse::NotFound.new if request.headers["HTTP_X_DISCOURSE_EMBER_CLI"] == "true"
raise Discourse::NotFound.new if is_ember_cli_proxy?
raise Discourse::InvalidAccess.new if Rails.env.production?
end

def theme
raise Discourse::NotFound.new if !can_see_theme_qunit?

@is_proxied = is_ember_cli_proxy?
@legacy_ember = Rails.env.development? && !@is_proxied

# In production mode all bundles use `application`
@app_bundle = "application"
if Rails.env.development? && @is_proxied
@app_bundle = "discourse"
end

param_key = nil
@suggested_themes = nil
if (id = get_param(:id)).present?
Expand Down
17 changes: 14 additions & 3 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ def self.extra_body_classes
@extra_body_classes ||= Set.new
end

def discourse_config_environment
def discourse_config_environment(testing: false)

# TODO: Can this come from Ember CLI somehow?
{ modulePrefix: "discourse",
config = {
modulePrefix: "discourse",
environment: Rails.env,
rootURL: Discourse.base_path,
locationType: "auto",
Expand All @@ -32,7 +34,16 @@ def discourse_config_environment
version: "#{Discourse::VERSION::STRING} #{Discourse.git_version}",
exportApplicationGlobal: true
}
}.to_json
}

if testing
config[:environment] = "test"
config[:locationType] = "none"
config[:APP][:autoboot] = false
config[:APP][:rootElement] = '#ember-testing'
end

config.to_json
end

def google_universal_analytics_json(ua_domain_name = nil)
Expand Down
Loading

0 comments on commit ea84a82

Please sign in to comment.