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

Error when testing a component that invokes an apex method which return value is not mocked #230

Closed
jodarove opened this issue Jun 23, 2021 · 0 comments · Fixed by #231
Closed
Assignees
Labels
bug Something isn't working

Comments

@jodarove
Copy link
Contributor

Description

A test for a component that programmatically invokes an apex method (without mocked return value) fails with the error TypeError: Cannot read property 'then' of undefined.

Steps to Reproduce

  <h1>Todos</h1>
  <ul>
      <template for:each={todos} for:item="todo">
         <li key={todo.id}>{todo.name}</li>
      </template>
  </ul>
import { LightningElement } from 'lwc';
import getTodos from '@salesforce/apex/Foo.getTodos';

export default class TodoList extends LightningElement {
    todos = [];

    connectedCallback() {
        getTodos()
                .then((result) => {
                    this.todos = result;
                });
    }
}

Test:

import { createElement } from 'lwc';
import TodoList from 'c/todo-list';

describe('c-todos', () => {
    it('should render h1', () => {
        const element = createElement('c-todo-list', {
            is: TodoList
        });
        document.body.appendChild(element);

        return Promise.resolve().then(() => {
            expect(element.shadowRoot.querySelector('h1')).not.toBeNull();
        });
    });
});

Expected Results

Test passes

Actual Results

TypeError: Cannot read property 'then' of undefined

Version

  • @salesforce/sfdx-lwc-jest: 0.12.3

(in v0.11.0 it passes)

Possible Solution
In v0.11.0, apex methods were auto-mocked to a function returning a Promise.resolve(). With 0.12.3 (#208) they are auto-mocked to a jest.fn() without return value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant