| """This module defines the karma_test rule.""" |
| |
| load("@npm//@bazel/rollup:index.bzl", "rollup_bundle") |
| load("@npm//karma:index.bzl", _generated_karma_test = "karma_test") |
| load("//infra-sk:ts_library.bzl", "ts_library") |
| |
| def karma_test(name, src, deps = []): |
| """Runs TypeScript unit tests in a browser with Karma, using Mocha as the test runner. |
| |
| When invoked via `bazel test`, a headless Chrome browser will be used. This supports testing |
| multiple karma_test targets in parallel, and works on RBE. |
| |
| When invoked via `bazel run`, it prints out a URL to stdout that can be opened in the browser, |
| e.g. to debug the tests using the browser's developer tools. Source maps are generated. |
| |
| When invoked via `ibazel test`, the test runner never exits, and tests will be rerun every time |
| a source file is changed. |
| |
| When invoked via `ibazel run`, it will act the same way as `bazel run`, but the tests will be |
| rebuilt automatically when a source file changes. Reload the browser page to see the changes. |
| |
| Args: |
| name: The name of the target. |
| src: A single TypeScript source file. |
| deps: Any ts_library dependencies. |
| """ |
| |
| # Enforce test naming conventions. The Gazelle extension for front-end code relies on these. |
| if not src.endswith("_test.ts"): |
| fail("Karma tests must end with \"_test.ts\".") |
| for suffix in ["_puppeteer_test.ts", "_nodejs_test.ts"]: |
| if src.endswith(suffix): |
| fail("Karma tests cannot end with \"%s\"." % suffix) |
| |
| ts_library( |
| name = name + "_lib", |
| srcs = [src], |
| deps = deps, |
| ) |
| |
| rollup_bundle( |
| name = name + "_bundle", |
| entry_point = src, |
| deps = [ |
| name + "_lib", |
| "@npm//@rollup/plugin-node-resolve", |
| "@npm//@rollup/plugin-commonjs", |
| "@npm//rollup-plugin-sourcemaps", |
| ], |
| format = "umd", |
| config_file = "//infra-sk:rollup.config.js", |
| ) |
| |
| # This rule is automatically generated by rules_nodejs from Karma's package.json file. |
| _generated_karma_test( |
| name = name, |
| size = "large", |
| data = [ |
| name + "_bundle", |
| "//infra-sk/karma_test:karma.conf.js", |
| "@npm//karma-chrome-launcher", |
| "@npm//karma-sinon", |
| "@npm//karma-mocha", |
| "@npm//karma-chai", |
| "@npm//karma-chai-dom", |
| "@npm//karma-spec-reporter", |
| "@npm//mocha", |
| ], |
| templated_args = [ |
| "start", |
| "$(execpath //infra-sk/karma_test:karma.conf.js)", |
| "$$(rlocation $(location %s_bundle))" % name, |
| ], |
| tags = [ |
| # Necessary for it to work with ibazel. |
| "ibazel_notify_changes", |
| ], |
| ) |