Skip to content

Commit

Permalink
@W-16845391 Revert the breaking change for jest.fn() (#315)
Browse files Browse the repository at this point in the history
* More cleanup

* README.md cleanup

---------

Co-authored-by: lturanscaia <[email protected]>
  • Loading branch information
Shinoni and lesya7 authored Sep 26, 2024
1 parent 6db799b commit 4917996
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 49 deletions.
30 changes: 0 additions & 30 deletions packages/@lwc/jest-preset/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,36 +110,6 @@ 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,13 +13,14 @@ 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 || jest.fn(Promise.resolve());
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || function myMethod() {
return Promise.resolve();
};
myMethod = global.__lwcJestMock_myMethod;
}
Expand All @@ -34,13 +35,14 @@ 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 || jest.fn(Promise.resolve());
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || function myMethod() {
return Promise.resolve();
};
myMethod = global.__lwcJestMock_myMethod;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ 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 || jest.fn(Promise.resolve());
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || function myMethod() {
return Promise.resolve();
};
myMethod = global.__lwcJestMock_myMethod;
}
Expand All @@ -34,13 +35,14 @@ 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 || jest.fn(Promise.resolve());
global.__lwcJestMock_myMethod = global.__lwcJestMock_myMethod || function myMethod() {
return Promise.resolve();
};
myMethod = global.__lwcJestMock_myMethod;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/@lwc/jest-transformer/src/transforms/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ 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 || jest.fn(Promise.resolve());
global.MOCK_NAME = global.MOCK_NAME || function RESOURCE_NAME() { return Promise.resolve(); };
RESOURCE_NAME = global.MOCK_NAME;
}
`);
Expand Down
13 changes: 4 additions & 9 deletions test/src/modules/transformer/apex/__tests__/apex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
import { ApexMethod, refreshApex, getSObjectValue } from '../apex';

describe('@salesforce/apex/<class>', () => {
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');
it('exports a default method returning a promise', () => {
expect(ApexMethod()).resolves.toEqual(undefined);
});
});

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

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

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

0 comments on commit 4917996

Please sign in to comment.