The actual wording of the Fifth Admendment is
No person ... shall be compelled in any criminal case to be a witness against himself, ...
Once a person has been granted immunity, that person's testimony cannot be used against him or her, and so compelling testimony does not violate this right.
A person asserting this right ("Pleading the Fifth") must have a reasonable belief that the testimony might actually be used in a criminal proceeding against him or her.
In Murphy v. Waterfront Comm'n, 378 U.S. 52 (1964) at page 68 it is pointed out that
the danger of prosecution "must be real and appreciable . . . , not a danger of an imaginary and unsubstantial character. . . ."
(quoting The Queen v. Boyes, 1 B. & S. 311, a English case decided by the Queen's Bench in 1861)
In The Queen v. Boyes a witness had declined to answer a question on the ground that it might tend to incriminate him, whereupon the
"Solicitor General then produced a pardon of the witness."
The witness nevertheless refused to answer the question on the ground that he could still be impeached by the Parliament. The court held
"that the danger to be apprehended must be real and appreciable, with reference to the ordinary operation of law in the ordinary course of things -- not a danger of an imaginary and unsubstantial character, having reference to some extraordinary and barely possible contingency so improbable that no reasonable man would suffer it to influence his conduct. . . ."
(from Footnote 7 of Murphy)
Murphy v. Waterfront Comm'n, reviews the history of grants of immunity is some detail, and is worth reading for those interested in the subject. In that case the rule against self-incrimination was made fully applicable to the states, and State and Federal governments were each required to honor grants of immunity by the other.
Addition: It should also be mentioned that if the statute of limitations for the underlying crime has expired, a witness cannot rely on the Fifth Amendment to refuse to testify, because that witness cannot be prosecuted. The same would be true if the law making the action criminal had been repealed.