Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
How to fail a test if no error is thrown in jest.js?

i'm currently improving the branch/function coverage of our feathers.js/node.js api (testing with jest). Currently there is a service with a property which should only accept certain values, which is not implemented yet.

Valid values would be something like:

const validValues = ["System", "Engineering", "Production"]

If one of the values is provided, the api should accept the request and return a valid response.

If a value like

const invalidValue = ["Some", "Invalid", "Value"]

is provided, the API should reject the request.

Since value validation is not implemented yet, the idea was to implement a test which fails if invalid values like which are accepted by the api and it is ensured that the api only accepts valid values.

it("test service for invalid values", async () => {
    const invalidValues = ["Some", "Invalid", "Value"];

    invalidValues.map(async (invalidValue) => {
      await expect(async () => {
        await app.service("release-types").create({
          someProperty: "some Value"
          propertyWithValueConstraint: invalidValue,
        });
      }).rejects.toThrow();
    });
  });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is that [].map does not run asynchronously and therefore does not catch the assertions. You have two options:

  1. Use await Promise.all(promises):
it("test service for invalid values", async () => {
  const invalidValues = ["Some", "Invalid", "Value"];

  await Promise.all(invalidValues.map(async (invalidValue) => {
    await expect(async () => {
      await app.service("release-types").create({
        someProperty: "some Value"
        propertyWithValueConstraint: invalidValue,
      });
    }).rejects.toThrow();
  }));
});
  1. Use a for ... of loop
it("test service for invalid values", async () => {
  const invalidValues = ["Some", "Invalid", "Value"];

  for (const invalidValue of invalidValues) {
    await expect(async () => {
      await app.service("release-types").create({
        someProperty: "some Value"
        propertyWithValueConstraint: invalidValue,
      });
    }).rejects.toThrow();
  }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!