Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda