From 125c68992acc3af85946c4f93c0cfd1985c85cfa Mon Sep 17 00:00:00 2001 From: Jose David Rodriguez Velasco Date: Tue, 30 Mar 2021 16:15:40 -0700 Subject: [PATCH] fix: automock apex methods with valid wire adapters (#208) --- .eslintrc | 2 +- src/apex-stubs/method/method.js | 9 +++++++++ src/resolver.js | 22 +++++++++++++++++++--- 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/apex-stubs/method/method.js diff --git a/.eslintrc b/.eslintrc index bd3938b7..3cfb4463 100644 --- a/.eslintrc +++ b/.eslintrc @@ -14,7 +14,7 @@ }, "overrides": [ { - "files": ["src/lightning-stubs/**"], + "files": ["src/lightning-stubs/**", "src/apex-stubs/**"], "parser": "babel-eslint", "parserOptions": { "sourceType": "module" diff --git a/src/apex-stubs/method/method.js b/src/apex-stubs/method/method.js new file mode 100644 index 00000000..072a3de4 --- /dev/null +++ b/src/apex-stubs/method/method.js @@ -0,0 +1,9 @@ +/* + * Copyright (c) 2021, salesforce.com, inc. + * All rights reserved. + * SPDX-License-Identifier: MIT + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT + */ +import { createApexTestWireAdapter } from '@salesforce/wire-service-jest-util'; + +export default createApexTestWireAdapter(jest.fn()); diff --git a/src/resolver.js b/src/resolver.js index 43008852..bf72ff1e 100644 --- a/src/resolver.js +++ b/src/resolver.js @@ -47,13 +47,21 @@ function resolveAsFile(name, extensions) { return undefined; } -function getLightningMock(modulePath) { - const p = path.join(__dirname, 'lightning-stubs', modulePath); +function getModuleMock(modulePath, moduleName) { + const p = path.join(__dirname, modulePath, moduleName); if (fs.existsSync(p)) { - return path.join(p, modulePath + '.js'); + return path.join(p, moduleName + '.js'); } } +function getLightningMock(moduleName) { + return getModuleMock('lightning-stubs', moduleName); +} + +function getApexMock(moduleName) { + return getModuleMock('apex-stubs', moduleName); +} + function getModule(modulePath, options) { const { ns, name } = getInfoFromId(modulePath); @@ -61,6 +69,14 @@ function getModule(modulePath, options) { return getLightningMock(name); } + // See https://developer.salesforce.com/docs/component-library/documentation/en/lwc/reference_salesforce_modules + if ( + modulePath.startsWith('@salesforce/apex/') || + modulePath.startsWith('@salesforce/apexContinuation/') + ) { + return getApexMock('method'); + } + if (ns === DEFAULT_NAMESPACE) { const paths = getModulePaths(); for (let i = 0; i < paths.length; i++) {