With entr (a small package, apt install entr):
ls *.py | entr -c python3 main.py
It reruns the command every time one of the listed files changes; -c clears the screen first. Watch a whole tree with find:
find src -name '*.ts' | entr -c npm test
Without entr, inotifywait from inotify-tools in a loop:
while inotifywait -qr -e modify,create,delete src; do npm test; done
And for the "just rerun it every two seconds" case, there's watch -n 2 ls -l.