Jest Test failing because it can't resolve stub
AllanOricil opened this issue ยท comments
Summary
I have an LWC that is used on a Quick Action and I can't test it because of this error
I followed exactly your example for testing LWC used in a Quick Action and it did not work. These are my dev dependencies:
"devDependencies": {
"@prettier/plugin-xml": "^0.12.0",
"@sa11y/jest": "^0.4.3",
"@salesforce/eslint-config-lwc": "^0.7.0",
"@salesforce/eslint-plugin-aura": "^1.4.0",
"@salesforce/sfdx-lwc-jest": "^1.0.1",
"eslint": "^7.6.0",
"eslint-config-prettier": "^6.11.0",
"husky": "^4.2.1",
"lint-staged": "^10.0.7",
"prettier": "2.3.2",
"prettier-plugin-apex": "1.10.0",
"xml-js": "^1.6.11",
"fs-extra": "^10.0.0",
"simple-git": "^2.45.1"
},
and this is my Node Version: 14.17.3
Salesforce Org Type
Scratch Org
Steps To Reproduce
1 - Create a LWC to use in a Quick Action
2 - Create a Jest test following this recipe https://github.com/trailheadapps/lwc-recipes/blob/main/force-app/main/default/lwc/editRecordScreenAction/__tests__/editRecordScreenAction.test.js
3 - Run the test and verify you get the same error I showed in the image
Current Behavior
Test can't start because it cant find a stub
Expected Behavior
Test should run and the stub should be resolved
Relevant Log Output
`
Executing task: c:\Users\allan_000\workspace\vodafone\sf-metadata\node_modules\.bin\lwc-jest -- --json --outputFile c:\Users\allan_000\workspace\vodafone\sf-metadata\.sfdx\tools\testresults\lwc\test-result-b5707f0a-a4b2-437d-9c4a-401dc7f00dc5.json --testLocationInResults --runTestsByPath salesforce_sfdx\main\default\lwc\dspRequestSignature\__tests__\dspRequestSignature.test.js --testNamePattern "it should render custom labels" <
FAIL salesforce_sfdx/main/default/lwc/dspRequestSignature/__tests__/dspRequestSignature.test.js
โ Test suite failed to run
Cannot find module 'lightning/actions' from 'salesforce_sfdx/main/default/lwc/dspRequestSignature/dspRequestSignature.js'
Require stack:
salesforce_sfdx/main/default/lwc/dspRequestSignature/dspRequestSignature.js
salesforce_sfdx/main/default/lwc/dspRequestSignature/__tests__/dspRequestSignature.test.js
2 | import fetchContentDocumentLinks from '@salesforce/apex/DSP_RequestSignatureActionController.fetchContentDocumentLinks';
3 | import createDocumentSigningRequest from '@salesforce/apex/DSP_RequestSignatureActionController.createDocumentSigningRequest';
> 4 | import { CloseActionScreenEvent } from 'lightning/actions';
| ^
5 | import { ShowToastEvent } from 'lightning/platformShowToastEvent';
6 |
7 | import requestSignatureHeader from '@salesforce/label/c.dsp_requestSignatureHeader';
at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:322:11)
at Object.<anonymous> (salesforce_sfdx/main/default/lwc/dspRequestSignature/dspRequestSignature.js:4:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 2.491 s
Ran all test suites within paths "salesforce_sfdx\main\default\lwc\dspRequestSignature\__tests__\dspRequestSignature.test.js".
Test results written to: .sfdx\tools\testresults\lwc\test-result-b5707f0a-a4b2-437d-9c4a-401dc7f00dc5.json
The terminal process "cmd.exe /d /c c:\Users\allan_000\workspace\vodafone\sf-metadata\node_modules\.bin\lwc-jest -- --json --outputFile c:\Users\allan_000\workspace\vodafone\sf-metadata\.sfdx\tools\testresults\lwc\test-result-b5707f0a-a4b2-437d-9c4a-401dc7f00dc5.json --testLocationInResults --runTestsByPath salesforce_sfdx\main\default\lwc\dspRequestSignature\__tests__\dspRequestSignature.test.js --testNamePattern "it should render custom labels"" terminated with exit code: 1.
Terminal will be reused by tasks, press any key to close it.
`
Code of Conduct
- I agree to follow this project's Code of Conduct
Welcome! ๐
Thank you for posting this issue. ๐๐ผโโ๏ธ We will come back to you latest within the next 48h (working days). Stay tuned!
So I finally understood what I was missing. To solve this problem you have to add this folder to your project
https://github.com/trailheadapps/lwc-recipes/tree/main/force-app/test/jest-mocks
it contains all the mocks you will need.
Then, in the root of your project, you have to add these configs on your jest.config.js file. This will tell jest where to look for the mocks :D
moduleNameMapper: {
'^@salesforce/apex$': '<rootDir>/salesforce_sfdx/test/jest-mocks/apex',
'^@salesforce/schema$': '<rootDir>/salesforce_sfdx/test/jest-mocks/schema',
'^lightning/navigation$':
'<rootDir>/salesforce_sfdx/test/jest-mocks/lightning/navigation',
'^lightning/platformShowToastEvent$':
'<rootDir>/salesforce_sfdx/test/jest-mocks/lightning/platformShowToastEvent',
'^lightning/uiRecordApi$':
'<rootDir>/salesforce_sfdx/test/jest-mocks/lightning/uiRecordApi',
'^lightning/messageService$':
'<rootDir>/salesforce_sfdx/test/jest-mocks/lightning/messageService',
'^lightning/actions$':
'<rootDir>/salesforce_sfdx/test/jest-mocks/lightning/actions'
},
Obs: dont forget that the location of the mocks will be different on each project. Mine is on a folder called "salesforce_sfdx"