Skip to content
Merged
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
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,32 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Tag-driven release workflow for Maven Central publishing and GitHub releases.
- Release runbook covering secrets, version sweep, annotated tags, and verification.

## [0.2.1] - Overridable test-time JS env

### Added

- `BunScalaJSTests.bunTestJsEnv` — a test-scoped override for the Scala.js
test process environment. Defaults to the outer module's `bunJsEnv`
unchanged (no behavior change for existing users).
- `BunScalaJSTests` now owns its own `jsEnvConfig` that sources its env
from `bunTestJsEnv`, so overriding that map is enough to diverge test
env from production `bunRun`.

### Why

Tests that spin up an in-process `Bun.serve({...})` typically want
`NODE_ENV=production`, which flips Bun's `development` default to
`false`. Otherwise a fetch-handler Promise rejection is rewritten into
a ~100 KB HTML `BunError` React-overlay `Response` — the test then
asserts against the overlay HTML instead of the real error, which
hides bugs.

Downstream libraries can now opt in with one override:

```scala
object test extends BunScalaJSTests:
override def bunTestJsEnv = Task {
super.bunTestJsEnv() + ("NODE_ENV" -> "production")
}
```

38 changes: 38 additions & 0 deletions millbun/src/mill/scalajslib/bun/BunScalaJSModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,44 @@ trait BunScalaJSModule extends ScalaJSConfigModule with BunToolchainModule { out
}
}

/**
* Runtime environment for the JS process that drives the Scala.js test
* framework. Defaults to the outer module's [[bunJsEnv]] unchanged, so
* plain test runs behave exactly like before.
*
* Override this when your tests need environment variables that differ
* from production `bunRun` invocations. A common case: set
* `NODE_ENV=production` so that in-process [[https://bun.sh/docs/api/http
* `Bun.serve({...})`]] calls default `development: false`. Otherwise
* Bun's dev-mode error overlay rewrites any fetch-handler Promise
* rejection into a ~100 KB HTML `Response` (the `BunError` React
* bundle), which masks the real error inside a running test and makes
* HTTP-level assertions impossible. Example:
*
* {{{
* object test extends BunScalaJSTests:
* override def bunTestJsEnv = Task {
* super.bunTestJsEnv() + ("NODE_ENV" -> "production")
* }
* }}}
*/
def bunTestJsEnv: T[Map[String, String]] = Task { outer.bunJsEnv() }

/**
* Scala.js environment used specifically for the test framework process.
* Mirrors [[outer.jsEnvConfig]] but sources its `env` from
* [[bunTestJsEnv]] so tests can diverge (e.g. override `NODE_ENV`)
* without affecting `bunRun` on the outer module.
*/
override def jsEnvConfig: T[JsEnvConfig] = Task {
JsEnvConfig.NodeJs(
executable = outer.bunExecutable(),
args = outer.bunJsEnvArgs().toList,
env = bunTestJsEnv(),
sourceMap = outer.scalaJSSourceMap()
)
}

override protected def testLinkTask: Task[Report] = Task.Anon {
val linkConfig =
outer.moduleKind() match {
Expand Down
Loading