0

I want to mock Amplify Auth service in my test. There is no error, but test doesn't work because of my mock.

Here is the code I'm about to test:

  signIn(): void {
    if (!this.valid) return;
    this.loading = 1;
    this.$Auth
      .signIn(this.email, this.password)
      .then(() => this.$router.push({ name: "homeManagement" }))
      .catch((err: any) => (this.errorMessage = err.message))
      .finally(() => (this.loading = 0));
  }

Here is the test:

const $t = jest.fn();
$t.mockReturnValue("");

const $Auth = jest.fn();
$Auth.mockReturnValue({
  code: "UserNotFoundException",
  name: "UserNotFoundException",
  message: "User does not exist."
});


const factory = mountFactory(LoginForm, {
  mount: {
    mocks: {
      $Auth
    }
  }
});

describe("LoginForm", () => {
  it("User not found", async () => {
    const wrapper = factory();

    await wrapper.setData({
      email: "[email protected]",
      password: "Qwer321"
    });
    await wrapper.vm.signIn();
    expect(wrapper.vm.$data.errorMessage.length).not.toEqual(0);
  });
});

1 Answer 1

1

Figured out a solution, but maybe there is a better one flush-promises to mock Amplify call:

const $Auth = jest.fn();
$Auth.signIn = () => Promise.resolve();

describe("LoginForm", () => {
it("User does not exist", async () => {
const wrapper = factory();

await wrapper.setData({
  email: "[email protected]",
  password: "Qwer321",
  valid: true
});

await wrapper.vm.signIn();
await flushPromises();
expect(wrapper.vm.$data.errorMessage.length).not.toEqual(0);
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.