Skip to content

Commit

Permalink
fix(#771): timeout overriding value not being respected
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasgmagalhaes committed Jun 12, 2021
1 parent 695172e commit e23f3cc
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 99 deletions.
9 changes: 2 additions & 7 deletions src/common/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DEFAULT_TEST_TIMEOUT } from "../consts";
import { IConfigOptions } from "../types";

/**
Expand Down Expand Up @@ -59,12 +58,8 @@ export class Config implements IConfigOptions {
this.guildId = config.guildId;
}

if (!this.timeOut || forceUpdate) {
if (config.timeOut) {
this.timeOut = config.timeOut;
} else {
this.timeOut = DEFAULT_TEST_TIMEOUT;
}
if (config.timeOut && (!this.timeOut || forceUpdate)) {
this.timeOut = config.timeOut;
}

if (config.botPrefix && (!this.botPrefix || forceUpdate)) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FileError } from "../errors";
import { IConfigOptions, ITestFilePattern, ITestFile } from "../types";
import { utils } from "../utils";

class Reader {
export class Reader {
/**
* Read config file(*.json) from root of project
* and validates it
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1274,3 +1274,10 @@ export interface ITestFilePattern {
filesPattern: string[];
ignorePattern?: string[];
}

export namespace Config {
export interface ICLIOptions {
files: string;
config: string;
}
}
68 changes: 7 additions & 61 deletions tests/cli/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,67 +29,6 @@ describe("testing cli", () => {
expect(spy).toBeCalled();
});

it("should call go command", () => {
program.exitOverride();
const spy = jest.spyOn(goFunc, "exec").mockImplementation(null);
program.parse(["node", "test", ""]);
expect(spy).toBeCalled();
});

it("should call go command with -c option", () => {
program.exitOverride();
const spy = jest.spyOn(goFunc, "exec").mockImplementation(null);
const testPath = "potatoe";
program.parse(["node", "test", "-c", testPath]);
expect(spy).toBeCalled();
expect(runtime.configFilePath).toBe(testPath);
});

it("should call go command with -f option (single file)", () => {
program.exitOverride();
const spy = jest.spyOn(goFunc, "exec").mockImplementation(null);
const testMatches = "./tests";
program.parse(["node", "test", "-f", testMatches]);
expect(spy).toBeCalled();
expect(runtime.testMatches).toEqual(testMatches.split(" "));
});

it("should call go command with -f option (multiple files)", () => {
program.exitOverride();
const spy = jest.spyOn(goFunc, "exec").mockImplementation(null);
const testMatches = "./tests ./tests2";
program.parse(["node", "test", "-f", testMatches]);
expect(spy).toBeCalled();
expect(runtime.testMatches).toEqual(testMatches.split(" "));
});

it("should call go command with --files option (single file)", () => {
program.exitOverride();
const spy = jest.spyOn(goFunc, "exec").mockImplementation(null);
const testMatches = "./tests";
program.parse(["node", "test", "--files", testMatches]);
expect(spy).toBeCalled();
expect(runtime.testMatches).toEqual(testMatches.split(" "));
});

it("should call go command with --files option (multiple files)", () => {
program.exitOverride();
const spy = jest.spyOn(goFunc, "exec").mockImplementation(null);
const testMatches = "./tests ./tests2";
program.parse(["node", "test", "--files", testMatches]);
expect(spy).toBeCalled();
expect(runtime.testMatches).toEqual(testMatches.split(" "));
});

it("should call go command with --config option", () => {
program.exitOverride();
const spy = jest.spyOn(goFunc, "exec").mockImplementation(null);
const testPath = "potatoe";
program.parse(["node", "test", "--config", testPath]);
expect(spy).toBeCalled();
expect(runtime.configFilePath).toBe(testPath);
});

it("should call validate command", () => {
program.exitOverride();
jest.spyOn(reader, "loadConfig").mockImplementation(null);
Expand All @@ -105,4 +44,11 @@ describe("testing cli", () => {
program.parse(["node", "test", "v"]);
expect(spyValidate).toBeCalled();
});

it("should call exec command", () => {
program.exitOverride();
const spy = jest.spyOn(goFunc, "exec").mockImplementation(null);
program.parse(["node", "test", ""]);
expect(spy).toBeCalled();
});
});
112 changes: 82 additions & 30 deletions tests/cli/exec.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { reader } from "../../src/core/reader";
import { Reader, reader } from "../../src/core/reader";
import * as validateFn from "../../src/cli/validate";
import { exec } from "../../src/cli/exec";
import { FileError } from "../../src/errors";
import * as execCommand from "../../src/cli/exec";
import { runtime } from "../../src/common/runtime";
import { TestExecutor } from "../../src/core/testExecutor";
import { DEFAULT_TEST_TIMEOUT } from "../../src/consts";
import { IConfigOptions } from "../../src/types";
import { summary } from "../../src/core/summary";
import { mockProcess } from "../mocks";
import { program } from "../../src/cli/cli";

jest.mock("ora", () => {
const spinner = {
Expand All @@ -16,32 +18,82 @@ jest.mock("ora", () => {
return () => result;
});

jest.mock("../../src/core/testExecutor.ts");
TestExecutor.prototype.runTestsAndPrint = jest.fn().mockImplementation(() => Promise.resolve({}));

describe("testing default command", () => {
it("Should read a file folder", async () => {
const exitMock = mockProcess.mockProcessExit();
const readerSpy = jest.spyOn(reader, "loadConfig");
runtime.loginBot = jest.fn().mockReturnValue(Promise.resolve());
runtime.events.onceReady = jest.fn().mockReturnValue(Promise.resolve());
summary.print = jest.fn().mockReturnValue("");
const validateSpy = jest.spyOn(validateFn, "validate");

validateSpy.mockImplementation(() => null);

readerSpy.mockReturnValue({
botPrefix: "!",
botTestId: "123123123",
channelId: "123123123",
cordeBotToken: "12312112312",
guildId: "12312312",
testMatches: ["tests/cli/testFolder"],
botToken: "123123",
timeOut: 1000,
});

await exec();
expect(exitMock).toBeCalledWith(0);
describe("testing configs load", () => {
const config: IConfigOptions = {
botPrefix: "",
botTestId: "",
channelId: "",
cordeBotToken: "",
guildId: "",
testMatches: [""],
botToken: "",
timeOut: DEFAULT_TEST_TIMEOUT,
};
it("should load configs overriding timout value", async () => {
// https://github.com/cordejs/corde/issues/771
const TIMEOUT = 100000;
config.timeOut = TIMEOUT;
mockExecProces(config);
await execCommand.exec(
{
files: "",
config: "",
},
[],
);
expect(runtime.timeOut).toEqual(TIMEOUT);
});

it("should call go command with -c option", () => {
mockExecProces(config);
const testPath = "potatoe";
program.parse(["node", "test", "-c", testPath]);
expect(runtime.configFilePath).toBe(testPath);
});

it("should call go command with -f option (single file)", () => {
mockExecProces(config);
const testMatches = "./tests";
program.parse(["node", "test", "-f", testMatches]);
expect(runtime.testMatches).toEqual(testMatches.split(" "));
});

it("should call go command with -f option (multiple files)", () => {
mockExecProces(config);
const testMatches = "./tests ./tests2";
program.parse(["node", "test", "-f", testMatches]);
expect(runtime.testMatches).toEqual(testMatches.split(" "));
});

it("should call go command with --files option (single file)", () => {
mockExecProces(config);
const testMatches = "./tests";
program.parse(["node", "test", "--files", testMatches]);
expect(runtime.testMatches).toEqual(testMatches.split(" "));
});

it("should call go command with --files option (multiple files)", () => {
mockExecProces(config);
const testMatches = "./tests ./tests2";
program.parse(["node", "test", "--files", testMatches]);
expect(runtime.testMatches).toEqual(testMatches.split(" "));
});

it("should call go command with --config option", () => {
mockExecProces(config);
const testPath = "potatoe";
program.parse(["node", "test", "--config", testPath]);
expect(runtime.configFilePath).toBe(testPath);
});
});

function mockExecProces(config: IConfigOptions) {
Reader.prototype.loadConfig = jest.fn().mockReturnValue(config);
jest.spyOn(validateFn, "validate").mockImplementation(null);
jest.spyOn(execCommand, "runTests").mockImplementation(null);
mockProcess.mockProcessExit();
runtime.loginBot = jest.fn().mockReturnValue(Promise.resolve());
runtime.events.onceReady = jest.fn().mockReturnValue(Promise.resolve());
summary.print = jest.fn().mockReturnValue("");
jest.spyOn(validateFn, "validate");
}

0 comments on commit e23f3cc

Please sign in to comment.