Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage #43

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/cli-inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ const pasteConfigFile = (config: configType, customConfigFile: any) => {
};

const updateConfig = (config: configType, options: OptionValues, customConfigFile: any) => {
//pageLoad
if (options.requestTimeout !== defaultTimeout) {
config.requestTimeout = options.requestTimeout;
} else {
Expand Down
44 changes: 11 additions & 33 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,26 @@
import { createLogger, format, transports } from "winston";

const newLogger = () => {
//@ts-ignore
const customFormat = format.printf(({ message }) => {
return `${message}`;
});
const customFormat = ({ message }: { message: string }) => {
return message;
};

const printPrefix = (message: string): string => {
return ` - ${message}`;
};

const newLogger = () => {
const logger = createLogger({
level: "info",
format: format.simple(),
//defaultMeta: { service: 'your-service-name' },
transports: [
//
// - Write to all logs with level `info` and below to `quick-start-combined.log`.
// - Write all logs error (and below) to `quick-start-error.log`.
//
new transports.File({ filename: "logs/combined.log", options: { flags: "w" } }),
],
transports: [new transports.File({ filename: "logs/combined.log", options: { flags: "w" } })],
});

//
// If we're not in production then **ALSO** log to the `console`
//

//const LEVEL = Symbol.for('level');

//const colorizeFormat = format.colorize({ colors: { info: 'blue' }});

/*const info = colorizeFormat.transform({
[LEVEL]: 'info',
level: 'info',
message: 'my message'
}, { all: true });*/

if (process.env.NODE_ENV !== "production") {
logger.add(
new transports.Console({
format: format.combine(
format.colorize({ message: true, colors: { error: "red", info: "white" } }),
customFormat,
format.printf(customFormat),
),
}),
);
Expand All @@ -57,10 +39,6 @@ const formatConnectionMessage = (
return `${counter}/${maxCounter} - ${status} - ${Math.floor(elapsed_time)} ms - ${url}`;
};

const printPrefix = (message: string): string => {
return ` - ${message}`;
};

const logger = newLogger();

export { newLogger, formatConnectionMessage, logger, printPrefix };
export { newLogger, formatConnectionMessage, logger, printPrefix, customFormat };
56 changes: 56 additions & 0 deletions tests/cli-inputs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
sitesInput,
createConfig,
pasteConfigFile,
checkConfigFile,
defaultParallel,
defaultTimeout,
} from "../src/cli-inputs";

describe("cli-inputs", () => {
describe("sitesInput", () => {
it("should return an empty array if filePath is falsy", () => {
expect(sitesInput("")).toEqual([]);
});

it("should return an empty array if filePath does not exist", () => {
expect(sitesInput("nonexistent-file.txt")).toEqual([]);
});

// Add more test cases for different scenarios
});

describe("createConfig", () => {
it("should create a config object with the correct properties", () => {
const options = {
pageLoadType: "document",
requestTimeout: "10000",
waitPageLoad: true,
parallel: "4",
exclude: "pattern",
dry: true,
debug: false,
silent: false,
configFile: "config.json",
inputFile: "sites.txt",
};

const config = createConfig(options);

expect(config.pageLoadType).toBe(options.pageLoadType);
expect(config.requestTimeout).toBe(Number(options.requestTimeout));
expect(config.utilizeWaitForLoadState).toBe(options.waitPageLoad);
expect(config.parallelBlockSize).toBe(Number(options.parallel));
expect(config.excludePattern).toBe(options.exclude);
expect(config.dryRun).toBe(options.dry);
expect(config.debugMode).toBe(options.debug);
expect(config.silentRun).toBe(options.silent);
expect(config.configFilePath).toBe(options.configFile);
expect(config.sitesFilePath).toBe(options.inputFile);
});

// Add more test cases for different scenarios
});

// Add more test cases for the remaining functions
});
20 changes: 19 additions & 1 deletion tests/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { formatConnectionMessage } from "../src/logger";
import { formatConnectionMessage, customFormat, printPrefix } from "../src/logger";

describe("formatConnectionMessage", () => {
test("correctly formats the output", () => {
console.log(formatConnectionMessage(3, 123, 200, 123, "http://test.url"));
});
});

describe("newLogger", () => {
it("should format the message correctly", () => {
const message = "Test message";
const formattedMessage = customFormat({ message });

expect(formattedMessage).toBe("Test message");
});
});

describe("printPrefix", () => {
it("should add a prefix to the message", () => {
const message = "Hello, world!";
const result = printPrefix(message);

expect(result).toEqual(" - Hello, world!");
});
});