Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coverage-predicates does not work with anonymous functions #1067

Open
Eomm opened this issue Sep 26, 2023 · 0 comments
Open

coverage-predicates does not work with anonymous functions #1067

Eomm opened this issue Sep 26, 2023 · 0 comments
Labels
support Questions, discussions, and general support

Comments

@Eomm
Copy link

Eomm commented Sep 26, 2023

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/lab@25.1.3 / @hapi/code@9.0.3
  • 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 Eomm added the support Questions, discussions, and general support label Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

1 participant