Skip to content

willjr/nixt

 
 

Repository files navigation

Nixt

Nixt is a unit-testing tool for Nixlang.

Installation

Imperatively

Using nix-env:

$ nix-env -if https://github.com/nix-community/nixt/archive/master.tar.gz

Using nix profile:

$ nix profile install github:nix-community/nixt

Declaratively

Add Nixt as a flake to your configuration:

{
  inputs.nixt = {
    url = "path:/home/ldlework/src/nixt";
    inputs.nixpkgs.follows = "nixpkgs";
  };
}

Then add the package to your packages:

{
  environment.systemPackages = [
    inputs.nixt.defaultPackage.x86_64-linux
  ];
}

Help

nixt

  Test-runner for nixlang.

Options

  -p, --path string   Path to the test suite
  -w, --watch         Watch for changes at path
  -v, --verbose       Show additional test info
  -l, --list          List, but don't run, tests
  -d, --debug         Show nixt-developent relevant info
  -h, --help          Prints this usage guide

Running Tests

The nixt CLI discovers and runs tests located at -p/--path:

$ nixt ./nix/

Found 14 cases in 8 suites over 3 files.

  ✗ 2 cases failed.

┏ /home/ldlework/src/nixt/cli/nix/get-testset.test.nix
┃   mkSuites
┗     ✗ always fails

┏ /home/ldlework/src/nixt/cli/nix/utils.test.nix
┃   broken test
┗     ✗ undefined variable

  ⚠ Couldn't import 1 files:
    - /home/ldlework/src/nixt/cli/nix/invalid.test.nix
      Import error: called with unexpected argument 'nixt'
      Did you forgot to add the 'nixt' argument to your test expression?

Adding in the -v/--verbose flag will show passing cases and additional information on failed cases:

$ nixt ./nix/ -v

Found 14 cases in 8 suites over 3 files.

  ✗ 2 cases failed.

┏ /home/ldlework/src/nixt/cli/nix/get-testset.test.nix
┃   mkSuite
┃     ✓ creates correct structure
┃   mkSuites
┃     ✗ always fails
┗     ✓ creates correct structure

┏ /home/ldlework/src/nixt/cli/nix/utils.test.nix
┃   broken test
┃     ✗ undefined variable
┃       error: undefined variable 'baz'
┃       at /home/ldlework/src/nixt/cli/nix/utils.test.nix:12:30:
┃       11|     "broken test" = {
┃       12|       "undefined variable" = baz;
┃       |                              ^
┃       13|     };
┃       (use '--show-trace' to show detailed location information)
┃   dirFiles
┃     ✓ empty list for non-existent path
┃     ✓ non-empty list for existing path
┃   findNixFiles
┃     ✓ empty list for non-existent path
┃     ✓ non-empty list for existing path
┃   getDir
┃     ✓ empty list for non-existent path
┃     ✓ non-empty list for existing path
┃   isNix
┃     ✓ false for non-nix files
┃     ✓ true for nix files
┃   isTestSuite
┃     ✓ false for non-test suites
┗     ✓ true for test suites

  ⚠ Couldn't import 1 files:
    - /home/ldlework/src/nixt/cli/nix/invalid.test.nix
      Import error: called with unexpected argument 'nixt'
      Did you forgot to add the 'nixt' argument to your test expression?

Two -v -v verbose flags implies --show-trace.

Listing Tests

To list discovered tests without actually evaluating their cases use the --l/-list flag:

$ nixt ./nix/ -l

Found 14 cases in 8 suites over 3 files.

  ⚠ Couldn't import 1 files:
    - /home/ldlework/src/nixt/cli/nix/invalid.test.nix
      Import error: called with unexpected argument 'nixt'
      Did you forgot to add the 'nixt' argument to your test expression?

Or with the -v/--verbose flag:

$ nixt ./nix/ -l -v

Found 14 cases in 8 suites over 3 files.

┏ /home/ldlework/src/nixt/cli/nix/get-testset.test.nix
┃   mkSuite
┃     - creates correct structure
┃   mkSuites
┃     - always fails
┗     - creates correct structure

┏ /home/ldlework/src/nixt/cli/nix/utils.test.nix
┃   broken test
┃     - undefined variable
┃   dirFiles
┃     - empty list for non-existent path
┃     - non-empty list for existing path
┃   findNixFiles
┃     - empty list for non-existent path
┃     - non-empty list for existing path
┃   getDir
┃     - empty list for non-existent path
┃     - non-empty list for existing path
┃   isNix
┃     - false for non-nix files
┃     - true for nix files
┃   isTestSuite
┃     - false for non-test suites
┗     - true for test suites

  ⚠ Couldn't import 1 files:
    - /home/ldlework/src/nixt/cli/nix/invalid.test.nix
      Import error: called with unexpected argument 'nixt'
      Did you forgot to add the 'nixt' argument to your test expression?

Writing Tests

Nixt tests are written in .test.nix, .spec.nix, or .nixt files that:

  • Contains a function taking attrset args pkgs and nixt
  • Evaluates to a call of nixt.mkSuite or nixt.mkSuites

Each suite is composed of one or more cases.

Each case should be an expression that evaluates to a boolean of whether the test passes.

mkSuite

Args:

  • name: string name of the suite
  • cases: attrset of cases

Defines a single suite of cases for the file.

{ pkgs ? import <nixpkgs> {}, nixt }:

nixt.mkSuite "always passes" {
  "always true" = true; # the expression here should test something
}

mkSuites

Args:

  • suites: attrset of suites

Defines multiple suites of cases for the file.

{ pkgs ? import <nixpkgs> {}, nixt }:

nixt.mkSuites {
  "foo suite" = {
    "foo is foo" = "foo" == "foo";
  };
  "bar suite" = {
    "bar is bar" = "bar" == "baz";
  };
}

About

Simple unit-testing for Nix [maintainer=@dustinlacewell]

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • TypeScript 76.5%
  • Nix 20.6%
  • JavaScript 2.6%
  • Shell 0.3%