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

Adds a console config object to ignore methods #38

Merged
merged 2 commits into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ require("console-fail-test").cft({
</tbody>
</table>

## 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
Expand Down
9 changes: 6 additions & 3 deletions src/cft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@ 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<CftRequest>) => {
mackenco marked this conversation as resolved.
Show resolved Hide resolved
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]);
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

testEnvironment.before(() => {
for (const methodName of consoleMethodNames) {
for (const methodName of relevantMethodNames) {
methodSpies[methodName] = spyFactory(console, methodName);
}
});

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(),
Expand Down
4 changes: 3 additions & 1 deletion src/console.ts
Original file line number Diff line number Diff line change
@@ -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();
9 changes: 9 additions & 0 deletions src/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { CftRequest } from "./types";

const defaults = {
console: {},
};

export const setDefaults = (request: Partial<CftRequest> = {}) => {
return { ...defaults, ...request };
};
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
export type CftRequest = {
console: ConsoleObject;
spyLibrary?: SupportedSpyLibrary;
testFramework?: SupportedTestFramework;
};

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 };
mackenco marked this conversation as resolved.
Show resolved Hide resolved