I'd say, How much do you want this job? And, How much work are they asking you to do?
A few years back I saw an ad for a programming job where they said they wanted a job applicant to produce a complete system with a database and I think it was 6 data entry screens and a dozen or so reports, "including complete documentation" they said. It sounded to me suspiciously like they just wanted this system built, and they figured they'd get someone to build it for them for free under the guise of a job application, then say sorry, the position has been filled. But even if they really saw this as simply a skills test, it looked to me like doing a quality job would take several weeks of full-time effort. Just writing "complete documentation" for 15 to 20 screens would would surely take a several days.
And so I asked myself: Say I could do it in just 40 hours. Which is the more productive use of my time? I could spend 40 hours on a cold application for this one job. Or in 40 hours I could find dozens of other job opportunities and submit resumes to them.
So I ask you, how much time do you think it would take to do this project? And is this job opportunity worth that much time? Factoring in, of course, the chance that you will get the job if you complete the test successfully. You said "several hours". If they're asking you to do a task that will take, say, 2 hours, and this will put you on the short list or you are already on the short list so there's a measurable chance that you'll get the job if you do this task well, I'd be inclined to do it. If someone asked me to spend 2 hours in a job interview I'd have no problem with that; this is in the same league. If they're asking you to put in 40 hours and at that point you're one out of 200 applicants, I wouldn't take the time unless there were just no other opportunities out there.
I'd be suspicious of someone who asks me to write code that they will then use in production. First off, what do they think is going to happen if there is a problem with this code, or it needs enhancements? Do they think I'm going to come back and do more work for free? I'd worry that these people have no concept how programming works. And second, I'd worry that they're not really planning to hire anyone, the "job opening" is just a scam to get someone to do the work for free. And any real life programming task typically requires significant time reviewing and clarifying requirements. If they think they're going to give a paragraph of requirements and I'm going to bang out production code in 2 hours, again, I don't think they know how programming works.
I've had job interviews where they asked me to write some small piece of code as a skills test. Typically they use some very simple problem that is easy to describe. Like find the first 50 prime numbers, sort a list of words alphabetically, etc. I have no problem with these. It makes good sense to give an applicant a practical skills test. Even if their code is incomplete or has bugs, you can get a good idea of their skill level.
But write code that we will use in production? I wouldn't say never, but I'd be cautious.