This teaching is found in Tanna D’vei Eliyahu, volume 2, chapter 7 - Eliyahu Zuta.
 The complete teaching from Tanna D’vei Eliyahu is as follows: “Similarly, [concerning] a good deed: When a person performs a mitzvah, he is rewarded [for] up to four generations. You should know that this is the case, for it says, “And HaShem said to Yehu, because you have done good to [perform] that which is proper in My eyes,… four generations of your descendants shall sit on the throne of Israel for you.”” (Milachim II: 10; 30) (Tanna D’vei Eliyahu, volume II, chapter 7; 1)
“Similarly, it was said about Yiraviam the son of Yoash, that he was a person who behaved respectfully toward the prophets. Therefore, those nations that The Holy One, Blessed is He, did not give over into the hand of Yihoshua bin Nun and did not deliver into the hand of David, the King of Israel, The Holy One, Blessed is He, gave over into the hand of Yiraviam the son of Yoash, as it says, “He returned the borders of Israel from the approach to Chamas until the Sea of Aravah, like the word of HaShem, the G-d of Israel, which He spoke through his servant Yonah the son of Amitai, the prophet…For HaShem saw that the suffering of Israel was very powerful and they were without property, both in the house and in the field… and HaShem brought them salvation by way of Yiraviam the son of Yoash.” (Milachim II: 14; 25 - 27) What was truly the [positive] attribute that Yiraviam the son of Yoash [possessed], which enabled him to restore the borders of Israel? Wasn’t he an idol worshipper! Rather, [he was successful in expanding the borders of Israel] because he did not accept the Lashon HaRa [spoken] against Amos, as it says, “And Amatziah, the priest of Beit-El, sent [word] to Yiraviam, the king of Israel, saying, ‘Amos has conspired against you in the midst of the House of Israel, the Land is unable to bear all of his words. For so said Amos, ‘Yiraviam shall die by the sword…’’” (Amos: 7, 10 – 11) Immediately [upon hearing this], Yiraviam rebuked Amatziah and sent him out with a reprimand. Yiraviam said to [Amatziah], Heaven Forbid, that righteous man did not prophesy such [as you say]. At that moment, The Holy One, Blessed is He, said, ‘Even though that generation worships idols, and the head of the generation is an idol worshipper, even so, the Land [concerning] which I said to Avraham, Yitzchak, and Ya’akov that I would give to their descendants, I shall give it through the hand of this one.’ Therefore, it is said [in reference to Yiraviam], “He restored the borders of Israel…” Based on this, [our Sages of Blessed Memory] have said: “Merit comes by way of those who are meritorious, and punishment [comes] by way of those who are guilty.” Similarly, [HaShem] acts with [the above] attribute toward all Jews in all of the places of their dwellings, [as well as] with all of the worshippers of idolatry, and with all people of the world.” (Tanna D’vei Eliyahu, volume II, chapter 7; 3)
MaLBI”M points out on the above passage from Sefer Amos, that Amos prophesied that the House of Yiraviam would fall, though he did not prophesy that Yiraviam would be killed by the sword.
 The Hebrew term "ovdei kochavim u'mazalos" is literally translated as “worshippers of stars and constellations”.