Miha Šušteršič
Miha Šušteršič

Reputation: 10042

jest async test - expect.assertion seems global

I have the following unit test:

var { Mockgoose } = require("mockgoose");
var mongoose = require("mongoose");
var Transaction = require("./transaction");

var mockgoose = new Mockgoose(mongoose);

describe("transaction", function() {
  afterEach(function() {
    return mockgoose.helper.reset();
  });

  afterAll(function() {
    const { connections } = mongoose;
    const { childProcess } = mockgoose.mongodHelper.mongoBin;
    // kill mongod
    childProcess.kill();
    // close all connections
    for (const con of connections) {
      return con.close();
    }
    return mongoose.disconnect();
  });

  test("category is required", function() {
    expect.assertions(1);
    return mockgoose.prepareStorage().then(function() {
      mongoose.connect("mongodb://foobar/baz");
      return mongoose.connection.on("connected", function() {
        var mockTransaction = new Transaction({
          amount: 25,
          comment: "Gas money, Petrol.",
          tags: ["Gas", "Car", "Transport"],
          currency: "EUR"
        });
        return mockTransaction.save(function(err, savedTransaction) {
          expect(err.errors.category.properties.message).toBe(
            "Category is required."
          );
        });
      });
    });
  });

  test("category should match one of the predefined categories", function() {
    expect.assertions(1);
    return mockgoose.prepareStorage().then(function() {
      mongoose.connect("mongodb://foobar/baz");
      return mongoose.connection.on("connected", function() {
        var mockTransaction = new Transaction({
          category: "dsawdsfawfsaf",
          amount: 25,
          comment: "Gas money, Petrol.",
          tags: ["Gas", "Car", "Transport"],
          currency: "EUR"
        });
        return mockTransaction.save(function(err, savedTransaction) {
          expect(err.errors.category.properties.message).toBe(
            "{VALUE} is not a valid category."
          );
        });
      });
    });
  });
});

Now if I only run one of the tests, everything passes, but when I run both of them I get the following error:

● transaction › category should match one of the predefined categories

expect.assertions(1)

Expected one assertion to be called but received two assertion calls.

  43 | 
  44 |   test("category should match one of the predefined categories", function() {
> 45 |     expect.assertions(1);
     |            ^
  46 |     return mockgoose.prepareStorage().then(function() {
  47 |       mongoose.connect("mongodb://foobar/baz");
  48 |       return mongoose.connection.on("connected", function() {

Shouldn't each test have it's own assertions and it's own expect?

Upvotes: 4

Views: 1305

Answers (1)

Renārs Vilnis
Renārs Vilnis

Reputation: 1221

I found in my code the issue arise when I had a mix of "async" and "sync" tests that both test async functionality.

I tested an async function in a "sync way" using jest expect().resolves/rejects API which caused the issue. Had to rewrite the test in an async-await style. That solved the issue.

describe('asyncSum()', () => {
  test('"broken" sync test', () => {
    expect(asyncSum(2, 2)).resolves.toEqual(4); // This line causes the issue incorrect assertion count
  })

  test('async test resolves example', async () => {
    expect.assertions(1);

    const sum = await asyncSum(2, 2);
    expect(sum).toEqual(4);
  })

  test('async test reject example', async () => {
    expect.assertions(1);

    let error;
    try {
      await asyncSum(2);
    } catch (err) {
      error = err;
    }

    expect(error.message).toEqual('Missing 2nd parameter')
  }
})

Upvotes: 2

Related Questions