Genesis 38:8-10 Then Judah said to Onan, "Go in to your brother's wife and perform the duty of a brother-in-law to her, and raise up offspring for your brother." But Onan knew that the offspring would not be his. So whenever he went in to his brother's wife he would waste the semen on the ground, so as not to give offspring to his brother. And what he did was wicked in the sight of the Lord, and he put him to death also.
I will content myself with briefly mentioning this, as far as the sense of shame allows to discuss it. It is a horrible thing to pour out seed besides the intercourse of man and woman. Deliberately avoiding the intercourse, so that the seed drops on the ground, is doubly horrible. For this means that one quenches the hope of his family, and kills the son, which could be expected, before he is born. This wickedness is now as severely as is possible condemned by the Spirit, through Moses, that Onan, as it were, through a violent and untimely birth, tore away the seed of his brother out the womb, and as cruel as shamefully was thrown on the earth. Moreover he thus has, as much as was in his power, tried to destroy a part of the human race. When a woman in some way drives away the seed out the womb, through aids, then this is rightly seen as an unforgivable crime. Onan was guilty of a similar crime, by defiling the earth with his seed, so that Tamar would not receive a future inheritor.
John Calvin, Commentary on Genesis