@jest/globals#afterAll TypeScript Examples

The following examples show how to use @jest/globals#afterAll. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: logInOrBypass.test.ts    From slice-machine with Apache License 2.0 5 votes vote down vote up
afterAll(() => {
  return nock.restore();
});
Example #2
Source File: communication.test.ts    From slice-machine with Apache License 2.0 5 votes vote down vote up
afterAll(() => {
  return nock.restore();
});
Example #3
Source File: cts.spec.tsx    From slice-machine with Apache License 2.0 5 votes vote down vote up
afterAll(() => server.close());
Example #4
Source File: slices.spec.tsx    From slice-machine with Apache License 2.0 5 votes vote down vote up
afterAll(() => server.close());
Example #5
Source File: communication.test.ts    From slice-machine with Apache License 2.0 4 votes vote down vote up
describe("communication", () => {
  afterEach(() => {
    jest.clearAllMocks();
  });

  afterAll(() => {
    return nock.restore();
  });

  const fakeCookie = "prismic-auth=biscuits;";
  test("validateSession, default base", async () => {
    const responseData = {
      email: "[email protected]",
      type: "USER",
      repositories: {
        "foo-repo": { dbid: "abcd", role: Roles.OWNER },
        qwerty: { dbid: "efgh", role: Roles.WRITER },
      },
    };
    nock("https://auth.prismic.io")
      .get("/validate?token=biscuits")
      .reply(200, responseData);

    return communication.validateSession(fakeCookie).then((data) => {
      expect(data).toEqual(responseData);
    });
  });

  test("validateSession, custom base", async () => {
    const responseData = {
      email: "[email protected]",
      type: "USER",
      repositories: {
        "foo-repo": { dbid: "abcd", role: Roles.OWNER },
        qwerty: { dbid: "efgh", role: Roles.WRITER },
      },
    };
    nock("https://auth.wroom.io")
      .get("/validate?token=biscuits")
      .reply(200, responseData);

    return communication
      .validateSession(fakeCookie, "https://wroom.io")
      .then((data) => {
        expect(data).toEqual(responseData);
      });
  });

  test("refreshSession", async () => {
    const token = "biscuits";
    const base = "https://prismic.io";
    const wanted = "some-new-token";
    nock("https://auth.prismic.io")
      .get(`/refreshtoken?token=${token}`)
      .reply(200, wanted);
    const result = await communication.refreshSession(
      `prismic-auth=${token}`,
      base
    );
    expect(result).toEqual(wanted);
  });

  test("listRepositories", async () => {
    const responseData = [
      { domain: "foo-repo", name: "foo-repo", role: "Administrator" },
      { domain: "qwerty", name: "qwerty", role: "Administrator" },
    ];
    nock("https://user.internal-prismic.io")
      .get("/repositories")
      .reply(200, responseData);

    const result = await communication.listRepositories(fakeCookie);
    expect(result).toEqual(responseData);
  });

  describe("validateRepositoryName", () => {
    const fakeBase = "https://prismic.io";

    test("should fail if subdomain is not defined", () => {
      const fn = () => communication.validateRepositoryName();
      return expect(fn).rejects.toThrow("repository name is required");
    });

    test("no upper case letters", () => {
      const fn = () => communication.validateRepositoryName("Abcd");
      return expect(fn).rejects.toThrow(
        "Must contain only lowercase letters, numbers and hyphens"
      );
    });

    test("should fail if name length is less than 4", () => {
      const fn = () => communication.validateRepositoryName("abc");
      return expect(fn).rejects.toThrow(
        "Must have four or more alphanumeric characters and/or hyphens."
      );
    });

    test("should fail if the name contains non alphanumeric characters", () => {
      const fn = () => communication.validateRepositoryName("a.bc");
      return expect(fn).rejects.toThrow(
        "Must contain only lowercase letters, numbers and hyphens"
      );
    });

    test("should fail if the name starts with a hyphen", () => {
      const fn = () => communication.validateRepositoryName("-abc");
      return expect(fn).rejects.toThrow("start with a letter");
    });

    test("should fail if the name ends with a hyphen", () => {
      const fn = () => communication.validateRepositoryName("abc-");
      return expect(fn).rejects.toThrow("Must end in a letter or a number");
    });

    test("Max length 30 characters", () => {
      const repoName = Array.from({ length: 31 }, () => "a").join("");
      const fn = () => communication.validateRepositoryName(repoName);
      return expect(fn).rejects.toThrow("30 characters or less");
    });

    test("multiple errors", () => {
      const repoName = "-abc.d";
      const fn = () => communication.validateRepositoryName(repoName);
      return expect(fn).rejects.toThrow(
        "(1: Must start with a letter. (2: Must contain only lowercase letters, numbers and hyphens."
      );
    });

    test("should fail if repo name is not available", () => {
      const repoName = "test";
      nock(fakeBase)
        .get(`/app/dashboard/repositories/${repoName}/exists`)
        .reply(200, () => false);

      const fn = () => communication.validateRepositoryName(repoName);

      return expect(fn).rejects.toThrow("already in use");
    });

    test("existing repo", () => {
      const repoName = "test";
      nock(fakeBase)
        .get(`/app/dashboard/repositories/${repoName}/exists`)
        .reply(200, () => false);
      return expect(
        communication.validateRepositoryName(repoName, fakeBase, true)
      ).resolves.toEqual(repoName);
    });

    test("existing repo, does not exist", () => {
      const repoName = "test";
      nock(fakeBase)
        .get(`/app/dashboard/repositories/${repoName}/exists`)
        .reply(200, () => true);
      return expect(
        communication.validateRepositoryName(repoName, fakeBase, true)
      ).rejects.toThrow("does not exist");
    });

    test("should pass if repo name is valid and available", () => {
      const repoName = "test";
      nock(fakeBase)
        .get(`/app/dashboard/repositories/${repoName}/exists`)
        .reply(200, () => true);

      return expect(
        communication.validateRepositoryName(repoName)
      ).resolves.toEqual(repoName);
    });

    test("different base", () => {
      const repoName = "test";
      nock("https://example.com")
        .get(`/app/dashboard/repositories/${repoName}/exists`)
        .reply(200, () => true);

      return expect(
        communication.validateRepositoryName(repoName, "https://example.com")
      ).resolves.toEqual(repoName);
    });
  });

  describe("createRepository", () => {
    const cookies = "prismic-auth=biscuit;";
    const repoName = "test";

    test("with default arguments it should call the prismic.io endpoint to create a new repo", async () => {
      const formData = {
        domain: repoName,
        framework: Frameworks.vanillajs,
        plan: "personal",
        isAnnual: "false",
        role: "developer",
      };

      nock("https://prismic.io")
        .post("/authentication/newrepository?app=slicemachine", formData)
        .reply(200, { domain: repoName });

      const result = await communication.createRepository(repoName, cookies);
      expect(result.data.domain).toEqual(repoName);
    });

    test("with framework and different base", async () => {
      const fakeBase = "https://example.com";
      const framework = Frameworks.next;

      const formData = {
        domain: repoName,
        framework,
        plan: "personal",
        isAnnual: "false",
        role: "developer",
      };

      nock(fakeBase)
        .post("/authentication/newrepository?app=slicemachine", formData)
        .reply(200, { domain: repoName });

      const result = await communication.createRepository(
        repoName,
        cookies,
        framework,
        fakeBase
      );
      expect(result.data.domain).toEqual(repoName);
    });
  });
});