Skip to content

Commit

Permalink
javascript: Test goal with package manager installed test runner supp…
Browse files Browse the repository at this point in the history
…ort (#18554)

Add support for running package manager installed test runners, and
`javascript_test` targets.

There's multiple flavors of how coverage reports are configured (extra
args, separate script) depending on test runner.
To stay unopinionated I implemented support for both. If one has to go,
extra args is the one.

This PR includes:
1. Introduce a new `scripts` mapper build symbol `node_test_script`,
where users can augment default behaviour, which is:
2. To run `(npm|pnpm|yarn) run test`, the "standard" way to execute test
runners.
3. Coverage reports per test support. I made no effort to merge the
coverage reports.
4. No batch support (yet). I think it would be good to OOTB assume that
`npm run test <all-files-for-workspace>` is preferred to current
behaviour of 1 run per file. See e.g
https://jestjs.io/docs/cli#--maxworkersnumstring.

Fixes #18524.
  • Loading branch information
tobni authored Mar 30, 2023
1 parent a805788 commit 37b3001
Show file tree
Hide file tree
Showing 41 changed files with 27,174 additions and 77 deletions.
12 changes: 10 additions & 2 deletions src/python/pants/backend/experimental/javascript/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
from typing import Iterable

from pants.backend.javascript import package_json
from pants.backend.javascript.goals import lockfile, tailor
from pants.backend.javascript.goals import lockfile, tailor, test
from pants.backend.javascript.package.rules import rules as package_rules
from pants.backend.javascript.run.rules import rules as run_rules
from pants.backend.javascript.subsystems import nodejs
from pants.backend.javascript.target_types import JSSourcesGeneratorTarget, JSSourceTarget
from pants.backend.javascript.target_types import (
JSSourcesGeneratorTarget,
JSSourceTarget,
JSTestsGeneratorTarget,
JSTestTarget,
)
from pants.build_graph.build_file_aliases import BuildFileAliases
from pants.engine.rules import Rule
from pants.engine.target import Target
Expand All @@ -24,13 +29,16 @@ def rules() -> Iterable[Rule | UnionRule]:
*lockfile.rules(),
*package_rules(),
*run_rules(),
*test.rules(),
)


def target_types() -> Iterable[type[Target]]:
return (
JSSourceTarget,
JSSourcesGeneratorTarget,
JSTestTarget,
JSTestsGeneratorTarget,
*package_json.target_types(),
)

Expand Down
4 changes: 4 additions & 0 deletions src/python/pants/backend/javascript/goals/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ python_sources()

python_tests(
name="tests",
# The package.json files are inlined in ./test_integration_tests.py
overrides={
"test_integration_test.py": dict(dependencies=["./jest_resources", "./mocha_resources"]),
},
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

resources(sources=["package-lock.json"])
Loading

0 comments on commit 37b3001

Please sign in to comment.