<commit_before>import runner from "./runner";
import Setup from "../types/Setup";
import Test from "../types/Test";
import Results from "../types/Results";
import createTest from "./testUtils/createTest";
import createSetup from "./testUtils/createSetup";
import { AssertionError } from "assert";

jest.mock("events");

describe("runner", () => {
  let expectedTest: Test;
  let expectedTests;
  let setup: Setup;

  let results: Results;

  beforeEach(() => {
    expectedTest = createTest("1");
    expectedTests = [expectedTest];
    setup = createSetup(expectedTests);
    results = [];
    setup.assertionErrorsTypes.push(AssertionError);
  });

  it("should run test", () => {
    runner(setup)(results);
    expect(expectedTest.fn).toBeCalledWith();
  });

  it("should set test file path from", () => {
    runner(setup)(results);
    expect(results[0].testFilePath).toEqual(expectedTest.testFilePath);
  });

  it("should set description from test", () => {
    runner(setup)(results);
    expect(results[0].description).toEqual(expectedTest.description);
  });

  it("should set state to passed", () => {
    runner(setup)(results);
    expect(results[0].state).toEqual("passed");
  });

  it.todo("should set start date");

  it.todo("should set end date");

  describe("events", () => {
    it("should emit on test start for each test", () => {
      runner(setup)(results);
      expect(setup.events.emit).toBeCalledWith("test:start", expectedTest);
    });

    it("should emit on test result for each test", () => {
      expectedTests.push(createTest("2"));
      runner(setup)(results);
      expect(setup.events.emit).toBeCalledWith("test:result", results[0]);
      expect(setup.events.emit).toBeCalledWith("test:result", results[1]);
    });

    describe("when test failure", () => {
      let error: Error;

      beforeEach(() => {
        error = new AssertionError({});

        (expectedTest.fn as any).mockImplementation(() => {
          throw error;
        });
      });

      it("should set state to failed", () => {
        runner(setup)(results);
        expect(results[0].state).toEqual("failed");
      });

      it("should emit test:failure", () => {
        runner(setup)(results);
        expect(setup.events.emit).toBeCalledWith("test:failure", results[0]);
      });
    });
  });

  describe("when test throws error", () => {
    let error: Error;

    beforeEach(() => {
      error = new Error();

      (expectedTest.fn as any).mockImplementation(() => {
        throw error;
      });
    });

    it("should set state to failed", () => {
      runner(setup)(results);
      expect(results[0].state).toEqual("errored");
    });

    it("should emit test:error", () => {
      runner(setup)(results);
      expect(setup.events.emit).toBeCalledWith("test:error", results[0]);
    });

    it("should set error to error thrown", () => {
      runner(setup)(results);
      expect(results[0].error).toEqual(error);
    });
  });

  describe("when run state is skip", () => {
    beforeEach(() => {
      expectedTest.runState = "skip";
    });

    it("should set state to skipped", () => {
      runner(setup)(results);
      expect(results[0].state).toEqual("skipped");
    });

    it("should not call test function 2", () => {
      runner(setup)(results);
      expect(expectedTest.fn).not.toBeCalled();
    });
  });

  describe("when run state is todo", () => {
    beforeEach(() => {
      expectedTest.runState = "todo";
    });

    it("should set state to todo", () => {
      runner(setup)(results);
      expect(results[0].state).toEqual("todo");
    });

    it("should not call test function", () => {
      runner(setup)(results);
      expect(expectedTest.fn).not.toBeCalled();
    });
  });

  describe("multiple tests", () => {
    const expectedTest2: Test = {
      testFilePath: "test file path 2",
      description: "description 2",
      runState: "run",
      fn: jest.fn()
    };<commit_msg>Add test for unknown test run state<commit_after>import runner from "./runner";
import Setup from "../types/Setup";
import Test from "../types/Test";
import Results from "../types/Results";
import createTest from "./testUtils/createTest";
import createSetup from "./testUtils/createSetup";
import { AssertionError } from "assert";

jest.mock("events");

describe("runner", () => {
  let expectedTest: Test;
  let expectedTests;
  let setup: Setup;

  let results: Results;

  beforeEach(() => {
    expectedTest = createTest("1");
    expectedTests = [expectedTest];
    setup = createSetup(expectedTests);
    results = [];
    setup.assertionErrorsTypes.push(AssertionError);
  });

  it("should run test", () => {
    runner(setup)(results);
    expect(expectedTest.fn).toBeCalledWith();
  });

  it("should set test file path from", () => {
    runner(setup)(results);
    expect(results[0].testFilePath).toEqual(expectedTest.testFilePath);
  });

  it("should set description from test", () => {
    runner(setup)(results);
    expect(results[0].description).toEqual(expectedTest.description);
  });

  it("should set state to passed", () => {
    runner(setup)(results);
    expect(results[0].state).toEqual("passed");
  });

  it.todo("should set start date");

  it.todo("should set end date");

  describe("events", () => {
    it("should emit on test start for each test", () => {
      runner(setup)(results);
      expect(setup.events.emit).toBeCalledWith("test:start", expectedTest);
    });

    it("should emit on test result for each test", () => {
      expectedTests.push(createTest("2"));
      runner(setup)(results);
      expect(setup.events.emit).toBeCalledWith("test:result", results[0]);
      expect(setup.events.emit).toBeCalledWith("test:result", results[1]);
    });

    describe("when test failure", () => {
      let error: Error;

      beforeEach(() => {
        error = new AssertionError({});

        (expectedTest.fn as any).mockImplementation(() => {
          throw error;
        });
      });

      it("should set state to failed", () => {
        runner(setup)(results);
        expect(results[0].state).toEqual("failed");
      });

      it("should emit test:failure", () => {
        runner(setup)(results);
        expect(setup.events.emit).toBeCalledWith("test:failure", results[0]);
      });
    });
  });

  describe("when test throws error", () => {
    let error: Error;

    beforeEach(() => {
      error = new Error();

      (expectedTest.fn as any).mockImplementation(() => {
        throw error;
      });
    });

    it("should set state to failed", () => {
      runner(setup)(results);
      expect(results[0].state).toEqual("errored");
    });

    it("should emit test:error", () => {
      runner(setup)(results);
      expect(setup.events.emit).toBeCalledWith("test:error", results[0]);
    });

    it("should set error to error thrown", () => {
      runner(setup)(results);
      expect(results[0].error).toEqual(error);
    });
  });

  describe("when run state is skip", () => {
    beforeEach(() => {
      expectedTest.runState = "skip";
    });

    it("should set state to skipped", () => {
      runner(setup)(results);
      expect(results[0].state).toEqual("skipped");
    });

    it("should not call test function 2", () => {
      runner(setup)(results);
      expect(expectedTest.fn).not.toBeCalled();
    });
  });

  describe("when run state is todo", () => {
    beforeEach(() => {
      expectedTest.runState = "todo";
    });

    it("should set state to todo", () => {
      runner(setup)(results);
      expect(results[0].state).toEqual("todo");
    });

    it("should not call test function", () => {
      runner(setup)(results);
      expect(expectedTest.fn).not.toBeCalled();
    });
  });

  describe("when run state is unknown", () => {
    beforeEach(() => {
      (expectedTest.runState as any) = "unknown";
    });

    it("should throw error", () => {
      expect(() => runner(setup)(results)).toThrow(
        `Invalid test run state: unknown`
      );
    });
  });

  describe("multiple tests", () => {
    const expectedTest2: Test = {
      testFilePath: "test file path 2",
      description: "description 2",
      runState: "run",
      fn: jest.fn()
    };
{'fraction_non_alphanumeric': 0.18434803742860614, 'fraction_numerical': 0.004374772147283996, 'mean_word_length': 3.052191038897095, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6789458', 'n_tokens_mistral': 2421, 'n_tokens_neox': 2378, 'n_words': 502}