Skip to content

Commit

Permalink
feat(examples): add Jest example
Browse files Browse the repository at this point in the history
This PR adds an example of how you might create a Jest rule

Unfortunately Jest does not handle symlinks very well so we have to add some patches so that it behaves correctly(See: jasongwartz/bazel_rules_nodejs_contrib#4 (comment))
  • Loading branch information
purkhusid committed Oct 15, 2019
1 parent 9d84407 commit c6d166b
Show file tree
Hide file tree
Showing 23 changed files with 5,244 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/jest/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions examples/jest/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import %workspace%/../../common.bazelrc
24 changes: 24 additions & 0 deletions examples/jest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load(":jest.bzl", "jest_test")

jest_test(
name = "test",
srcs = [
"index.test.js",
"index2.test.js",
],
jest_config = ":jest.config.js",
deps = [
":extra.js",
":index.js",
"@npm//@babel/core",
"@npm//@babel/preset-env",
"@npm//@jest/transform",
"@npm//babel-jest",
],
)

# Just a dummy test so that we have a test target for //... on certain bazelci platforms with bazel_integration_test
sh_test(
name = "dummy_test",
srcs = ["dummy_test.sh"],
)
3 changes: 3 additions & 0 deletions examples/jest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Jest example

This example shows how you might use the Jest testing framework
48 changes: 48 additions & 0 deletions examples/jest/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright 2019 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(
name = "examples_jest",
managed_directories = {"@npm": ["node_modules"]},
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "ad4be2c6f40f5af70c7edf294955f9d9a0222c8e2756109731b25f79ea2ccea0",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.38.3/rules_nodejs-0.38.3.tar.gz"],
)

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
name = "npm",
data = [
"//:tools/patches/@jest+core+24.7.1.patch",
"//:tools/patches/@jest+transform+24.7.1.patch",
"//:tools/patches/jest-cli+24.7.1.patch",
"//:tools/patches/jest-config+24.7.1.patch",
"//:tools/patches/jest-haste-map+24.7.1.patch",
"//:tools/patches/jest-resolve+24.7.1.patch",
"//:tools/patches/jest-runtime+24.7.1.patch",
],
package_json = "//:package.json",
quiet = False,
yarn_lock = "//:yarn.lock",
)

load("@npm//:install_bazel_dependencies.bzl", "install_bazel_dependencies")

install_bazel_dependencies()
12 changes: 12 additions & 0 deletions examples/jest/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
2 changes: 2 additions & 0 deletions examples/jest/dummy_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
echo "Just a dummy test so that we have a test target for //... on certain bazelci platforms with bazel_integration_test"
exit 0
1 change: 1 addition & 0 deletions examples/jest/extra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'test';
3 changes: 3 additions & 0 deletions examples/jest/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import extra from './extra';

export default extra;
5 changes: 5 additions & 0 deletions examples/jest/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import indexfile from './index.js';

test('it should work', () => {
expect(indexfile).toBe('test');
});
5 changes: 5 additions & 0 deletions examples/jest/index2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import indexfile from './index.js';

test('it should work', () => {
expect(indexfile).toBe('test');
});
25 changes: 25 additions & 0 deletions examples/jest/jest.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"Shows how you might create a macro for the autogeneratd Jest rule"

load("@npm//jest-cli:index.bzl", _jest_test = "jest_test")

def jest_test(name, srcs, deps, jest_config):
"A macro around the autogenerated jest_test rule"
jest_config_location_arg = "$(location " + jest_config + ")"
jest_test_path_arg = ["--runTestsByPath"]
for src in srcs:
jest_test_path_arg = jest_test_path_arg + ["$(location " + src + ")"]

args = [
"--config",
jest_config_location_arg,
"--no-cache",
"--no-watchman",
"--ci",
"--runTestsByPath",
] + jest_test_path_arg

_jest_test(
name = name,
data = [jest_config] + srcs + deps,
args = args,
)
6 changes: 6 additions & 0 deletions examples/jest/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
testEnvironment: 'node',

transform: {'^.+\\.jsx?$': 'babel-jest'},
testMatch: ['**/*.test.js']
};
21 changes: 21 additions & 0 deletions examples/jest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "jest_example",
"private": true,
"devDependencies": {
"@babel/cli": "^7.6.0",
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"@jest/transform": "24.7.1",
"babel-jest": "24.7.1",
"jest-cli": "24.7.1",
"jest-config": "24.7.1",
"jest-haste-map": "24.7.1",
"jest-resolve": "24.7.1",
"jest-runtime": "24.7.1",
"patch-package": "6.1.2"
},
"scripts": {
"test": "bazel test ...",
"postinstall": "patch-package --patch-dir=./tools/patches"
}
}
Empty file added examples/jest/tools/BUILD.bazel
Empty file.
13 changes: 13 additions & 0 deletions examples/jest/tools/patches/@jest+core+24.7.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/node_modules/@jest/core/build/runJest.js b/node_modules/@jest/core/build/runJest.js
index 5afb90a..9a2c15b 100644
--- a/node_modules/@jest/core/build/runJest.js
+++ b/node_modules/@jest/core/build/runJest.js
@@ -244,7 +244,7 @@ const processResults = (runResults, options) => {

if (isJSON) {
if (outputFile) {
- const cwd = (0, _realpathNative().sync)(process.cwd());
+ const cwd = process.cwd();

const filePath = _path().default.resolve(cwd, outputFile);

13 changes: 13 additions & 0 deletions examples/jest/tools/patches/@jest+transform+24.7.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/node_modules/@jest/transform/build/ScriptTransformer.js b/node_modules/@jest/transform/build/ScriptTransformer.js
index 81397c7..7589de2 100644
--- a/node_modules/@jest/transform/build/ScriptTransformer.js
+++ b/node_modules/@jest/transform/build/ScriptTransformer.js
@@ -328,7 +328,7 @@ class ScriptTransformer {
}

transformSource(filepath, content, instrument) {
- const filename = this._getRealPath(filepath);
+ const filename = filepath;

const transform = this._getTransformer(filename);

26 changes: 26 additions & 0 deletions examples/jest/tools/patches/jest-cli+24.7.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/node_modules/jest-cli/build/cli/index.js b/node_modules/jest-cli/build/cli/index.js
index 831a476..46f7570 100644
--- a/node_modules/jest-cli/build/cli/index.js
+++ b/node_modules/jest-cli/build/cli/index.js
@@ -271,7 +271,7 @@ const getProjectListFromCLIArgs = (argv, project) => {

if (!projects.length && process.platform === 'win32') {
try {
- projects.push((0, _realpathNative().sync)(process.cwd()));
+ projects.push(process.cwd());
} catch (err) {
// do nothing, just catch error
// process.binding('fs').realpath can throw, e.g. on mapped drives
diff --git a/node_modules/jest-cli/build/init/index.js b/node_modules/jest-cli/build/init/index.js
index cae2fc2..7f38281 100644
--- a/node_modules/jest-cli/build/init/index.js
+++ b/node_modules/jest-cli/build/init/index.js
@@ -134,7 +134,7 @@ var _default =
/*#__PURE__*/
(function() {
var _ref = _asyncToGenerator(function*(
- rootDir = (0, _realpathNative().sync)(process.cwd())
+ rootDir = process.cwd()
) {
// prerequisite checks
const projectPackageJsonPath = _path().default.join(
35 changes: 35 additions & 0 deletions examples/jest/tools/patches/jest-config+24.7.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
diff --git a/node_modules/jest-config/build/getCacheDirectory.js b/node_modules/jest-config/build/getCacheDirectory.js
index 7e64ba6..6ea2cfd 100644
--- a/node_modules/jest-config/build/getCacheDirectory.js
+++ b/node_modules/jest-config/build/getCacheDirectory.js
@@ -50,7 +50,7 @@ const getCacheDirectory = () => {
getuid = _process.getuid;

const tmpdir = _path().default.join(
- (0, _realpathNative().sync)(_os().default.tmpdir()),
+ _os().default.tmpdir(),
'jest'
);

diff --git a/node_modules/jest-config/build/normalize.js b/node_modules/jest-config/build/normalize.js
index ce37c38..c34497a 100644
--- a/node_modules/jest-config/build/normalize.js
+++ b/node_modules/jest-config/build/normalize.js
@@ -428,7 +428,7 @@ const normalizeRootDir = options => {

try {
// try to resolve windows short paths, ignoring errors (permission errors, mostly)
- options.rootDir = (0, _realpathNative().sync)(options.rootDir);
+ options.rootDir = options.rootDir;
} catch (e) {
// ignored
}
@@ -1004,7 +1004,7 @@ function normalize(options, argv, configPath, projectIndex = Infinity) {

try {
// try to resolve windows short paths, ignoring errors (permission errors, mostly)
- newOptions.cwd = (0, _realpathNative().sync)(process.cwd());
+ newOptions.cwd = process.cwd();
} catch (e) {
// ignored
}
26 changes: 26 additions & 0 deletions examples/jest/tools/patches/jest-haste-map+24.7.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
diff --git a/node_modules/jest-haste-map/build/crawlers/node.js b/node_modules/jest-haste-map/build/crawlers/node.js
index a179225..06106b9 100644
--- a/node_modules/jest-haste-map/build/crawlers/node.js
+++ b/node_modules/jest-haste-map/build/crawlers/node.js
@@ -165,7 +165,7 @@ function find(roots, extensions, ignore, callback) {
}

function findNative(roots, extensions, ignore, callback) {
- const args = Array.from(roots);
+ const args = Array.from(['-L'].concat(roots));
args.push('-type', 'f');

if (extensions.length) {
diff --git a/node_modules/jest-haste-map/build/index.js b/node_modules/jest-haste-map/build/index.js
index eb8d84f..97ec2c1 100644
--- a/node_modules/jest-haste-map/build/index.js
+++ b/node_modules/jest-haste-map/build/index.js
@@ -403,7 +403,7 @@ class HasteMap extends _events().default {
roots: Array.from(new Set(options.roots)),
skipPackageJson: !!options.skipPackageJson,
throwOnModuleCollision: !!options.throwOnModuleCollision,
- useWatchman: options.useWatchman == null ? true : options.useWatchman,
+ useWatchman: false,
watch: !!options.watch
};
this._console = options.console || global.console;
46 changes: 46 additions & 0 deletions examples/jest/tools/patches/jest-resolve+24.7.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
diff --git a/node_modules/jest-resolve/build/defaultResolver.js b/node_modules/jest-resolve/build/defaultResolver.js
index 64e7387..b5b74d3 100644
--- a/node_modules/jest-resolve/build/defaultResolver.js
+++ b/node_modules/jest-resolve/build/defaultResolver.js
@@ -137,7 +137,7 @@ function resolveSync(target, options) {
if (result) {
// Dereference symlinks to ensure we don't create a separate
// module instance depending on how it was referenced.
- result = _fs().default.realpathSync(result);
+ // result = _fs().default.realpathSync(result);
}

return result;
diff --git a/node_modules/jest-resolve/build/index.js b/node_modules/jest-resolve/build/index.js
index 8e6bfe0..c3a9f27 100644
--- a/node_modules/jest-resolve/build/index.js
+++ b/node_modules/jest-resolve/build/index.js
@@ -57,7 +57,7 @@ function _defineProperty(obj, key, value) {
const NATIVE_PLATFORM = 'native'; // We might be inside a symlink.

const cwd = process.cwd();
-const resolvedCwd = (0, _realpathNative().sync)(cwd) || cwd;
+const resolvedCwd = cwd;
const nodePaths = process.env.NODE_PATH
? process.env.NODE_PATH.split(_path().default.delimiter)
.filter(Boolean) // The resolver expects absolute paths.
diff --git a/node_modules/jest-resolve/build/nodeModulesPaths.js b/node_modules/jest-resolve/build/nodeModulesPaths.js
index 8d9d10f..5d55456 100644
--- a/node_modules/jest-resolve/build/nodeModulesPaths.js
+++ b/node_modules/jest-resolve/build/nodeModulesPaths.js
@@ -55,14 +55,7 @@ function nodeModulesPaths(basedir, options) {
} // The node resolution algorithm (as implemented by NodeJS and TypeScript)
// traverses parents of the physical path, not the symlinked path

- let physicalBasedir;
-
- try {
- physicalBasedir = (0, _realpathNative().sync)(basedirAbs);
- } catch (err) {
- // realpath can throw, e.g. on mapped drives
- physicalBasedir = basedirAbs;
- }
+ let physicalBasedir = basedirAbs;

const paths = [physicalBasedir];

13 changes: 13 additions & 0 deletions examples/jest/tools/patches/jest-runtime+24.7.1.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/node_modules/jest-runtime/build/cli/index.js b/node_modules/jest-runtime/build/cli/index.js
index 47f4f5d..ff2bbc1 100644
--- a/node_modules/jest-runtime/build/cli/index.js
+++ b/node_modules/jest-runtime/build/cli/index.js
@@ -203,7 +203,7 @@ function run(cliArgv, cliInfo) {
return;
}

- const root = (0, _realpathNative().sync)(process.cwd());
+ const root = process.cwd();

const filePath = _path().default.resolve(root, argv._[0]);

Loading

0 comments on commit c6d166b

Please sign in to comment.