lab icon indicating copy to clipboard operation
lab copied to clipboard

coverage-predicates does not work with anonymous functions

Open Eomm opened this issue 9 months ago • 0 comments

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: v18.x
  • module version with issue: @hapi/[email protected] / @hapi/[email protected]
  • last module version without issue: NA
  • environment (e.g. node, browser, native): node.js
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information: using typescript

What are you trying to achieve or the steps to reproduce?

1 Create a bug.ts file with the content:

bug.ts file
class Operation {
  a: number;
  b: number;

  constructor(a, b) {
    this.a = a;
    this.b = b;
  }

  public calculate(funcA, funcB) {
    // $lab:coverage:off$
    return process.env.WHAT === 'sum' ? funcA(this.a, this.b) : funcB(this.a, this.b);
    // $lab:coverage:on$
  }
}

export default function fooBar(a, b) {
  const c = new Operation(a, b);

  return c.calculate(
    // $lab:coverage:off$ $not:isSum$
    (a, b) => {
      return a + b;
    },
    // $lab:coverage:on$
    // $lab:coverage:off$ $not:isMulti$
    (a, b) => {
      return a * b;
    },
    // $lab:coverage:on$
  );
}

2 Create a bug.test.ts file with:

bug.test.ts file
import * as Lab from '@hapi/lab';
import * as Code from '@hapi/code';

const { describe, it } = (exports.lab = Lab.script());
const expect = Code.expect;

import fun from './bug';

describe('Foo', () => {
  it('operation', async () => {
    const result = fun(2, 2);
    expect(result).to.equal(4);
  });
});

3 Create a .labrc.js file with the content:

.labrc.js file
module.exports = {
  typescript: true,
  assert: '@hapi/code',
  'coverage-predicates': {
    isSum: process.env.WHAT === 'sum',
    isMulti: process.env.WHAT !== 'sum',
  }
};

What was the result you got?

If I run the code with:

WHAT=sum lab --threshold 100 --verbose bug.test.ts

I get:

Foo
  ✔ 1) operation (4 ms and 1 assertions)


1 tests complete
Test duration: 5 ms
Assertions count: 1 (verbosity: 1.00)
Leaks: No issues
Coverage: 96.77% (1/31)
bug.ts missing coverage from file(s):
        bug.ts on line(s): 28
Code coverage below threshold: 96.77 < 100

What result did you expect?

I would expect a 100% coverage thanks to the coverage-predicates configuration.

Note that if I change the bug.ts > fooBar to a named functions like this:

export default function fooBar(a, b) {
  const c = new Operation(a, b);

  // $lab:coverage:off$ $not:isSum$
  function sum(a, b) {
    return a + b;
  }
  // $lab:coverage:on$

  // $lab:coverage:off$ $not:isMulti$
  function multi(a, b) {
    return a * b;
  }
  // $lab:coverage:on$

  return c.calculate(sum, multi);
}

With the same command I get:

> lab --threshold 100 --verbose bug.test.ts

Foo
  ✔ 1) operation (4 ms and 1 assertions)


1 tests complete
Test duration: 4 ms
Assertions count: 1 (verbosity: 1.00)
Leaks: No issues
Coverage: 100.00%

Eomm avatar Sep 26 '23 10:09 Eomm