sfdx-lwc-jest
sfdx-lwc-jest copied to clipboard
Incorrect coverage when mocked apex function and copyright placed before its import
Description
First of all, my apologies if the issue description lacks enough details or it is not thorough enough as it is the first issue I report via Github. After upgrading some dependencies in the company's repo it was noticed a degradation in the function's coverage due to a new function added to the calculation that turns out to be coming from the Apex function being imported. It occurs when this function is mocked in the test class and the JS class under testing has a copyright before the import (which is mandatory in our company's policy).
Steps to Reproduce
/**
* @copyright 2023 FranM.
*/
import { getCreateComponent } from 'jest-utils';
import dummySayHi from '@salesforce/apex/CoverageIssueWithCopyrightAndApex.dummySayHi';
import coverageWithApexModal from 'c/coverageWithApexModal';
jest.mock('@salesforce/apex/CoverageIssueWithCopyrightAndApex.dummySayHi', () => ({ default: jest.fn() }), { virtual: true });
const createComponent = getCreateComponent(coverageWithApexModal, 'c-coverage-with-apex-modal');
describe('coverageWithApexModal component test', () => {
describe('apply clicked', () => {
it('controller is called', () => {
//Given
const { component } = createComponent();
const applyBtn = component.shadowRoot.querySelector('.apply-btn');
//When
applyBtn.click();
//Then
expect(dummySayHi).toHaveBeenCalledTimes(1);
});
});
});
<!-- HTML for component under test -->
<template>
<div class="slds-is-relative">
<lightning-button label="apply" class="apply-btn slds-m-left_x-small" onclick={handleSayHi}></lightning-button>
</div>
</template>
// JS for component under test
/**
* @copyright 2023 FranM.
*/
import { LightningElement } from 'lwc';
import dummySayHi from '@salesforce/apex/CoverageIssueWithCopyrightAndApex.dummySayHi';
const NAME = 'Peter';
export default class CoverageWithApex extends LightningElement {
handleSayHi() {
const result = dummySayHi(NAME);
console.log(result);
}
}
public with sharing class CoverageIssueWithCopyrightAndApex {
@AuraEnabled(cacheable=true)
public static String dummySayHi(String name) {
return 'Hi, ' + name;
}
}
const { jestConfig } = require('@salesforce/sfdx-lwc-jest/config');
const { setupFilesAfterEnv } = jestConfig;
module.exports = {
...jestConfig,
setupFilesAfterEnv: [...setupFilesAfterEnv, './jest.setup.js'],
clearMocks: true,
roots: ['<rootDir>/force-app/main'],
transformIgnorePatterns: ['/node_modules/(?!(.*@salesforce/sfdx-lwc-jest/src/lightning-stubs)/)'],
moduleDirectories: ['<rootDir>/node_modules', '<rootDir>/force-app/test/jest-utils'],
moduleNameMapper: {
'^lightning/actions$': '<rootDir>/force-app/main/common/test/jest-mocks/actions',
},
collectCoverageFrom: ['force-app/main/**/lwc/**/*.js', '!**/__tests__/**', '!**/__mocks__/**'],
coverageThreshold: {
global: {
statements: 98.84,
branches: 96.39,
functions: 99.47,
lines: 98.9,
},
},
reporters: [
'default',
[
'jest-junit',
{
suiteName: 'LWC Tests',
outputDirectory: 'test-results/jest',
outputName: './jest-result.xml',
classNameTemplate: '{classname}',
titleTemplate: '{title}',
ancestorSeparator: ' → ',
},
],
],
};
# Command to repro
npm run lwc-jest --coverage
Expected Results
Function coverage to be 100%
Actual Results
Function coverage is 66.66%
Version
- @salesforce/sfdx-lwc-jest: 1.2.1
- Node: 18.15.0
Additional context/Screenshots