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

Update @salesforce/apex/* imports to return jest.fn() #260

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/@lwc/jest-preset/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,36 @@ Then, create a new test file in `__tests__` that follows the naming convention `

Now you can write and run the Jest tests!

#### @Salesforce/apex/ Method Mocks

Imports of `@Salesforce/apex/*` automatically resolve to `jest.fn()`, these can be optionally overwritten.

```js
import apexMethod from '@Salesforce/apex/apexClass.apexMethod';

it('test apex cal', async () => {
apexMethod.mockResolvedValue({ foo: 'bar' });
});
```

Optional set function for method manually

```js
import apexMethod from '@Salesforce/apex/apexClass.apexMethod';

jest.mock(
'@salesforce/apex/apexClass.apexMethod',
() => ({
default: jest.fn(),
}),
{ virtual: true },
);

it('test apex callout', async () => {
apexMethod.mockResolvedValue({ foo: 'bar' });
});
```

### Custom matchers

This package contains convenience functions to help test web components, including Lightning Web Components.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ describe('@salesforce/apexContinuation import', () => {
import myMethod from '@salesforce/apexContinuation/FooController.fooMethod';
`,
`
import { jest } from '@jest/globals';
let myMethod;

try {
myMethod = require("@salesforce/apexContinuation/FooController.fooMethod").default;
} catch (e) {
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || function myMethod() {
return Promise.resolve();
};
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || jest.fn(Promise.resolve());

myMethod = global.__lwcJestMock_myMethod;
}
Expand All @@ -35,14 +34,13 @@ describe('@salesforce/apexContinuation import', () => {
`,
`
import { otherNamed } from './something-valid';
import { jest } from '@jest/globals';
let myMethod;

try {
myMethod = require("@salesforce/apexContinuation/FooController.fooMethod").default;
} catch (e) {
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || function myMethod() {
return Promise.resolve();
};
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || jest.fn(Promise.resolve());

myMethod = global.__lwcJestMock_myMethod;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ describe('@salesforce/apex import', () => {
import myMethod from '@salesforce/apex/FooController.fooMethod';
`,
`
import { jest } from '@jest/globals';
let myMethod;

try {
myMethod = require("@salesforce/apex/FooController.fooMethod").default;
} catch (e) {
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || function myMethod() {
return Promise.resolve();
};
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || jest.fn(Promise.resolve());

myMethod = global.__lwcJestMock_myMethod;
}
Expand All @@ -35,14 +34,13 @@ describe('@salesforce/apex import', () => {
`,
`
import { otherNamed } from './something-valid';
import { jest } from '@jest/globals';
let myMethod;

try {
myMethod = require("@salesforce/apex/FooController.fooMethod").default;
} catch (e) {
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || function myMethod() {
return Promise.resolve();
};
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || jest.fn(Promise.resolve());

myMethod = global.__lwcJestMock_myMethod;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/@lwc/jest-transformer/src/transforms/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ function stringScopedImportTransform(t, path, importIdentifier, fallbackData) {
* shared.
*/
const resolvedPromiseTemplate = babelTemplate(`
import { jest } from '@jest/globals';
let RESOURCE_NAME;
try {
RESOURCE_NAME = require(IMPORT_SOURCE).default;
} catch (e) {
global.MOCK_NAME = global.MOCK_NAME || function RESOURCE_NAME() { return Promise.resolve(); };
global.MOCK_NAME = global.MOCK_NAME || jest.fn(Promise.resolve());
RESOURCE_NAME = global.MOCK_NAME;
}
`);
Expand Down
13 changes: 9 additions & 4 deletions test/src/modules/transformer/apex/__tests__/apex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
import { ApexMethod, refreshApex, getSObjectValue } from '../apex';

describe('@salesforce/apex/<class>', () => {
it('exports a default method returning a promise', () => {
expect(ApexMethod()).resolves.toEqual(undefined);
it('exports a default method returning a promise', async () => {
expect(ApexMethod).not.toHaveBeenCalled();

ApexMethod.mockResolvedValue('bar');
const result = await ApexMethod('foo');
expect(ApexMethod).toHaveBeenCalledWith('foo');
expect(result).toBe('bar');
});
});

Expand All @@ -18,9 +23,9 @@ describe('@salesforce/apex', () => {
});

it('exports getSObjectValue method returning a jest.fn()', () => {
expect(getSObjectValue).not.toBeCalled();
expect(getSObjectValue).not.toHaveBeenCalled();

getSObjectValue('foo');
expect(getSObjectValue).toBeCalledWith('foo');
expect(getSObjectValue).toHaveBeenCalledWith('foo');
});
});