Jest Mocking с помощью Quasar и Typescript

Я хочу имитировать службу Amplify Auth в своем тесте. Ошибки нет, но тест не работает из-за моего макета.

Вот код, который я собираюсь протестировать:

  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));
  }

Вот тест:

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);
  });
});

person David Go    schedule 23.09.2020    source источник


Ответы (1)


Выяснилось решение, но, возможно, есть лучший промыв-обещание, чтобы имитировать вызов Amplify:

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);
  });
});
person David Go    schedule 24.09.2020