Chapter 4 - error-handling ticket

Hi, I am working on error-handling ticket and facing an issue.
After making required changes in the moviesDAO, I am able to execute unit test successfully but not able to pass it in STATUS page.

After checking console.log at server side, I found that API is failing with error " `ReferenceError: expect is not defined". Is there a way to check if I have correct copy of code file?

If you’re seeing this in the log whilst running the web application (rather than the unit tests) then somehow you’re trying to reference something called expect in the application code.

expect is the name of a function which unit test frameworks put into the global scope, and is used in unit tests for asserting things, such as expect(a).toBe(b). We wouldn’t put code like this into the application itself, only into unit tests.

Now I can’t be certain about this, because the application code could genuinely contain a reference to something called expect and you could have somehow broken the code around it, but I’d suggest that it’s worth checking something - have you accidentally put some code which belongs in the unit tests (e.g. a call to expect()) into the application code?

1 Like

Thank you so much Simon for quick reply.
Yes, I was using expect to identify a specific error in the application code.
After I commented it, it is working but I need to figure out why expect is not referenced as I didn’t touched anything apart from DAO’s.

Thanks again for your help. Cheers!!!

If you want to put temporary debug code into the application (which is a perfectly acceptable approach in my humble opinion, just remember to remove it before putting the code into production), don’t use expect, instead try the console global variable, for example

console.log("I want to know this");
console.warn("I wasn't expecting this");
console.error("It's broken");

These will all write messages to your console / terminal window when you run unit tests from it. On my PC, console.log writes in white, console.warn writes in a sort of yellow colour and console.error writes in red. The colours may be different on your machine but I think the principle stands for all platforms.

EDIT: The expect function is only defined when you’re in a unit test (and how it comes to be defined there when you haven’t done anything to make it so is beyond my understanding of JavaScript and certainly beyond the scope of this course). The important thing is that when you’re in your application code, the expect function is not, and should not, be defined, because it’s part of your test framework, it’s one of the things you use to verify that your code is ready to deploy into production. Once your code is in production, it doesn’t need (and shouldn’t use) the test framework, because by then the test framework has helped you to prove that your code is fit for deployment into production. Your unit tests and other tests need to know about the application that they’re testing, but the application doesn’t need to know anything about how it’s being tested or what frameworks are being used to test it, it just needs to know how to do its job.

I hope that edit clarifies more than it confuses:slightly_smiling_face:

1 Like