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

When sfdx-lwc-jest is installed globally, tests run with mocked Apex Method are failing #293

Open
RemyVULTAGGIO opened this issue Oct 24, 2022 · 1 comment

Comments

@RemyVULTAGGIO
Copy link

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.

@RemyVULTAGGIO
Copy link
Author

RemyVULTAGGIO commented Oct 25, 2022

In addition, when sfdx-lwc-jest is installed globally, the "jest.config.js" file is not correctly interpreted.

sfdx-lwc-jest

Error: Cannot find module '@salesforce/sfdx-lwc-jest/config'
Require stack:

  • C:\mySFDXProject\jest.config.js
  • C:\Users\aUser\AppData\Roaming\npm\node_modules@salesforce\sfdx-lwc-jest\node_modules\jest-util\build\requireOrImportModule.js
  • C:\Users\aUser\AppData\Roaming\npm\node_modules@salesforce\sfdx-lwc-jest\node_modules\jest-util\build\index.js
  • C:\Users\aUser\AppData\Roaming\npm\node_modules@salesforce\sfdx-lwc-jest\node_modules\jest-config\build\getCacheDirectory.js
  • C:\Users\aUser\AppData\Roaming\npm\node_modules@salesforce\sfdx-lwc-jest\node_modules\jest-config\build\Defaults.js
  • C:\Users\aUser\AppData\Roaming\npm\node_modules@salesforce\sfdx-lwc-jest\node_modules\jest-config\build\normalize.js
  • C:\Users\aUser\AppData\Roaming\npm\node_modules@salesforce\sfdx-lwc-jest\node_modules\jest-config\build\index.js
  • C:\Users\aUser\AppData\Roaming\npm\node_modules@salesforce\sfdx-lwc-jest\node_modules\jest-cli\build\init\index.js
  • C:\Users\aUser\AppData\Roaming\npm\node_modules@salesforce\sfdx-lwc-jest\node_modules\jest-cli\build\run.js
  • C:\Users\aUser\AppData\Roaming\npm\node_modules@salesforce\sfdx-lwc-jest\node_modules\jest-cli\build\index.js
  • C:\Users\aUser\AppData\Roaming\npm\node_modules@salesforce\sfdx-lwc-jest\node_modules\jest-cli\bin\jest.js
  • C:\Users\aUser\AppData\Roaming\npm\node_modules@salesforce\sfdx-lwc-jest\node_modules\jest\bin\jest.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Function.Module._load (node:internal/modules/cjs/loader:804:27)
    at Module.require (node:internal/modules/cjs/loader:1028:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object. (C:\Lokahi\sources\lokahi-isv\jest.config.js:1:24)
    at Module._compile (node:internal/modules/cjs/loader:1126:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
    at Module.load (node:internal/modules/cjs/loader:1004:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Module.require (node:internal/modules/cjs/loader:1028:19)

Here too, when sfdx-lwc-jest is installed locally everything works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant