Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rules for running jest tests #395

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ load("//internal/node:node.bzl",
_nodejs_test = "nodejs_test_macro")
load("//internal/node:node_repositories.bzl",_node_repositories = "node_repositories")
load("//internal/jasmine_node_test:jasmine_node_test.bzl", _jasmine_node_test = "jasmine_node_test")
load("//internal/nodejs_jest_test:nodejs_jest_test.bzl", _nodejs_jest_test = "nodejs_jest_test")
load("//internal/npm_install:npm_install.bzl", _npm_install = "npm_install", _yarn_install = "yarn_install")
load("//internal/rollup:rollup_bundle.bzl", _rollup_bundle = "rollup_bundle")
load("//internal/npm_package:npm_package.bzl", _npm_package = "npm_package")
Expand All @@ -41,6 +42,7 @@ rollup_bundle = _rollup_bundle
npm_package = _npm_package
history_server = _history_server
http_server = _http_server
nodejs_jest_test = _nodejs_jest_test
# ANY RULES ADDED HERE SHOULD BE DOCUMENTED, run yarn skydoc to verify

check_rules_nodejs_version = _check_rules_nodejs_version
Expand Down
21 changes: 21 additions & 0 deletions internal/nodejs_jest_test/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2017 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.

package(default_visibility = ["//visibility:public"])

exports_files([
"jest_runner.js",
# Exported to be consumed for generating skydoc.
"nodejs_jest_test.bzl",
])
33 changes: 33 additions & 0 deletions internal/nodejs_jest_test/jest_runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
jest = require('jest')
fs = require('fs-extra')
path = require('path')
process = require('process')

// [0], [1] is node executable and script path
npm_ws_name = process.argv[2]
package_name = process.argv[3]

var packageJson = {
jest: {
modulePaths: [
path.join(process.cwd(),
'external/{}/node_modules'.replace('{}', npm_ws_name))
]
}
}

console.log('Current directory ' + process.cwd())


console.log('Changing directory to ' + package_name)
process.chdir(package_name)

console.log('Writing package.json for jest')
fs.writeFileSync('package.json', JSON.stringify(packageJson));

console.log('Copying tests over temp directory')
fs.copySync('tests', 'jest', {
dereference: true,
});

jest.run();
64 changes: 64 additions & 0 deletions internal/nodejs_jest_test/nodejs_jest_test.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2017 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.

"""Jest testing for NodeJS

These rules let you run tests with facebook/jest.
"""
load("//internal/node:node.bzl", "nodejs_test")

def nodejs_jest_test(
name,
srcs = [],
data = [],
deps = [],
**kwargs):
"""Runs test with a jest.

Args:
name: Name of the resulting label
srcs: JavaScript source files containing jest tests
data: Runtime dependencies which will be loaded while the test executes
deps: Other targets which produce JavaScript; should contain `jest` and `fs-extra`
**kwargs: remaining arguments are passed to the test rule
"""

npm_repo_workspace_name = None
fs_extra_dep_name = None

for dep in deps:
if dep.endswith('//jest'):
npm_repo_workspace_name = dep.replace('//jest', '').replace('@', '')
elif dep.endswith('//fs-extra'):
fs_extra_dep_name = dep

if not npm_repo_workspace_name:
fail('jest is not found in `deps`, please include it')

if not fs_extra_dep_name:
fail('fs-extra is not found in `deps`, please include it')

all_data = data + srcs + deps
all_data += [Label("//internal/nodejs_jest_test:jest_runner.js")]
all_data += [Label("@bazel_tools//tools/bash/runfiles")]
entry_point = "build_bazel_rules_nodejs/internal/nodejs_jest_test/jest_runner.js"


nodejs_test(
name = name,
data = all_data,
entry_point = entry_point,
templated_args = [npm_repo_workspace_name, native.package_name()],
**kwargs
)
2 changes: 2 additions & 0 deletions internal/test/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"description": "runtime dependencies for tests",
"devDependencies": {
"fs-extra": "^7.0.0",
"is-builtin-module": "^2.0.0",
"jasmine": "~2.8.0",
"jest": "^23.6.0",
"node_resolve_index": "file:./node_resolve_index",
"node_resolve_index_2": "file:./node_resolve_index_2",
"node_resolve_index_3": "file:./node_resolve_index_3",
Expand Down
Loading