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

Incorrect code coverage when calling from mock. #9553

Open
desout opened this issue Sep 12, 2024 · 0 comments
Open

Incorrect code coverage when calling from mock. #9553

desout opened this issue Sep 12, 2024 · 0 comments
Labels

Comments

@desout
Copy link

desout commented Sep 12, 2024

Describe the bug

During testing, I found an issue where if you use an exported function inside mocks and it is triggered by mocks, the function's name will not be covered, but the body of the function will be. Logically, since this function has been called, it should be covered as well.

Input code

import {TestClass} from './main'
import dependency from "./dependency";
let eventMap = {};
jest.mock('./dependency', () => {
  return {
    default: {
      on: jest.fn((eventNames, handler) => {
        eventNames.split(' ').forEach(eventName => {
          eventMap[eventName] = handler;
        });
      }),
      fire: jest.fn((eventName, data = {}, target = undefined) => {
        if (eventMap[eventName]) {
          eventMap[eventName]({
            detail: {
              ...data,
              ts: data.ts || Date.now(),
            },
            target,
          });
        }
      })
    },
    __esModule: true,
  };
});

test('runDependency()',() => {
  const testClass = new TestClass();

  testClass.triggerEvent();

  expect(dependency.fire).toHaveBeenCalledTimes(1)
});

Config

{
  "$schema": "https://swc.rs/schema.json",
  "jsc": {
    "target": "es2022",
    "loose": true,
    "parser": {
      "syntax": "ecmascript",
      "dynamicImport": true
    }
  },
  "module": {
    "type": "commonjs"
  }
}

Playground link (or link to the minimal reproduction)

https://github.com/desout/jest-swc-coverage-issue

SWC Info output

Operating System:
    Platform: darwin
    Arch: arm64
    Machine Type: arm64
    Version: Darwin Kernel Version 24.1.0: Fri Aug 16 19:18:30 PDT 2024; root:xnu-11215.40.42~4/RELEASE_ARM64_T6000
    CPU: (8 cores)
        Models: Apple M1 Pro

Binaries:
    Node: 20.13.1
    npm: 10.5.2
    Yarn: 1.22.19
    pnpm: 9.1.3

Relevant Packages:
    @swc/core: 1.7.14
    @swc/helpers: N/A
    @swc/types: 0.1.12
    

SWC Config:
    output: N/A
    .swcrc path: N/A

Next.js info:
    output: N/A

Expected behavior

export function should be covered

Actual behavior

image

Version

1.7.14

Additional context

No response

@desout desout added the C-bug label Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant