網站搜尋

typescript - Jest 工作程序遇到 4 個子進程例外,超過重試限制


我對 Vue 和 Jest 測試還很陌生,當我運行特定測試時,我一直收到這個錯誤。我明白這是一個通用錯誤,但我不確定如何深入查找並弄清楚問題出在哪裡。

錯誤如下:

 Test suite failed to run

    Jest worker encountered 4 child process exceptions, exceeding retry limit

      at ChildProcessWorker.initialize (node_modules/jest-worker/build/workers/ChildProcessWorker.js:185:21)

這是失敗的測試:

test("signupAsUser logs results if email is provided", async () => {
  const consoleSpy = jest.spyOn(console, "log");
  const email = ref("[email ");
  const { signupAsUser } = useSignup(email);

  await signupAsUser();

  expect(consoleSpy).toHaveBeenCalledWith("USER:", mockSignup);
});

這裡是正在測試的檔案。Vue 檔案:

<!--
  View for user signup operations.
-->
<template lang="pug">
.Signup
    .Signup__focus
        .Signup__title Sign Up
            .Signup__form
                .Signup__field
                    va-input.Signup__emailInput(
                       type="email",
                      name="email",
                      placeholder="Email",
                      v-model="email",
                      @keyup.enter="signupAsUser()"
                    )
                        template(v-slot:prependInner="")
                            va-icon(name="email")
                    .Login__buttonRow
                        va-button.Login__submitButton(@click="signupAsUser") Sign Up
</template>

<script lang="ts">
import { ref, defineComponent } from "vue";
import useSignup from "@/views/Signup/useSignup";

/**
 * Assemble the Signup component
 *
 *  @returns Data for the component to use.
 * - email: of the user to sign up with
 * - signupAsUser: function to call to carry out the login operation.
 */
function setup() {
  const email = ref("");
  const { signupAsUser } = useSignup(email);

  return {
    email,
    signupAsUser,
  };
}

export default defineComponent({
  name: "Signup",
  setup,
});
</script>

<style lang="scss">
//basic scss style taken from Login.vue until button and verification code is added
.Signup {
  position: fixed;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;

  &__focus {
    width: 360px;
    max-width: 95vw;
  }

  &__field {
    padding-bottom: 0.5em;
  }

  &__title {
    font-size: 1.2em;
    padding-bottom: 0.5em;
    text-align: center;
  }
}
</style>

以及 TypeScript 文件:

import { Ref } from "vue";
import { useApolloClient } from "@vue/apollo-composable";
import { ValidatedUser } from "@/models";
import { gql } from "graphql-tag";
import router from "@/router";

const query = gql`
  query Signup($input: Signup) {
    signup(input: $input) {
      __typename
      token
      user {
        emailAddress
        id
      }
    }
  }
`;

/**
 * Retrive apollo client and provide useSignup
 * function to validate input and execute Signup process.
 *
 * @param emailAddress - reactively wrapped emailAddress address of the user signing up.
 * @returns useSignup composition functionality.
 */
export default function useSignup(emailAddress: Ref<string>): {
  signupAsUser: () => Promise<void>;
} {
  const { resolveClient } = useApolloClient();
  /**
   * Execute the Signup process for the specified user values.
   */
  /**
   *
   */
  async function signupAsUser(): Promise<void> {
    console.log("emailAddress " + emailAddress.value);
    if (emailAddress.value.length < 5) {
      console.log("here");
      return;
    } else {
      const client = resolveClient();

      const variables = {
        input: { username: emailAddress.value },
      };
      // const response = await client.query({query, variables});
      console.log("here");
      // const validatedUser: ValidatedUser = response.data.signup;
      // console.log("USER:", validatedUser);
      console.log("emailAddress: ", variables);
    }
    router.push({ path: "/signup/verify" });
  }

  return { signupAsUser };
}

我能得到一些指引,告訴我在哪裡超時了嗎?或者錯誤可能來自哪裡?

答案#1

此錯誤隱藏了您測試或程式碼中的真正問題。您可以串行執行測試以顯示測試中的實際錯誤 jest --runInBand

答案#2

我也有過這種經歷,而這個問題討論串引導我走向正確的方向。有兩件事可以嘗試:

  1. 你可以嘗試在你的 jest 測試命令中加入 --maxWorkers 2。

  2. 這個錯誤似乎是幾個問題的混合,但未捕捉的 promise 拒絕是其中一個原因。你也可以嘗試使用 waitFor,看看這是否有幫助。

    import { waitFor } from 'test/test-utils'
    
    test("signupAsUser logs results if email is provided", async() => {
      const consoleSpy = jest.spyOn(console, "log");
      const email = ref("[email ");
      const {
        signupAsUser
      } = useSignup(email);
    
      await waitFor(() => signupAsUser());
    
      expect(consoleSpy).toHaveBeenCalledWith("USER:", mockSignup);
    });
    
  3. 這個答案提供了更多的啟示。

    深入探討,這是因為 findBy 測試返回一個 promise,所以需要使用 await。https://testing-library.com/docs/guide-disappearance/#1-using-findby-queries 不過,如果這個庫能拋出更好的錯誤會更好。

答案#3

測試套件無法運行。Jest 工作程序遇到 4 個子進程異常,超過了重試限制

我在 CI 測試中遇到了完全相同的錯誤訊息。它影響了我所有的測試案例。這個訊息掩蓋了真正的問題。你必須將 maxWorkers 改為 1,才能在單執行緒上看到問題。然後你就會看到錯誤的原因,這有助於你解決問題。