rules_jest icon indicating copy to clipboard operation
rules_jest copied to clipboard

[FR]: Add support for --test_filter flag

Open gregmagolan opened this issue 2 years ago • 3 comments

What is the current behavior?

Not supported

Describe the feature

When --test_filter is set, Bazel will encode its value in the TESTBRIDGE_TEST_ONLY environment variable (see Test Encyclopedia). rules_jest should read this value if it is set and filter test specs accordingly.

gregmagolan avatar Nov 10 '23 13:11 gregmagolan

A possible solution is to pass into https://jestjs.io/docs/cli#--filterfile something like:

const jestTestFilter: Filter = async testPaths => {
  if ('TESTBRIDGE_TEST_ONLY' in process.env) {
    const issuesFilterRegexp = new RegExp(process.env.TESTBRIDGE_TEST_ONLY!)
    const filteringFunction = (testPath: string) => issuesFilterRegexp.test(testPath)

    const allowedPaths = testPaths.filter(filteringFunction).map(test => ({
      test,
      message: `This test suite was selected by value --test_filter=${process.env.TESTBRIDGE_TEST_ONLY}`
    }))

    return {
      filtered: allowedPaths
    }
  } else {
    return {
      filtered: testPaths.map(test => ({ test }))
    }
  }
}

module.exports = jestTestFilter

// taken from @jest/core
type Filter = ( testPaths: Array<string> ) => Promise<{ filtered: Array<FilterResult> }>

type FilterResult = {
  test: string
  message?: string
}

However, we should not enable sharding when --test_filter= is used. Is it possible to set shard_count to zero when --test_filter contains a non-default value? Or the only way to go is always to pass --test_sharding_strategy=disabled?

@Wenqer what was the reason for not using sharding when tests are filtered?

jbedard avatar Nov 28 '23 07:11 jbedard

@jbedard Specifying the test filter usually means that we will execute much fewer suite files. In most situations, it is used to test only a single suite, and in such cases, all other shards will return exit code 1 because spawned with an empty suites list.