The image above shows the only nuclear weapon detonated in space. It happened in 1962 and the reason why the detonated it was to test a few theories.
The plan was to send rockets hundreds of miles up, higher than the Earth's atmosphere, and then detonate nuclear weapons to see: a) If a bomb's radiation would make it harder to see what was up there (like incoming Russian missiles!); b) If an explosion would do any damage to objects nearby; c) If the Van Allen belts would move a blast down the bands to an earthly target (Moscow! for example); and — most peculiar — d) if a man-made explosion might "alter" the natural shape of the [Earth's magnetic] belts.