I'll chime in with the other two well-stated answers. In addition to all the testing, there is the issue of "What do you do when the instrument fails a test?"
Most COTS (Commercial Off-The-Shelf) instruments you might get at Home Depot or even Omega Engineering are designed to work in an Earth environment, with some margin. But not too much margin; that makes the instrument more expensive than the competitors', and that loses business. Note that the Melexis instrument @Giskard42 mentioned has a range of -40 to +125 C. You can get lower temperatures here on Earth's surface. Mars gets a lot colder than that at night!
The Melexis engineers, who would certainly be consulted early in the process, would immediately say that to handle Mars temperatures without adding heaters some of the components would have to be replaced with more resilient—and more expensive—parts. But the cost of those parts pales in comparison to the cost of the redesign necessary to incorporate the parts. Rarely does the more resilient part behave exactly like the original, or fit where the original did, so even if redesign winds up being unnecessary, the operating characteristics have to be reanalyzed and retested. Adding heaters would also be a redesign.
Thermal qualification is only one part of space qualification, a rather lengthy process NASA requires for hardware intended for use in all but the smallest of NASA space flight missions. But often it's not the hardest part for COTS hardware.
@Giskard42 already mentioned radiation tolerance. For interplanetary missions that is often the hardest part for COTS hardware. Modern microcircuitry (such as ADCs), with exquisitely small feature sizes, is sensitive to radiation effects from sources like primary cosmic rays and solar radiation, especially solar protons. A single hit can cause single-event upsets, bit-flips, and even the dreaded latchups. Flight-qualified hardware needs to demonstrate (via test) a certain level of tolerance to radiation, sometimes requiring redundant sub-assemblies or components, which you won't find in an off-the-shelf instrument. Unmodified COTS parts or components often fail the radiation tests and that usually means redesign, and that's expensive.
All these processes can quickly turn a 5 dollar instrument into a 50 kdollar instrument, or even 500 kdollar instrument if nobody else wants a space-qualified version of this widget.
But buying the space-qualified instrument isn't the end of the money story. You also have to pay the spacecraft engineers who have to do instrument accomodation. Is your instrument the only one requiring +9 VDC instead of the spacecraft-standard 28 VDC? Then you pay for an engineer to design a 9 V power subassembly into the spacecraft's power system, and to design and run that part of the cable harness. You'll also be paying for a thermal engineer to verify that the thermal design is adequate, even before it goes onto the shake table (as @PearsonArtPhoto mentioned) and into the thermal-vac chamber. Will your instrument generate any signals that interfere with other spacecraft systems? An engineer trained in EMI will examine this. There is an instrument team that you pay for, and a spacecraft team the project pays for, shepherding this process all the way through. For an inexpensive piece of hardware this is the most expensive part.
In my experience with Voyager, Cassini, Genesis, and Rosetta, and a lot of mission concept studies and proposals, I've seen a few instruments for interplanetary missions come in at single-digit millions of dollars, but not many. Most are tens of millions of dollars, and really complex ones can add another zero to that. I'd love to know what the Kepler instrument cost, but a PI usually holds cost breakdown figures for competed missions very close to the chest.
One final note. In the 1990's, under Dan Goldin as NASA Administrator, NASA tried the approach of flying missions on the cheap, to get more missions flown. But a series of embarrassing failures that resulted (such as Mars Polar Lander and the DS-2 instrumented impactors it carried) put an end to that approach and Dan resigned soon after. NASA is rather intolerant of failures, especially on highly visible (to the public and to Congress), big-buck missions, and is willing to spend a lot of money to prevent them.