Count it all joy, my brothers, when you meet trials of various kinds, for you know that the testing of your faith produces steadfastness." James 1:2-3
To "count it all joy" doesn't mean we will always feel happy in the midst of trials; but regardless of how we feel, it is a command we can and should obey. Martyn Lloyd Jones explains:
“There is all the difference in the world between rejoicing and feeling happy. The Scripture tells us that we should always rejoice [Phil. 4:4]....To rejoice is a command, yes, but there is all the difference in the world between rejoicing and being happy. You cannot make yourself happy, but you can make yourself rejoice, in the sense that you will always rejoice in the Lord. Happiness is something within ourselves, rejoicing is ‘in the Lord.’ Take the fourth chapter of the Second Epistle to the Corinthians. There you will find that the great Apostle puts it all very plainly and clearly in that series of extraordinary contrasts which he makes: ‘We are troubled on every side (I don’t think he felt very happy at the moment) yet not distressed’, ‘we are perplexed (he wasn’t feeling happy at all at that point) but not in despair’, ‘persecuted but not forsaken’, ‘cast down, but not destroyed’--and so on. In other words the Apostle does not suggest a kind of happy person in a carnal sense, but he was still rejoicing."