sfdx-lwc-jest
sfdx-lwc-jest copied to clipboard
When sfdx-lwc-jest is installed globally, tests run with mocked Apex Method are failing
Description
When sfdx-lwc-jest is installed globally, tests run with mocked Apex Method are failing
Steps to Reproduce
From the root of a SFDX Project with LWC tests using mocked Apex Method
example of mocking code in one of the lwc/tests/*.test.js files : // Mock myMethod Apex wire adapter jest.mock( '@salesforce/apex/myController.myMethod', () => { const { createApexTestWireAdapter } = require( '@salesforce/sfdx-lwc-jest' ); return { default: createApexTestWireAdapter( jest.fn() ) }; }, { virtual: true } );
If sfdx-lwc-jest is installed globally
example of installation command-line : npm install @salesforce/sfdx-lwc-jest --location=global --omit=dev
When running the command-line "sfdx-lwc-jest", tests with mocked Apex Method are failing.
Expected Results
sfdx-lwc-jest
PASS force-app/lwc/tests/myComponentA.test.js PASS force-app/lwc/tests/myComponentB.test.js
Test Suites: 2 passed, 2 total Tests: 17 passed, 17 total Snapshots: 0 total Time: 2.45 s Ran all test suites.
Actual Results
sfdx-lwc-jest
FAIL force-app/lwc/tests/myComponentA.test.js
● c-my-component-a › myMethod @wire › renders three toasts when data returned
TypeError: myMethod.emit is not a function
57 |
58 | // Emit data from @wire
> 59 | myMethod.emit( mockGetCheckMessages );
| ^
60 |
61 | // Wait for any asynchronous DOM updates
62 | await flushPromises();
at Object.emit (force-app/lwc/__tests__/myComponentA.test.js:59:30)
● c-my-component-a › is accessible when data is returned
TypeError: myMethod.emit is not a function
101 | // Wait for any asynchronous DOM updates
102 | await flushPromises();
at Object.error (force-app/lwc/__tests__/myComponentA.test.js:99:26)
PASS force-app/lwc/tests/myComponentB.test.js
Test Suites: 1 failed, 1 passed, 2 total Tests: 3 failed, 14 passed, 17 total Snapshots: 0 total Time: 2.445 s Ran all test suites.
Version
node --version v16.17.0
sfdx version sfdx-cli/7.173.0 win32-x64 node-v16.17.1
sfdx-lwc-jest --version 1.1.3
@renatoliveira already mentioned this "global install issue" in #76
I did several tests. When sfdx-lwc-jest is installed locally (e.g. : npm install @salesforce/sfdx-lwc-jest --local --omit=dev) everything works fine.
Very important : I don't want to install sfdx-lwc-jest on each sfdx project. Or, at least, I want to install locally as few modules as possible if none is not an option.
Thanks in advance for your time & help.