Jest worker遇到4个子进程异常,超过重试限制

Jest worker encountered 4 child process exceptions, exceeding retry limit

我是 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("testuser@scoutapm.com");
  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>

和打字稿文件:

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

我能得到正确方向的指示吗?或者错误可能来自哪里?

我也遇到过这种情况,this issue thread 引导我朝着正确的方向前进。尝试两件事:

  1. 您可以尝试将 --maxWorkers 2 添加到您的 jest 测试命令中。

  2. 这个错误似乎是几个问题的混合体,但未捕获的承诺拒绝是一个问题。您也可以尝试使用 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("testuser@scoutapm.com");
      const {
        signupAsUser
      } = useSignup(email);
    
      await waitFor(() => signupAsUser());
    
      expect(consoleSpy).toHaveBeenCalledWith("USER:", mockSignup);
    });
    
  3. This answer 散发更多光芒。

    Digging deeper, this is because findBy tests return a promise so the await is needed. https://testing-library.com/docs/guide-disappearance/#1-using-findby-queries It would have been nice for the library to throw a better error however.