Skip to content

Add support for @wire mocks #6

@divmain

Description

@divmain

Currently, there is no ergonomic way for a test author to mock the behavior of an @wire dependency. A mechanism should be built so that the test environment can directly call or modify the state of a mock, on a per-test basis. Once this is in place, an additional layer should be built on top of the first to enable a test to modify the behavior of a wire.

Example component code:

import { LightningElement, api, wire } from "lwc";
import { getRecord } from "lightning/uiRecordApi";
import ACCOUNT_NAME_FIELD from "@salesforce/schema/Account.Name";

export default class Record extends LightningElement {
  @api recordId;

  @wire(getRecord, { recordId: "$recordId", fields: [ACCOUNT_NAME_FIELD] })
  record;
}

Example test code:

import recordApiMock from 'mock{getRecord}:lightning/uiRecordApi';
import mockWire from '@lwc/test-runner';

describe(() => {
  beforeEach(async () => {
    await recordApiMock.reset();
  });

  it('should be possible to mock the wire value', async () => {
    const recordApiWire = await mockWire(recordApiMock, 'getRecord');
    await recordApiWire.setWiredData({
      Name: {
        value: 'foo'
      }
    });
    // Test the component's subsequent behavior.
  });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions