Rework test runner and prerendering

Use doo for tests.
This commit is contained in:
Juho Teperi 2017-09-29 01:10:22 +03:00
parent 95026c77fd
commit fd93b09c96
11 changed files with 1052 additions and 135 deletions

View File

@ -88,9 +88,9 @@ push-project-docs: gen-site gen-docs
# build site into a gh-pages branch # build site into a gh-pages branch
build-gh-pages: gen-site gh-pages-add build-gh-pages: gen-site gh-pages-add
gen-site: clean gen-site: clean node_modules
lein with-profile prod cljsbuild once
lein with-profile prerender cljsbuild once lein with-profile prerender cljsbuild once
node pre-render/main.js
gen-docs: clean gen-docs: clean
lein codox lein codox

View File

@ -1,42 +0,0 @@
var fs = require("fs");
var vm = require("vm");
var path = require("path");
var run = function (src) {
global.require = require;
vm.runInThisContext(fs.readFileSync(src), src);
}
var imported = {};
var loadSrc = function (mainFile, outputDir, devFile) {
var googDir = path.join(outputDir, "goog");
var optNone = false;
if (outputDir) {
optNone = fs.existsSync(path.join(googDir, "deps.js"));
}
if (optNone) {
var cwd = process.cwd();
if (!global.goog) {
global.goog = {};
}
global.CLOSURE_IMPORT_SCRIPT = function (src) {
var s = path.resolve(path.resolve(cwd, path.join(googDir, src)));
if (!(s in imported)) {
imported[s] = true;
run(s);
return true;
}
};
run(path.join(googDir, "base.js"));
run(path.join(outputDir, "cljs_deps.js"));
run(path.join(outputDir, devFile));
} else {
run(mainFile);
}
return optNone;
};
exports.load = loadSrc;

View File

@ -1,40 +0,0 @@
var cljsLoad = require("./cljs-load");
var srcFile = "pre-render/main.js";
var outputDirectory = "outsite/public/js/out";
var devFile = "reagenttest/runtests.js";
var beep = "\u0007";
if (typeof location === "undefined") {
// figwheel wants js/location to exist, even if it doesn't run,
// for some reason
global.location = {};
}
var gensite = function () {
console.log("Pre-rendering or testing...");
var optNone = cljsLoad.load(srcFile, outputDirectory, devFile);
sitetools.server.genpages({"opt-none": optNone});
}
var compileFail = function () {
var msg = process.argv[process.argv.length - 1];
if (msg && msg.match(/failed/)) {
console.log("Compilation failed" + beep);
return true;
}
};
process.env.NODE_ENV = "production";
if (!compileFail()) {
try {
gensite();
} catch (e) {
console.log(e + beep);
console.error(e.stack);
process.exit(1);
}
}
process.exit(0);

View File

@ -1,17 +1,22 @@
machine: machine:
node: node:
version: v8.2.0 version: v8.2.0
dependencies:
override:
- lein deps
# Install only test dependencies for start
- npm install --only=dev
test: test:
override: override:
# Run tests against Cljsjs # Cljsjs - browser
- mv node_modules node_modules_disabled - lein with-profile test do clean, doo chrome client once
- lein with-profile dev do clean, cljsbuild once - test -f out/cljsjs/react/development/react.inc.js
- node bin/gen-site.js # Cljsjs - Production build - Browser
- test -f outsite/public/js/out/cljsjs/react/development/react.inc.js - lein with-profile prod-test do clean, doo chrome client once
- lein with-profile prod-test do clean, cljsbuild once # Node Modules - Browser
# Run tests against Node Modules - npm install
- mv node_modules_disabled - lein with-profile test do clean, doo chrome-headless client once
- lein clean - test -f out/node_modules/react/react.js
- lein with-profile dev do clean, cljsbuild once # Node Modules - Node Target
- node bin/gen-site.js - lein with-profile node-test do clean, doo node client once
- test -f outsite/public/js/out/node_modules/react/react.js - test -f out/node_modules/react/react.js

View File

@ -1,4 +0,0 @@
(ns reagentdemo.server
"Used to pre-render HTML files."
(:require [reagentdemo.core]
[sitetools.server]))

View File

@ -0,0 +1,18 @@
(ns sitetools.prerender
(:require [reagent.debug :refer-macros [log]]
[sitetools.core :as tools]
[sitetools.server :as server]
reagentdemo.core))
(defn -main [& args]
(log "Generating site")
(let [conf @tools/config
conf (assoc conf :timestamp (str "?" (js/Date.now)))
{:keys [site-dir pages]} conf]
(doseq [f (keys pages)]
(server/write-file (->> f tools/to-relative (server/path-join site-dir))
(server/gen-page f conf)))
(server/write-resources site-dir conf))
(log "Wrote site"))
(set! *main-cli-fn* -main)

View File

@ -57,6 +57,7 @@
($ (fs) mkdirSync d)))) ($ (fs) mkdirSync d))))
(defn write-file [f content] (defn write-file [f content]
(log "Write" f)
(mkdirs ($ (path) dirname f)) (mkdirs ($ (path) dirname f))
($ (fs) writeFileSync f content)) ($ (fs) writeFileSync f content))
@ -68,16 +69,3 @@
(->> css-infiles (->> css-infiles
(map #($ (fs) readFileSync %)) (map #($ (fs) readFileSync %))
(string/join "\n")))) (string/join "\n"))))
;;; Main entry points
(defn ^:export genpages [opts]
(log "Generating site")
(let [conf (swap! tools/config merge (js->clj opts :keywordize-keys true))
conf (assoc conf :timestamp (str "?" (js/Date.now)))
{:keys [site-dir pages]} conf]
(doseq [f (keys pages)]
(write-file (->> f tools/to-relative (path-join site-dir))
(gen-page f conf)))
(write-resources site-dir conf))
(log "Wrote site"))

997
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -2,14 +2,17 @@
"dependencies": { "dependencies": {
"@cljs-oss/module-deps": "1.1.1", "@cljs-oss/module-deps": "1.1.1",
"create-react-class": "^15.6.2", "create-react-class": "^15.6.2",
"react": "^15.6.1", "react": "^16.0.0",
"react-dom": "^15.6.1" "react-dom": "^16.0.0"
}, },
"scripts": { "scripts": {
"bundle": "webpack && NODE_ENV=production webpack -p" "bundle": "webpack && NODE_ENV=production webpack -p"
}, },
"devDependencies": { "devDependencies": {
"babel-core": "^5.8.25", "babel-core": "^5.8.25",
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
"karma-cljs-test": "^0.1.0",
"webpack": "^1.12.2" "webpack": "^1.12.2"
} }
} }

View File

@ -14,6 +14,7 @@
[cljsjs/create-react-class "15.6.2-0"]] [cljsjs/create-react-class "15.6.2-0"]]
:plugins [[lein-cljsbuild "1.1.7"] :plugins [[lein-cljsbuild "1.1.7"]
[lein-doo "0.1.7"]
[lein-codox "0.10.3"]] [lein-codox "0.10.3"]]
:source-paths ["src"] :source-paths ["src"]
@ -22,11 +23,12 @@
:exclude clojure.string :exclude clojure.string
:source-paths ["src"]} :source-paths ["src"]}
:profiles {:test {:cljsbuild :profiles {:node-test [:test {:cljsbuild
{:builds {:client {:compiler {:target :nodejs}}}}}]
:test {:cljsbuild
{:builds {:client {:source-paths ["test"] {:builds {:client {:source-paths ["test"]
:notify-command ["node" "bin/gen-site.js"] :compiler {:main "reagenttest.runtests"}}}}}
:compiler
{:main "reagenttest.runtests"}}}}}
:fig [{:dependencies [[figwheel "0.5.13"]] :fig [{:dependencies [[figwheel "0.5.13"]]
:plugins [[lein-figwheel "0.5.13"]] :plugins [[lein-figwheel "0.5.13"]]
@ -58,10 +60,11 @@
:prerender [:prod :prerender [:prod
{:cljsbuild {:cljsbuild
{:builds {:client {:builds {:client
{:compiler {:main "reagentdemo.server" {:compiler {:main "sitetools.prerender"
:target :nodejs
;; Undefine module and exports so React UMD modules work on Node
:output-to "pre-render/main.js" :output-to "pre-render/main.js"
:output-dir "pre-render/out"} :output-dir "pre-render/out"}}}}}]
:notify-command ["node" "bin/gen-site.js"] }}}}]
:webpack {:cljsbuild :webpack {:cljsbuild
{:builds {:client {:builds {:client
@ -101,10 +104,7 @@
:language-in :ecmascript6 :language-in :ecmascript6
:language-out :ecmascript3 :language-out :ecmascript3
;; Add process.env.NODE_ENV preload ;; Add process.env.NODE_ENV preload
:process-shim true :process-shim true}}}}
:npm-deps {:react "15.6.1"
:react-dom "15.6.1"
:create-react-class "15.6.2"}}}}}
:figwheel {:http-server-root "public" ;; assumes "resources" :figwheel {:http-server-root "public" ;; assumes "resources"
:repl false}) :repl false})

View File

@ -8,6 +8,7 @@
[reagenttest.testwithlet] [reagenttest.testwithlet]
[reagenttest.testwrap] [reagenttest.testwrap]
[cljs.test :as test :include-macros true] [cljs.test :as test :include-macros true]
[doo.runner :refer-macros [doo-tests]]
[reagent.core :as r] [reagent.core :as r]
[reagent.debug :refer-macros [dbg log]] [reagent.debug :refer-macros [dbg log]]
[reagentdemo.core :as demo] [reagentdemo.core :as demo]
@ -55,4 +56,11 @@
(reset! demo/test-results [#'test-output-mini]) (reset! demo/test-results [#'test-output-mini])
(run-tests))) (run-tests)))
(init!) (doo-tests 'reagenttest.testreagent
'reagenttest.testcursor
'reagenttest.testinterop
'reagenttest.testratom
'reagenttest.testratomasync
'reagenttest.testtrack
'reagenttest.testwithlet
'reagenttest.testwrap)