Skip to content

Commit

Permalink
add Jest for TS unit tests
Browse files Browse the repository at this point in the history
@hgiesel the reason no files were being found is because Jest ignores
symlinks by default. The Bazel example includes a patch we can use
to work around it, and Jest plan to add symlink support in a future
update.

https://github.com/bazelbuild/rules_nodejs/blob/stable/examples/jest/patches/jest-haste-map%2B24.9.0.patch

jestjs/jest#9351
  • Loading branch information
dae committed Mar 28, 2021
1 parent 05ea624 commit c891f45
Show file tree
Hide file tree
Showing 8 changed files with 3,153 additions and 35 deletions.
2 changes: 2 additions & 0 deletions ts/BUILD.bazel
Expand Up @@ -23,6 +23,8 @@ exports_files([
"licenses.json",
"sql_format.ts",
"protobuf-shim.js",
"jest.config.js",
"package.json",
])

alias(
Expand Down
33 changes: 32 additions & 1 deletion ts/html-filter/BUILD.bazel
@@ -1,16 +1,47 @@
load("@npm//@bazel/typescript:index.bzl", "ts_library")
load("@npm//jest-cli:index.bzl", "jest_test")
load("//ts:prettier.bzl", "prettier_test")
load("//ts:eslint.bzl", "eslint_test")

ts_library(
name = "html-filter",
srcs = glob(["*.ts"]),
srcs = glob(
["*.ts"],
exclude = ["*.test.ts"],
),
module_name = "html-filter",
tsconfig = "//ts:tsconfig.json",
visibility = ["//visibility:public"],
deps = [],
)

ts_library(
name = "test_lib",
srcs = glob(["*.test.ts"]),
tsconfig = "//ts:tsconfig.json",
deps = [
"html-filter",
"@npm//@types/jest",
],
)

jest_test(
name = "test",
args = [
"--no-cache",
"--no-watchman",
"--ci",
"--colors",
"--config",
"$(location //ts:jest.config.js)",
],
data = [
":test_lib",
"//ts:jest.config.js",
"//ts:package.json",
],
)

# Tests
################

Expand Down
3 changes: 3 additions & 0 deletions ts/html-filter/index.test.ts
@@ -0,0 +1,3 @@
test("it should work", () => {
expect("test").toBe("test");
});
Empty file added ts/jest.config.js
Empty file.
6 changes: 5 additions & 1 deletion ts/package.json
Expand Up @@ -14,6 +14,7 @@
"@tsconfig/svelte": "^1.0.10",
"@types/d3": "^6.3.0",
"@types/diff": "^5.0.0",
"@types/jest": "^26.0.22",
"@types/jquery": "^3.5.0",
"@types/jqueryui": "^1.12.13",
"@types/lodash": "^4.14.162",
Expand All @@ -32,9 +33,11 @@
"espree": "^7.3.1",
"estraverse": "^5.2.0",
"glob": "^7.1.6",
"jest-cli": "^24.9",
"jsdoc": "^3.6.6",
"license-checker-rseidelsohn": "=1.1.2",
"minimist": "^1.2.5",
"patch-package": "^6.4.7",
"prettier": "^2.1.2",
"prettier-plugin-svelte": "^1.4.0",
"sass": "^1.32.6",
Expand All @@ -49,7 +52,8 @@
"uglify-js": "^3.13.1"
},
"scripts": {
"fix": "prettier --write */*.ts */*.svelte"
"fix": "prettier --write */*.ts */*.svelte",
"postinstall": "patch-package"
},
"dependencies": {
"@fluent/bundle": "^0.15.1",
Expand Down
16 changes: 16 additions & 0 deletions ts/patches/jest-haste-map+24.9.0.patch
@@ -0,0 +1,16 @@
diff --git a/node_modules/jest-haste-map/build/crawlers/node.js b/node_modules/jest-haste-map/build/crawlers/node.js
index 23985ae..a728a9a 100644
--- a/node_modules/jest-haste-map/build/crawlers/node.js
+++ b/node_modules/jest-haste-map/build/crawlers/node.js
@@ -166,7 +166,11 @@ function find(roots, extensions, ignore, callback) {

function findNative(roots, extensions, ignore, callback) {
const args = Array.from(roots);
+ args.push('(');
args.push('-type', 'f');
+ args.push('-o');
+ args.push('-type', 'l');
+ args.push(')');

if (extensions.length) {
args.push('(');
2 changes: 1 addition & 1 deletion ts/tsconfig.json
Expand Up @@ -26,7 +26,7 @@
"allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"jsx": "react",
"types": ["svelte", "long", "node"],
"types": ["svelte", "long", "node", "jest"],
"noEmitHelpers": true,
"importHelpers": true
}
Expand Down

0 comments on commit c891f45

Please sign in to comment.