@jest/globals#test TypeScript Examples

The following examples show how to use @jest/globals#test. 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: async-refinements.test.ts    From zod with MIT License 6 votes vote down vote up
test("parseAsync async test", async () => {
  // expect.assertions(2);

  const schema1 = z.string().refine((_val) => Promise.resolve(true));
  const v1 = await schema1.parseAsync("asdf");
  expect(v1).toEqual("asdf");

  const schema2 = z.string().refine((_val) => Promise.resolve(false));
  await expect(schema2.parseAsync("asdf")).rejects.toBeDefined();

  const schema3 = z.string().refine((_val) => Promise.resolve(true));
  await expect(schema3.parseAsync("asdf")).resolves.toEqual("asdf");
  return await expect(schema3.parseAsync("qwer")).resolves.toEqual("qwer");
});
Example #2
Source File: main.test.ts    From gh-get-current-pr with GNU General Public License v3.0 6 votes vote down vote up
test('prefers PR with commit as head SHA', () => {
  const testPRs = [
    createDummyPR(1, {sha: '09e30775c'}),
    createDummyPR(2, {sha: '90775cae3'})
  ]
  const options = {
    preferWithHeadSha: testPRs[1].head.sha
  }
  const foundPR = getLastPullRequest(testPRs, options) || {id: null}
  expect(foundPR.id).toBe(testPRs[1].id)
})
Example #3
Source File: async-parsing.test.ts    From zod with MIT License 6 votes vote down vote up
test("ensure early async failure prevents follow-up refinement checks", async () => {
  let count = 0;
  const base = z.object({
    hello: z.string(),
    foo: z
      .number()
      .refine(async () => {
        count++;
        return true;
      })
      .refine(async () => {
        count++;
        return true;
      }, "Good"),
  });

  const testval = { hello: "bye", foo: 3 };
  const result = await base.safeParseAsync(testval);
  if (result.success === false) {
    expect(result.error.issues.length).toBe(1);
    expect(count).toBe(1);
  }

  // await result.then((r) => {
  //   if (r.success === false) expect(r.error.issues.length).toBe(1);
  //   expect(count).toBe(2);
  // });
});
Example #4
Source File: cli-provider-service.spec.ts    From leapp with Mozilla Public License 2.0 6 votes vote down vote up
describe("CliProviderService", () => {
  test("services", async () => {
    for (const propertyName of Object.keys(Object.getOwnPropertyDescriptors(CliProviderService.prototype))) {
      const cliProviderService = new CliProviderService();

      let result;
      try {
        result = cliProviderService[propertyName];
      } catch (e) {
        throw new Error(`error getting: ${propertyName}`);
      }

      try {
        expect(result).not.toBeFalsy();
      } catch (e) {
        throw new Error(`${propertyName} is falsy`);
      }

      try {
        expect(cliProviderService[propertyName]).toBe(result);
      } catch (error) {
        throw new Error(`singleton not working for ${propertyName}`);
      }
    }
  });
});
Example #5
Source File: array.test.ts    From zod with MIT License 6 votes vote down vote up
test("failing validations", () => {
  expect(() => minTwo.parse(["a"])).toThrow();
  expect(() => maxTwo.parse(["a", "a", "a"])).toThrow();
  expect(() => justTwo.parse(["a"])).toThrow();
  expect(() => justTwo.parse(["a", "a", "a"])).toThrow();
  expect(() => intNum.parse([])).toThrow();
  expect(() => nonEmptyMax.parse([])).toThrow();
  expect(() => nonEmptyMax.parse(["a", "a", "a"])).toThrow();
});
Example #6
Source File: get-default.spec.ts    From leapp with Mozilla Public License 2.0 6 votes vote down vote up
describe("GetDefaultRegion", () => {
  test("run", async () => {
    const cliProviderService = { regionsService: { getDefaultAwsRegion: () => "defaultRegion" } } as any;

    const command = new GetDefaultRegion([], {} as any);
    (command as any).cliProviderService = cliProviderService;
    command.log = jest.fn();

    await command.run();

    expect(command.log).toHaveBeenCalledWith("defaultRegion");
  });
});
Example #7
Source File: all-errors.test.ts    From zod with MIT License 6 votes vote down vote up
test(".flatten() type assertion", () => {
  const parsed = Test.safeParse({}) as z.SafeParseError<void>;
  const validFlattenedErrors: TestFlattenedErrors = parsed.error.flatten(
    () => ({ message: "", code: 0 })
  );
  // @ts-expect-error should fail assertion between `TestFlattenedErrors` and unmapped `flatten()`.
  const invalidFlattenedErrors: TestFlattenedErrors = parsed.error.flatten();
  const validFormErrors: TestFormErrors = parsed.error.flatten();
  // @ts-expect-error should fail assertion between `TestFormErrors` and mapped `flatten()`.
  const invalidFormErrors: TestFormErrors = parsed.error.flatten(() => ({
    message: "string",
    code: 0,
  }));

  [
    validFlattenedErrors,
    invalidFlattenedErrors,
    validFormErrors,
    invalidFormErrors,
  ];
});
Example #8
Source File: workspace-service.spec.ts    From leapp with Mozilla Public License 2.0 6 votes vote down vote up
describe("WorkspaceService", () => {
  test("getWorkspace - return an instance of the workspace model", () => {
    const repository = {
      getWorkspace: jest.fn(() => new Workspace()),
    };
    const workspaceService = new WorkspaceService(repository as any);
    expect(workspaceService.getWorkspace()).toBeInstanceOf(Workspace);
    expect(repository.getWorkspace).toHaveBeenCalled();
  });
  test("persistsWorkspace - persist the changes in the workspace", () => {
    let modifiedWorkspace;
    const repository = {
      getWorkspace: jest.fn(() => new Workspace()),
      persistWorkspace: jest.fn((workspace: Workspace) => {
        modifiedWorkspace = Object.assign(workspace, {});
      }),
    };
    const workspaceService = new WorkspaceService(repository as any);
    const newWorkspace = workspaceService.getWorkspace();
    newWorkspace.colorTheme = "testValue";
    workspaceService.persistWorkspace(newWorkspace);
    expect(newWorkspace).toStrictEqual(modifiedWorkspace);
    expect(repository.persistWorkspace).toHaveBeenCalled();
  });
  test("workspaceExists - verify if the workspace exists or not", () => {
    const repository = {
      getWorkspace: jest.fn(() => new Workspace()),
    };
    const workspaceService = new WorkspaceService(repository as any);
    expect(workspaceService.workspaceExists()).toStrictEqual(true);
    expect(repository.getWorkspace).toHaveBeenCalled();
  });
});