salesforce / lightning-labs

MIT License
3 stars 4 forks source link

Add support for partial ES module mocks #5

Closed divmain closed 1 month ago

divmain commented 1 month ago

When mocking an ES module, one of the following two statements must be true:

  1. the module reference can be resolved to an existing ES module, or
  2. the module reference cannot be resolved to an existing ES module.

Examples of scenario one:

If the module resolution algorithm cannot resolve the mock reference, then the mock falls into scenario two. This might happen if, for example, an LWC references a @salesforce scoped module that is only present when running on the Salesforce platform.

In scenario two, you can declare whatever mocks that you expect would be present on platform. For example, a component might have the following code:

import getTimeZone from '@salesforce/schema/UserInfo.getTimeZone';

In a test file, this might be mocked like so:

import timeZoneMock from 'mock{default}:@salesforce/schema/UserInfo.getTimeZone';

In other words, if the module reference is not resolvable, it is up to the test-author to ensure that the mocks are sufficient for their consumption in component code.

However, if the module reference is resolved correctly (scenario one), the system currently requires that the test author mock all exports in that resolved module. For example, if the real ES module exported default and somethingElse, the corresponding mock would have to be declared like so: import mock from 'mock{default,somethingElse}:...';.

This shouldn't be required. It should be possible to only mock default or somethingElse, and the non-mocked exports should be importable as normal.