Skip to content

Writing Tests

Conflake integrates lib.debug.runTests with nix flake check.

Here is a minimal outputs to define tests,

nix
outputs =
  { conflake, ... }@inputs:
  conflake ./. {
    inherit inputs;

    tests = {
      add = {
        expr = 1 + 1;
        expected = 2;
      };
    };
  };

Tests attributes are prefixed with test- before calling lib.debug.runTests, set presets.checks.tests.prefix = ""; to turn it off.

Running Tests

Run nix flake check will run the tests with lib.debug.runTests along side with other checks.

To run only the tests,

shell
# Print nothing when tests passed
nix build --no-link .#checks.x86_64-linux.tests

File-based Tests

Additionally, nix files under tests/folder will be loaded and run withnix flake check too.

Each test files should define a function returning an attribute set of test cases, e.g.

nix
{ lib, ... }:

{
  singleton = {
    expr = lib.singleton 1;
    expected = [ 1 ];
  };
}

The function arguments is a combination of pkgs, moduleArgs and presets.checks.tests.args.

List Form Test

Apart from attribute set {expr, expected} test, test case can be defined as a list, which is convenient when the testing value need to be transformed before comparing against the expected value.

nix
{ lib, ... }:

{
  simple = [
    (lib.singleton { a = 1; })
    [ { a = 1; } ]
  ];

  chained = [
    (lib.singleton { a = 1; })
    builtins.head
    (x: x.a)
    1
  ];
}