Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda