There may be many different reasons for muscle spasm, however, from a musculoskeletal perspective, it could be due to a problem with:
- the nerve exiting from the spine,
- the nerve as it passes through the tissue
- or from the muscle itself.
Irritation of the nerve can result in abberant firing and stimulation of the muscle and hence spasm. If this is suspected, an MRI scan of the spine or nerve conduction tests can be helpful.
Sometimes, cramp in the muscle could be due to a change in blood electrolytes or a problem within the muscle itself. In these situations, blood tests or muscle biopsies can help with the diagnosis.
I hope this helps.