From da6c353f3374d5dec1e5a4e94bf09ab9cf06d9aa Mon Sep 17 00:00:00 2001 From: cmackenz Date: Wed, 6 Mar 2019 16:49:20 -0500 Subject: [PATCH 1/2] adds a console config object to ignore methods --- README.md | 12 ++++++++++++ src/cft.ts | 9 ++++++--- src/console.ts | 4 +++- src/defaults.ts | 9 +++++++++ src/types.ts | 3 +++ 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 src/defaults.ts diff --git a/README.md b/README.md index 10585a9a..afbfcf7c 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,18 @@ require("console-fail-test").cft({ +## Ignoring `console` methods + +By default, `console-fail-test` will error on _any_ called `console` method. If you'd like ignore certain methods, pass a `console` object to the `cft` API when you set it up: + +```js +require("console-fail-test").cft({ + console: { + warn: false, // won't error on any instance of console.warn + }, +}); +``` + ## Why? Logging to the console during tests can be a sign of diff --git a/src/cft.ts b/src/cft.ts index 269177ac..f0c53895 100644 --- a/src/cft.ts +++ b/src/cft.ts @@ -4,14 +4,17 @@ import { selectTestEnvironment } from "./environments/selectTestEnvironments"; import { getSpyFactory } from "./spies/selectSpyFactory"; import { MethodCall, MethodSpy } from "./spies/spyTypes"; import { CftRequest } from "./types"; +import { setDefaults } from "./defaults"; -export const cft = (request: CftRequest = {}) => { +export const cft = (_request: Partial) => { + const request = setDefaults(_request); const spyFactory = getSpyFactory(request); const testEnvironment = selectTestEnvironment(request); const methodSpies: { [i: string]: MethodSpy } = {}; + const relevantMethodNames = consoleMethodNames.filter((name) => !!request.console[name as keyof typeof request.console]); testEnvironment.before(() => { - for (const methodName of consoleMethodNames) { + for (const methodName of relevantMethodNames) { methodSpies[methodName] = spyFactory(console, methodName); } }); @@ -19,7 +22,7 @@ export const cft = (request: CftRequest = {}) => { testEnvironment.after(({ reportComplaint }) => { const methodsWithCalls: [keyof Console, MethodCall[]][] = []; - for (const methodName of consoleMethodNames) { + for (const methodName of relevantMethodNames) { const spy = methodSpies[methodName]; const calls = testEnvironment.filterMethodCalls({ methodCalls: spy.getCalls(), diff --git a/src/console.ts b/src/console.ts index dd5ac9f7..d194e5ce 100644 --- a/src/console.ts +++ b/src/console.ts @@ -1,5 +1,7 @@ +import { ConsoleObject } from "./types"; + const isValidMemberName = (methodName: string) => !methodName.startsWith("_") && methodName[0].toLowerCase() === methodName[0]; -export const consoleMethodNames = (Object.keys(console) as (keyof Console)[]) +export const consoleMethodNames: (keyof ConsoleObject)[] = (Object.keys(console) as (keyof Console)[]) .filter((methodName) => isValidMemberName(methodName) && typeof console[methodName] === "function") .sort(); diff --git a/src/defaults.ts b/src/defaults.ts new file mode 100644 index 00000000..9f9e186f --- /dev/null +++ b/src/defaults.ts @@ -0,0 +1,9 @@ +import { CftRequest } from "./types"; + +const defaults = { + console: {}, +}; + +export const setDefaults = (request: Partial = {}) => { + return { ...defaults, ...request }; +}; diff --git a/src/types.ts b/src/types.ts index 0fb2e47d..a3ef9858 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,5 @@ export type CftRequest = { + console: ConsoleObject; spyLibrary?: SupportedSpyLibrary; testFramework?: SupportedTestFramework; }; @@ -6,3 +7,5 @@ export type CftRequest = { export type SupportedSpyLibrary = "fallback" | "jasmine" | "jest" | "sinon" | unknown; export type SupportedTestFramework = "mocha" | "jasmine" | "jest" | unknown; + +export type ConsoleObject = { [P in keyof Console]?: Console[P] extends Function ? boolean : never }; From b274702ca15597d3eb0df794236bef6928cf60ae Mon Sep 17 00:00:00 2001 From: cmackenz Date: Wed, 6 Mar 2019 17:30:37 -0500 Subject: [PATCH 2/2] incorporates josh feedback --- src/cft.ts | 6 +++--- src/console.ts | 4 ++-- src/defaults.ts | 2 +- src/types.ts | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cft.ts b/src/cft.ts index f0c53895..232e9ae8 100644 --- a/src/cft.ts +++ b/src/cft.ts @@ -6,12 +6,12 @@ import { MethodCall, MethodSpy } from "./spies/spyTypes"; import { CftRequest } from "./types"; import { setDefaults } from "./defaults"; -export const cft = (_request: Partial) => { - const request = setDefaults(_request); +export const cft = (rawRequest: Partial) => { + const request = setDefaults(rawRequest); const spyFactory = getSpyFactory(request); const testEnvironment = selectTestEnvironment(request); const methodSpies: { [i: string]: MethodSpy } = {}; - const relevantMethodNames = consoleMethodNames.filter((name) => !!request.console[name as keyof typeof request.console]); + const relevantMethodNames = consoleMethodNames.filter((name) => !!request.console[name]); testEnvironment.before(() => { for (const methodName of relevantMethodNames) { diff --git a/src/console.ts b/src/console.ts index d194e5ce..4c9c7c1f 100644 --- a/src/console.ts +++ b/src/console.ts @@ -1,7 +1,7 @@ -import { ConsoleObject } from "./types"; +import { ConsoleSettings } from "./types"; const isValidMemberName = (methodName: string) => !methodName.startsWith("_") && methodName[0].toLowerCase() === methodName[0]; -export const consoleMethodNames: (keyof ConsoleObject)[] = (Object.keys(console) as (keyof Console)[]) +export const consoleMethodNames: (keyof ConsoleSettings)[] = (Object.keys(console) as (keyof Console)[]) .filter((methodName) => isValidMemberName(methodName) && typeof console[methodName] === "function") .sort(); diff --git a/src/defaults.ts b/src/defaults.ts index 9f9e186f..eb8084f7 100644 --- a/src/defaults.ts +++ b/src/defaults.ts @@ -4,6 +4,6 @@ const defaults = { console: {}, }; -export const setDefaults = (request: Partial = {}) => { +export const setDefaults = (request: Partial = {}): CftRequest => { return { ...defaults, ...request }; }; diff --git a/src/types.ts b/src/types.ts index a3ef9858..38166fce 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,5 +1,5 @@ export type CftRequest = { - console: ConsoleObject; + console: ConsoleSettings; spyLibrary?: SupportedSpyLibrary; testFramework?: SupportedTestFramework; }; @@ -8,4 +8,4 @@ export type SupportedSpyLibrary = "fallback" | "jasmine" | "jest" | "sinon" | un export type SupportedTestFramework = "mocha" | "jasmine" | "jest" | unknown; -export type ConsoleObject = { [P in keyof Console]?: Console[P] extends Function ? boolean : never }; +export type ConsoleSettings = { [P in keyof Console]?: Console[P] extends Function ? boolean : never };