Add unit tests to a LoopBack API project

Share this video with your friends

Send Tweet

In this lesson we will learn how to add tests to the project to make sure our API behaves as we expect.

After installing mocha and chai as devDependencies we will add test and test:watch scripts to our package.json. We will create a different datasource what we will use when running the tests by copying datasources.json to datasources.test.json and prefixing our test command with NODE_ENV=test.

We will verify that our tests run against an empty datasource. We’ll also add various tests to verify that our remote method and validation on the Product model behave as expected.

David
David
~ 7 years ago

@Graeme, the buy function should run well, you are passing the error as second argument when the fact of the callback is to have first argument just when there is an error and you will check it latter like:

Product.buy((err, result) => {
  if (err) throw err; // throw the error and stop the execution
 
  // You probably don't whant this line to be executed if there is an error

/** more code
}
Gush
Gush
~ 6 years ago

no (res.status) - just (res)