Resistance to parasites confers a fitness advantage, yet hosts show substantial variation in resistance in natural populations. Evolutionary theory indicates that resistant and susceptible genotypes can coexist if resistance is costly, but there is mixed evidence that resistant individuals have lower fitness in the absence of parasites. One explanation for this discrepancy is that the cost of resistance varies with environmental context. We tested this hypothesis using
Caenorhabditis elegans
and its natural microsporidian parasite,
Nematocida ironsii
. We used multiple metrics to compare the fitness of two near‐isogenic host genotypes differing at regions associated with resistance to
N. ironsii
. To quantify the effect of the environment on the cost associated with these known resistance regions, we measured fitness on three microbial diets. We found that the cost of resistance varied with both diet and the measure of fitness. We detected no cost to resistance, irrespective of diet, when fitness was measured as fecundity. However, we detected a cost when fitness was measured in terms of population growth, and the magnitude of this cost varied with diet. These results provide a proof of concept that, by mediating the cost of resistance, environmental context may govern the rate and nature of resistance evolution in heterogeneous environments.