Skip to content
This repository has been archived by the owner on Jun 4, 2022. It is now read-only.

Commit

Permalink
Try to make sense of the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arichiardi committed Apr 28, 2018
1 parent 4303c59 commit 8059eaf
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 25 deletions.
10 changes: 10 additions & 0 deletions src/test/cljs/calculator.js
@@ -0,0 +1,10 @@
var calculator = {
add: function (a, b) {
return a + b;
},
subtract: function (a, b) {
return a - b;
}
};

module.exports = calculator;
3 changes: 3 additions & 0 deletions src/test/cljs/es6_default_hello.js
@@ -0,0 +1,3 @@
export default function sayHello () {
return "Hello, world!";
};
3 changes: 3 additions & 0 deletions src/test/cljs/es6_dep.js
@@ -0,0 +1,3 @@
import {default as calc} from './calculator';

export var calculator = calc;
144 changes: 129 additions & 15 deletions src/test/lumo/lumo/build_api_tests.cljs
Expand Up @@ -5,7 +5,7 @@
lumo.build-api-tests
(:require-macros [cljs.env.macros :as env]
[cljs.analyzer.macros :as ana])
(:require [clojure.test :as t :refer [deftest is testing async]]
(:require [clojure.test :as t :refer [deftest is testing async use-fixtures]]
[cljs.env :as env]
[cljs.analyzer :as ana]
[lumo.io :refer [spit slurp]]
Expand All @@ -15,6 +15,23 @@
fs
path))

(enable-console-print!)
(set! *print-newline* true)

(use-fixtures :once
;; backup and restore package.json cause we are executing these in the lumo
;; folder.
{:before (fn [] (fs/copyFileSync "package.json" "package.json.bak"))
:after (fn [] (fs/copyFileSync "package.json.bak" "package.json"))})

(use-fixtures :each
{:before (fn []
(test/delete-node-modules)
(spit "package.json" "{}"))
:after (fn []
(fs/unlinkSync "package.json")
(test/delete-node-modules))})

(deftest test-target-file-for-cljs-ns
(is (= (build/target-file-for-cljs-ns 'example.core-lib nil)
(test/platform-path "out/example/core_lib.js")))
Expand Down Expand Up @@ -143,12 +160,16 @@
;; (is (re-find #"Module B loaded" (slurp (-> modules :module-b :output-to))))))

(deftest cljs-1883-test-foreign-libs-use-relative-path
;; there might be a bug somewhere cause I keep getting "node_modules not
;; found" if I don't create it - Andrea Richiardi"
(fs/mkdirSync "node_modules")
(let [out (path/join (test/tmp-dir) "cljs-1883-out")
root (path/join "src" "test" "cljs_build")
opts {:foreign-libs
[{:file (str (path/join root "thirdparty" "add.js"))
:provides ["thirdparty.add"]}]
:output-dir (str out)
:main 'foreign-libs.core
:target :nodejs}]
(test/delete-out-files out)
(build/build (build/inputs (path/join root "foreign_libs") (path/join root "thirdparty")) opts)
Expand All @@ -160,6 +181,17 @@
"goog.require('thirdparty.add');")))))

(deftest cljs-1537-circular-deps
;; there might be a bug somewhere cause I keep getting "node_modules not
;; found" if I don't create it - Andrea Richiardi" the problem is that
;; `closure/index-node-modules` receives this in input, which is weird (first
;; line?):
;;
;; #object[Buffer [{"file":"/tmp/npm-deps-simple-test-out/cljs$node_modules.js","provides":["node_modules.js","node_modules"]}
;; ,{"file":"/home/arichiardi/git/lumo/node_modules/left-pad/index.js","provides":["left-pad","left-pad/index.js","left-pad/index"]}
;; ,{"file":"/home/arichiardi/git/lumo/node_modules/left-pad/package.json"}
;; ]]
;; so `closure/node-inputs` things that there is a node_modules module
(fs/mkdirSync "node_modules")
(let [out (path/join (test/tmp-dir) "cljs-1537-test-out")
root "src/test/cljs_build"]
(test/delete-out-files out)
Expand Down Expand Up @@ -222,9 +254,7 @@
;; (is (not (nil? (re-find #"foreignA[\s\S]+foreignB" (slurp (io/file out "foo.js"))))))))))

(deftest test-npm-deps-simple
(test/delete-node-modules)
(spit "package.json" "{}")
(let [out (path/join (test/tmp-dir) "npm-deps-test-out")
(let [out (path/join (test/tmp-dir) "npm-deps-simple-test-out")
{:keys [inputs opts]} {:inputs (path/join "src" "test" "cljs_build")
:opts {:main 'npm-deps-test.core
:output-dir out
Expand All @@ -242,10 +272,7 @@
(test/delete-out-files out)
(build/build (build/inputs (path/join inputs "npm_deps_test/core.cljs")) opts cenv)
(is (fs/existsSync (path/join out "node_modules/left-pad/index.js")))
(is (contains? (:js-module-index @cenv) "left-pad")))

(fs/unlinkSync "package.json")
(test/delete-node-modules))
(is (contains? (:js-module-index @cenv) "left-pad"))))

(deftest test-npm-deps
(let [cenv (env/default-compiler-env)
Expand All @@ -263,7 +290,6 @@
:non-standard-jsdoc :off}}}]
(test/delete-out-files out)
(testing "mix of symbol & string-based requires"
(test/delete-out-files out)
(test/delete-node-modules)
(build/build (build/inputs (path/join inputs "npm_deps_test/string_requires.cljs")) opts cenv)
(is (fs/existsSync (path/join out "node_modules/react/react.js")))
Expand All @@ -272,16 +298,16 @@

(testing "builds with string requires are idempotent"
(build/build (build/inputs (path/join inputs "npm_deps_test/string_requires.cljs")) opts cenv)
(is (not (nil? (re-find #"\.\.[\\/]node_modules[\\/]react-dom[\\/]server\.js" (slurp (path/join out "cljs_deps.js"))))))))
(test/delete-node-modules))
(is (not (nil? (re-find #"\.\.[\\/]node_modules[\\/]react-dom[\\/]server\.js" (slurp (path/join out "cljs_deps.js")))))))))

(deftest test-preloads
(let [out (path/join (test/tmp-dir) "preloads-test-out")
{:keys [inputs opts]} {:inputs (path/join "src" "test" "cljs")
:opts {:main 'preloads-test.core
:preloads '[preloads-test.preload]
:output-dir out
:optimizations :none}}
:optimizations :none
:closure-warnings {:check-types :off}}}
cenv (env/default-compiler-env)]
(test/delete-out-files out)
(build/build
Expand Down Expand Up @@ -356,8 +382,7 @@
(is (fs/existsSync (path/join out "emit_node_requires_test/native_modules.js")))
(is (true? (boolean (re-find #"emit_node_requires_test\.native_modules\.node\$module\$path\.isAbsolute"
(slurp (path/join out "emit_node_requires_test/native_modules.js"))))))
(is (empty? @ws))))
(test/delete-node-modules))
(is (empty? @ws)))))

(deftest cljs-test-compilation
(testing "success"
Expand Down Expand Up @@ -487,6 +512,7 @@
;; (let [out (io/file (test/tmp-dir) "cljs-2249-out")
;; root (io/file "src" "test" "cljs_build")
;; opts {:output-dir (str out)
;; :main 'foreign-libs-cljs-2249.core
;; :target :nodejs}]
;; (test/delete-out-files out)
;; (build/build (build/inputs (io/file root "foreign_libs_cljs_2249")) opts)
Expand Down Expand Up @@ -572,7 +598,7 @@
;; (is (re-find #"goog\.provide\(\"module\$[A-Za-z0-9$_]+?src\$test\$cljs_build\$foreign_libs_dir\$vendor\$lib\"\)"
;; (slurp (io/file out "src/test/cljs_build/foreign-libs-dir/vendor/lib.js"))))))

;; (deftest cljs-1883-test-foreign-libs-use-relative-path
;; (deftest cljs-2334-test-foreign-libs-that-are-modules
;; (test/delete-node-modules)
;; (let [out "cljs-2334-out"
;; root (io/file "src" "test" "cljs_build")
Expand All @@ -596,3 +622,91 @@
;; (is (re-find #"module\$.*\$node_modules\$left_pad\$index\[\"default\"\]\(42,5,0\)" (slurp foreign-lib-file))))
;; (test/delete-out-files out)
;; (test/delete-node-modules)))

;; TODO - when module splitting will be available to JS GCC - Andrea Richiardi
;; (deftest cljs-2519-test-cljs-base-entries
;; (let [dir (io/file "src" "test" "cljs_build" "code-split")
;; out (io/file (test/tmp-dir) "cljs-base-entries")
;; opts {:output-dir (str out)
;; :asset-path "/out"
;; :optimizations :none
;; :modules {:a {:entries '#{code.split.a}
;; :output-to (io/file out "a.js")}
;; :b {:entries '#{code.split.b}
;; :output-to (io/file out "b.js")}
;; :c {:entries '#{code.split.c}
;; :output-to (io/file out "c.js")}}}]
;; (test/delete-out-files out)
;; (build/build (build/inputs dir) opts)
;; (testing "Module :cljs-base"
;; (let [content (slurp (io/file out "cljs_base.js"))]
;; (testing "requires code.split.d (used in :b and :c)"
;; (is (test/document-write? content 'code.split.d)))))
;; (testing "Module :a"
;; (let [content (slurp (-> opts :modules :a :output-to))]
;; (testing "requires code.split.a"
;; (is (test/document-write? content 'code.split.a)))
;; (testing "requires cljs.pprint (only used in :a)"
;; (is (test/document-write? content 'cljs.pprint)))))
;; (testing "Module :b"
;; (let [content (slurp (-> opts :modules :b :output-to))]
;; (testing "requires code.split.b"
;; (is (test/document-write? content 'code.split.b)))))
;; (testing "Module :c"
;; (let [content (slurp (-> opts :modules :c :output-to))]
;; (testing "requires code.split.c"
;; (is (test/document-write? content 'code.split.c)))))))

;; TODO when --package_json_entry_names will be exposed in JS GCC - Andrea Richiardi
;; (deftest test-cljs-2592
;; (test/delete-node-modules)
;; (spit (io/file "package.json") "{}")
;; (let [cenv (env/default-compiler-env)
;; dir (io/file "src" "test" "cljs_build" "package_json_resolution_test")
;; out (io/file (test/tmp-dir) "package_json_resolution_test")
;; opts {:main 'package-json-resolution-test.core
;; :output-dir (str out)
;; :output-to (str (io/file out "main.js"))
;; :optimizations :none
;; :install-deps true
;; :npm-deps {:iterall "1.2.2"
;; :graphql "0.13.1"}
;; :package-json-resolution :nodejs
;; :closure-warnings {:check-types :off
;; :non-standard-jsdoc :off}}]
;; (test/delete-out-files out)
;; (build/build (build/inputs dir) opts cenv)
;; (testing "processes the iterall index.js"
;; (let [index-js (io/file out "node_modules/iterall/index.js")]
;; (is (.exists index-js))
;; (is (contains? (:js-module-index @cenv) "iterall"))
;; (is (re-find #"goog\.provide\(\"module\$.*\$node_modules\$iterall\$index\"\)" (slurp index-js)))))
;; (testing "processes the graphql index.js"
;; (let [index-js (io/file out "node_modules/graphql/index.js")
;; execution-index-js (io/file out "node_modules/graphql/execution/index.js")
;; ast-from-value-js (io/file out "node_modules/grapqhl/utilities/astFromValue.js")]
;; (is (.exists index-js))
;; (is (contains? (:js-module-index @cenv) "graphql"))
;; (is (re-find #"goog\.provide\(\"module\$.*\$node_modules\$graphql\$index\"\)" (slurp index-js)))))
;; (testing "processes a nested index.js in graphql"
;; (let [nested-index-js (io/file out "node_modules/graphql/execution/index.js")]
;; (is (.exists nested-index-js))
;; (is (contains? (:js-module-index @cenv) "graphql/execution"))
;; (is (re-find #"goog\.provide\(\"module\$.*\$node_modules\$graphql\$execution\$index\"\)" (slurp nested-index-js)))))
;; (testing "processes cross-package imports"
;; (let [ast-from-value-js (io/file out "node_modules/graphql/utilities/astFromValue.js")]
;; (is (.exists ast-from-value-js))
;; (is (re-find #"goog.require\(\"module\$.*\$node_modules\$iterall\$index\"\);" (slurp ast-from-value-js)))))
;; (testing "adds dependencies to cljs_deps.js"
;; (let [deps-js (io/file out "cljs_deps.js")]
;; (is (re-find #"goog\.addDependency\(\"..\/node_modules\/iterall\/index.js\"" (slurp deps-js)))
;; (is (re-find #"goog\.addDependency\(\"..\/node_modules\/graphql\/index.js\"" (slurp deps-js)))
;; (is (re-find #"goog\.addDependency\(\"..\/node_modules\/graphql\/execution/index.js\"" (slurp deps-js)))))
;; (testing "adds the right module names to the core.cljs build output"
;; (let [core-js (io/file out "package_json_resolution_test/core.js")]
;; (is (re-find #"goog\.require\('module\$.*\$node_modules\$iterall\$index'\);" (slurp core-js)))
;; (is (re-find #"module\$.+\$node_modules\$iterall\$index\[\"default\"\]\.isCollection" (slurp core-js)))
;; (is (re-find #"goog\.require\('module\$.*\$node_modules\$graphql\$index'\);" (slurp core-js)))
;; (is (re-find #"module\$.+\$node_modules\$graphql\$index\[\"default\"\]" (slurp core-js))))))
;; (.delete (io/file "package.json"))
;; (test/delete-node-modules))
84 changes: 79 additions & 5 deletions src/test/lumo/lumo/closure_tests.cljs
Expand Up @@ -3,7 +3,7 @@
- you transform .getAbsolutePath to path/resolve
- you transform .delete to fs/unlinkSync"}
lumo.closure-tests
(:require [clojure.test :as t :refer [deftest is testing]]
(:require [clojure.test :as t :refer [deftest is testing use-fixtures]]
[lumo.build.api :as build]
[lumo.closure :as closure]
[lumo.cljs-deps :as deps]
Expand All @@ -15,6 +15,18 @@
fs
path))

(use-fixtures :once
;; backup and restore package.json cause we are executing these in the lumo
;; folder
{:before (fn [] (fs/copyFileSync "package.json" "package.json.bak"))
:after (fn [] (fs/copyFileSync "package.json.bak" "package.json"))})

(use-fixtures :each
{:before (fn []
;; cleanup node_modules
(test/delete-node-modules)
(spit "package.json" "{}"))})

(deftest test-make-preamble
(testing "no options"
(is (= "" (closure/make-preamble {}))))
Expand Down Expand Up @@ -86,8 +98,6 @@
(is (= (closure/lib-rel-path ijs) "tabby.js"))))

(deftest test-index-node-modules
(test/delete-node-modules)
(spit "package.json" "{}")
(closure/maybe-install-node-deps! {:npm-deps {:left-pad "1.1.3"}})
(let [modules (closure/index-node-modules-dir)]
(is (true? (some (fn [module]
Expand Down Expand Up @@ -159,10 +169,8 @@
(test/delete-node-modules))

(deftest test-index-node-modules-module-deps-js
(spit "package.json" "{}")
(let [opts {:npm-deps {:left-pad "1.1.3"}}
out (util/output-directory opts)]
(test/delete-node-modules)
(test/delete-out-files out)
(closure/maybe-install-node-deps! opts)
(is (true? (some (fn [module]
Expand Down Expand Up @@ -394,6 +402,72 @@
(test/delete-node-modules)
(test/delete-out-files out)))

(deftest test-cljs-2580
(let [opts {:npm-deps {"npm-package-with-main-entry-pointing-to-folder" "1.0.0"}
:target :nodejs}
out (util/output-directory opts)]
(test/delete-node-modules)
(test/delete-out-files out)
(closure/maybe-install-node-deps! opts)
(let [modules (closure/index-node-modules-dir)]
(is (true? (some (fn [module]
(= module
{:file (path/resolve "node_modules/npm-package-with-main-entry-pointing-to-folder/folder/index.js")
:module-type :es6
:provides ["npm-package-with-main-entry-pointing-to-folder/folder/index.js"
"npm-package-with-main-entry-pointing-to-folder/folder/index"
"npm-package-with-main-entry-pointing-to-folder"
"npm-package-with-main-entry-pointing-to-folder/folder"]}))
modules))))
(let [modules (closure/index-node-modules ["npm-package-with-main-entry-pointing-to-folder"] opts)]
(is (true? (some (fn [module]
(= module {:module-type :es6
:file (path/resolve "node_modules/npm-package-with-main-entry-pointing-to-folder/folder/index.js")
:provides ["npm-package-with-main-entry-pointing-to-folder"
"npm-package-with-main-entry-pointing-to-folder/folder/index.js"
"npm-package-with-main-entry-pointing-to-folder/folder/index"
"npm-package-with-main-entry-pointing-to-folder/folder"]}))
modules))))
(fs/unlinkSync "package.json")
(test/delete-node-modules)
(test/delete-out-files out)))

;; TODO when --package_json_entry_names will be exposed in JS GCC - Andrea Richiardi
;; (deftest test-cljs-2592
;; (spit (io/file "package.json") "{}")
;; (let [opts {:npm-deps {:iterall "1.2.2"
;; :graphql "0.13.1"}
;; :package-json-resolution :nodejs}
;; out (util/output-directory opts)]
;; (test/delete-node-modules)
;; (test/delete-out-files out)
;; (closure/maybe-install-node-deps! opts)
;; (let [modules (closure/index-node-modules ["iterall" "graphql"] opts)]
;; (is (true? (some (fn [module]
;; (= module {:module-type :es6
;; :file (.getAbsolutePath (io/file "node_modules/iterall/index.js"))
;; :provides ["iterall"
;; "iterall/index.js"
;; "iterall/index"]}))
;; modules)))
;; (is (true? (some (fn [module]
;; (= module {:module-type :es6
;; :file (.getAbsolutePath (io/file "node_modules/graphql/index.js"))
;; :provides ["graphql"
;; "graphql/index.js"
;; "graphql/index"]}))
;; modules)))
;; (is (true? (some (fn [module]
;; (= module {:module-type :es6
;; :file (.getAbsolutePath (io/file "node_modules/graphql/execution/index.js"))
;; :provides ["graphql/execution/index.js"
;; "graphql/execution/index"
;; "graphql/execution"]}))
;; modules))))
;; (.delete (io/file "package.json"))
;; (test/delete-node-modules)
;; (test/delete-out-files out)))

(deftest test-no-package-json
(spit "package.json" "{}")
(let [opts {:npm-deps {:left-pad "1.1.3"}}
Expand Down
10 changes: 5 additions & 5 deletions src/test/lumo/lumo/test_runner.cljs
Expand Up @@ -8,8 +8,8 @@

(defn run-all-tests []
(run-tests
'lumo.repl-tests
'lumo.js-deps-tests
'lumo.closure-tests
'lumo.build-api-tests
'lumo.util-tests))
;; 'lumo.repl-tests
;; 'lumo.js-deps-tests
;; 'lumo.closure-tests
'lumo.build-api-tests
#_'lumo.util-tests))

0 comments on commit 8059eaf

Please sign in to comment.