Skip to content

Latest commit

 

History

History
131 lines (95 loc) · 4.99 KB

File metadata and controls

131 lines (95 loc) · 4.99 KB

Jest

Jest tests JavaScript.

Reference

Starlark reference

Install

Add jest as an external dependency.

Options

By default, Jest uses -i --no-cache. Add options via test arguments.

Sharding support requires Jest v28.

Use

example/example.js

export function add(a, b) {
  return a + b;
}

example/example.spec.js

const { add } = require("./example");

test("adds", () => {
  expect(add(1, 2)).toBe(3);
});

example/jest.config.js

exports.verbose = true;

example/BUILD.bzl

load("@better_rules_javascript//commonjs:rules.bzl", "cjs_root")
load("@better_rules_javascript//javascript:rules.bzl", "js_library")
load("@better_rules_javascript//jest:rules.bzl", "jest_test")

cjs_root(
    name = "root",
)

js_library(
    name = "jest_config",
    root = ":root",
    srcs = ["jest.config.js"],
)

js_library(
    name = "lib",
    root = ":root",
    srcs = ["example.js"],
)

jest_test(
    name = "test",
    config = "jest.config.js",
    config_dep = ":jest_config",
    dep = ":test_lib",
    jest = "@npm//jest:lib",
    node = "@better_rules_javascript//rules:nodejs",
)

js_library(
    name = "test_lib",
    root = ":root",
    srcs = ["example.spec.js"],
)

Snapshots

To update snapshots, run the test as an executable with -u/--update-snapshot.

bazel run :example -- -u

//jest:rules.bzl

jest_test

jest_test(name, bash_preamble, config, config_dep, data, dep, env, jest, node, node_options)

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
bash_preamble - String optional ""
config Path to config file, relative to config_dep root. String required
config_dep Jest config dependency. Label required
data Runtime data. List of labels optional []
dep Test dependency. Label required
env Environment variables. Dictionary: String -> String optional {}
jest Jest dependency. Label required
node - Label optional //nodejs
node_options Node.js options. List of strings optional []