salesforce / sfdx-lwc-jest

Run Jest against LWC components in SFDX workspace environment
MIT License
162 stars 81 forks source link

Cannot Find `lightning/toast` Module #352

Open freddysilber opened 5 months ago

freddysilber commented 5 months ago

Description

When running unit tests using the new `lightning/toast' module, I get an error.

Cannot find module 'lightning/toast' from 'path/to/lightningComponent/js'

Steps to Reproduce

// JS for component under test
import { LightningElement } from 'lwc';
import Toast from 'lightning/toast';

export default class Foo extends LightningElement {
  showToast() {
    Toast.show(
      {
        label: 'Error!',
        message: 'There was an error getting data!',
        mode: 'sticky',
        variant: 'error',
      },
      this
    );
  }
}
# Command to repro
sfdx-lwc-jest -- --no-cache

Expected Results

Test to pass

Actual Results

Cannot find module 'lightning/toast' from 'path/to/lightningComponent/js'

Version

Possible Solution

Add a new stub for the toast module

freddysilber commented 5 months ago

I'm happy to submit a PR for this

Mrgagne commented 3 months ago

@freddysilber You might need to add it to a custom jest.config.js file, similar to this. https://github.com/trailheadapps/lwc-recipes/blob/main/jest.config.js