Unit Testing in Jsonnet


For the time being(April 2025), there doesn’t seem to be a de facto Jsonnet framework for unit testing. Yet with simple assert statements it’s very helpful to have tests for shared functions, etc.

# shared.libsonnet
{
  hello(name):: 'hello, %s' % name
}

# tests.jsonnet
local lib = import './shared.libsonnet';

{
  test_hello: lib.hello('Joe'),
  assert self.test_hello == 'hello, John' // this is expected to fail
}

# to run the tests
> jsonnet tests.jsonnet
Error: RUNTIME ERROR: Object assertion failed.
...
  
# and if we fix the test and replace 'John' with 'Joe'
> jsonnet tests.jsonnet
{
  "test_hello": "hello, Joe"
}

🙂