Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MichaelDimmitt/1f1a530b1c68cac26a26fccd890a4bb7 to your computer and use it in GitHub Desktop.
Save MichaelDimmitt/1f1a530b1c68cac26a26fccd890a4bb7 to your computer and use it in GitHub Desktop.

global-npm-bats-install-including-bats-assert.md

Check before running bats does not exist: which bats
Check after running bats exists at nodemodule path which bats

If you have nvm and switch versions a reinstall may be required.
Putting the following into terminal will setup a bash test suite example using NPM global packages.

## remove potential previous bats installation:
## rm -rf /usr/local/bats;

npm install -g bats &&
npm install -g bats-assert &&
npm install -g bats-support@git+https://github.com/ztombol/bats-support.git#v0.2.0 &&

mkdir bats_test_suite_example &&
cd bats_test_suite_example &&

echo '
node_module_path() {
  bats_location=$(which bats);
  echo "${bats_location%/*/*/*/*}";
}
load "$(node_module_path)/bats-assert/load.bash";
load "$(node_module_path)/bats-support/load.bash";
' >> helpers.bash &&

echo '
#!/usr/bin/env bats
load 'helpers';

@test "addition using bc" {
  result="$(echo 2+2 | bc)"
  [ "$result" -eq 4 ]
}
@test "assert_equal()" {
  assert_equal "have" "want"
}
' >> addition.bats &&
bats addition.bats
@MichaelDimmitt
Copy link
Author

MichaelDimmitt commented Apr 21, 2019

Uninstall using:

npm uninstall -g bats &&
npm uninstall -g bats-assert &&
npm uninstall -g bats-support@git+https://github.com/ztombol/bats-support.git#v0.2.0

@MichaelDimmitt
Copy link
Author

The helper file idea came from this bash program called notes:
https://github.com/pimterry/notes

There are also some good test examples in that repo.
Notes does not use npm global installs instead that project uses submodules:

git clone <your fork>
git submodule update --init --recursive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment