Skip to content

Instantly share code, notes, and snippets.

@jamessom
Forked from ArturT/.bash_profile
Last active December 5, 2019 13:53
Show Gist options
  • Save jamessom/e2c26ca626ffb4e9bfd492feb75d29da to your computer and use it in GitHub Desktop.
Save jamessom/e2c26ca626ffb4e9bfd492feb75d29da to your computer and use it in GitHub Desktop.
Watch file changes with nodemon and run rspec for the test file or specified line number test. Use spring with rspec optionally.

Watch files with Nodemon and RSpec

Watch file changes with nodemon and run rspec for the test file or specified line number test. Use spring with rspec optionally.

We need to install Nodemon

$ npm install -g nodemon

Add this in your default bash:

  • ~/.bash_profile
  • ~/.zshrc

How to use it?

Run spec for user_spec.rb when file changed.

$ wr spec/models/user_spec.rb

Run spec for user_spec.rb:80 (only spec in line 80) when file changed.

$ wr spec/models/user_spec.rb 80

Use wsr if you want to run spring rspec to speed up. For instance:

$ wsr spec/models/user_spec.rb 80

function run_after_save {
  prefix=$1
  file=$2
  line=$3
  if [ -z "$line" ]; then
    nodemon -x "$prefix $file" -w "$file"
  else
    nodemon -x "$prefix $file:$line" -w "$file"
  fi
}

Create alias to make files easy ;)

alias wsr='run_after_save "bundle exec spring rspec"'
alias wr='run_after_save "bundle exec rspec"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment