Skip to content

Commit

Permalink
Add Prettier config
Browse files Browse the repository at this point in the history
Makes all formatting consistent
  • Loading branch information
fatso83 committed Sep 5, 2019
1 parent 5a75ef0 commit 965d047
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 66 deletions.
3 changes: 3 additions & 0 deletions .prettierrc
@@ -0,0 +1,3 @@
{
"singleQuote": true,
}
18 changes: 10 additions & 8 deletions src/buildConfigState.js
@@ -1,27 +1,29 @@
import buildGetNameForSnapshotUsingTemplate from "./buildGetNameForSnapshotUsingTemplate";
import buildGetNameForSnapshotUsingTemplate from './buildGetNameForSnapshotUsingTemplate';

module.exports = function buildConfigState(determineConfig) {
const config = {
snapshotFilename: undefined,
snapshotNameTemplate: undefined,
snapshotNameTemplate: undefined
};

function setFilename(snapshotFilename) {
config.snapshotFilename = snapshotFilename
config.snapshotFilename = snapshotFilename;
}

function setTestName(snapshotNameTemplate) {
config.snapshotNameTemplate = snapshotNameTemplate
config.snapshotNameTemplate = snapshotNameTemplate;
}

function configureUsingMochaContext(mochaContext) {
const { currentTest } = mochaContext;
setFilename(currentTest.file + ".snap");
setFilename(currentTest.file + '.snap');
setTestName(currentTest.fullTitle());
}

const snapshotNameRegistry = {}; // snapshotNameRegistry[filename][name] => number
const getNameForSnapshotUsingTemplate = buildGetNameForSnapshotUsingTemplate(snapshotNameRegistry);
const getNameForSnapshotUsingTemplate = buildGetNameForSnapshotUsingTemplate(
snapshotNameRegistry
);

function parseArgs(args) {
return determineConfig(args, config, getNameForSnapshotUsingTemplate);
Expand All @@ -38,6 +40,6 @@ module.exports = function buildConfigState(determineConfig) {
setTestName,
configureUsingMochaContext,
parseArgs,
resetSnapshotRegistry,
resetSnapshotRegistry
};
}
};
16 changes: 11 additions & 5 deletions src/buildGetNameForSnapshotUsingTemplate.js
@@ -1,11 +1,17 @@
module.exports = function buildGetNameForSnapshotUsingTemplate(snapshotNameRegistry) {
return function getNameForSnapshotUsingTemplate(snapshotFilename, snapshotNameTemplate) {
module.exports = function buildGetNameForSnapshotUsingTemplate(
snapshotNameRegistry
) {
return function getNameForSnapshotUsingTemplate(
snapshotFilename,
snapshotNameTemplate
) {
if (snapshotNameRegistry[snapshotFilename] == null) {
snapshotNameRegistry[snapshotFilename] = {};
}
const nextCounter = (snapshotNameRegistry[snapshotFilename][snapshotNameTemplate] || 0) + 1;
const nextCounter =
(snapshotNameRegistry[snapshotFilename][snapshotNameTemplate] || 0) + 1;
snapshotNameRegistry[snapshotFilename][snapshotNameTemplate] = nextCounter;

return `${snapshotNameTemplate} ${nextCounter}`;
}
}
};
};
65 changes: 36 additions & 29 deletions src/buildMatchSnapshot.js
@@ -1,41 +1,42 @@
import path from "path";
import jsonPath from "jsonpath";
import values from "lodash.values";
import cloneDeep from "lodash.clonedeep";
import clone from "lodash.clone";
import set from "lodash.set";
import { SnapshotState, getSerializers } from "jest-snapshot";
import path from 'path';
import jsonPath from 'jsonpath';
import values from 'lodash.values';
import cloneDeep from 'lodash.clonedeep';
import clone from 'lodash.clone';
import set from 'lodash.set';
import { SnapshotState, getSerializers } from 'jest-snapshot';

const buildMatchSnapshot = (utils, parseArgs) => {
if (thisRunsInJest()) {
const jestExpect = safeRequireJestExpect();
if (jestExpect) {
return function matchSnapshot(...args) {
return jestExpect(this._obj).toMatchSnapshot(...args);
}
};
}
}

return function matchSnapshot(...args) {
// support passing a property matcher object as first argument.
// This is backwards compatible, as all the prior args were strings or bools.
const propertyMatchers = (typeof args[0] === 'object') ? args.shift() : undefined;
const propertyMatchers =
typeof args[0] === 'object' ? args.shift() : undefined;
const { snapshotFilename, snapshotName, update, ci } = parseArgs(args);

if (utils.flag(this, 'negate')) {
throw new Error("`matchSnapshot` cannot be used with `.not`.");
throw new Error('`matchSnapshot` cannot be used with `.not`.');
}

const obj = this._obj;
const absolutePathToSnapshot = path.resolve(snapshotFilename);
const snapshotState = new SnapshotState(undefined, {
updateSnapshot: ci ? "none" : (update ? "all" : "new"),
snapshotPath: absolutePathToSnapshot,
updateSnapshot: ci ? 'none' : update ? 'all' : 'new',
snapshotPath: absolutePathToSnapshot
});

// Treat property matchers as jsonpath queries
// if they start with a $ and contain a dot.
const isJsonPath = it => it[0] === '$' && it.indexOf(".") > -1;
const isJsonPath = it => it[0] === '$' && it.indexOf('.') > -1;

// If we have property matchers, we have to reassign the value (pattern)
// from the matcher into the data being snapshotted, so that that data
Expand All @@ -45,17 +46,19 @@ const buildMatchSnapshot = (utils, parseArgs) => {
// do a deep clone, because the jsonpath could match a deep property.
const toMatch = (() => {
// No matchers == no cloning required
if(typeof propertyMatchers !== 'object') {
if (typeof propertyMatchers !== 'object') {
return obj;
}

const hasJsonPathMatchers = Object.keys(propertyMatchers).some(isJsonPath);
const hasJsonPathMatchers = Object.keys(propertyMatchers).some(
isJsonPath
);
return hasJsonPathMatchers ? cloneDeep(obj) : clone(obj);
})();

if(typeof propertyMatchers === 'object') {
if (typeof propertyMatchers === 'object') {
Object.keys(propertyMatchers).forEach(k => {
if(isJsonPath(k)) {
if (isJsonPath(k)) {
jsonPath.paths(obj, k).forEach(path => {
const lodashPath = path.slice(1);
set(toMatch, lodashPath, propertyMatchers[k]);
Expand All @@ -74,12 +77,12 @@ const buildMatchSnapshot = (utils, parseArgs) => {
//
// Note: we only test the top-level data for cusotm serializers
// (jest might look for them recursively).
const logCustomSerializerWarning = obj !== toMatch // we cloned
&& getSerializers().some(it => it.test(obj)); // has custom serializers
const logCustomSerializerWarning =
obj !== toMatch && getSerializers().some(it => it.test(obj)); // we cloned // has custom serializers

if(logCustomSerializerWarning) {
if (logCustomSerializerWarning) {
console.warn(
"Using property matchers may change how your object is serialized for snapshotting."
'Using property matchers may change how your object is serialized for snapshotting.'
);
}

Expand All @@ -89,8 +92,8 @@ const buildMatchSnapshot = (utils, parseArgs) => {
key: snapshotName
});

const actual = match.actual || "";
const expected = match.expected || "";
const actual = match.actual || '';
const expected = match.expected || '';
snapshotState.save();

this.assert(
Expand All @@ -108,14 +111,18 @@ const safeRequireJestExpect = () => {
// Jest might rename its "jest-matchers" module to "expect", so let's
// avoid an actual require and bank on the global expect here.
// (see https://github.com/facebook/jest/issues/1679#issuecomment-282478002)
return (typeof expect === 'undefined') ? null : expect;
return typeof expect === 'undefined' ? null : expect;
};

const JEST_MARKERS = ["enableAutomock", "genMockFromModule", "clearAllMocks", "runAllTicks"];
const JEST_MARKERS = [
'enableAutomock',
'genMockFromModule',
'clearAllMocks',
'runAllTicks'
];

const thisRunsInJest = () => (
typeof jest === "object" &&
JEST_MARKERS.every((marker) => typeof jest[marker] === "function")
);
const thisRunsInJest = () =>
typeof jest === 'object' &&
JEST_MARKERS.every(marker => typeof jest[marker] === 'function');

export default buildMatchSnapshot;
33 changes: 25 additions & 8 deletions src/determineConfig.js
@@ -1,15 +1,23 @@
module.exports = function determineConfig(args, config, getNameForSnapshotUsingTemplate) {
module.exports = function determineConfig(
args,
config,
getNameForSnapshotUsingTemplate
) {
let snapshotFilename;
let snapshotName;
let update;
let ci = false;

if (config.snapshotFilename && !config.snapshotNameTemplate) {
throw new Error("Using `setFilename` without also using `setTestName` is not supported.");
throw new Error(
'Using `setFilename` without also using `setTestName` is not supported.'
);
}

if (!config.snapshotFilename && config.snapshotNameTemplate) {
throw new Error("Using `setTestName` without also using `setFilename` is not supported.");
throw new Error(
'Using `setTestName` without also using `setFilename` is not supported.'
);
}

// Possible call signatures:
Expand All @@ -22,22 +30,31 @@ module.exports = function determineConfig(args, config, getNameForSnapshotUsingT

if (config.snapshotFilename && config.snapshotNameTemplate) {
snapshotFilename = config.snapshotFilename;
snapshotName = getNameForSnapshotUsingTemplate(snapshotFilename, config.snapshotNameTemplate);
snapshotName = getNameForSnapshotUsingTemplate(
snapshotFilename,
config.snapshotNameTemplate
);
update = args[0] || false;
} else {
if (args.length < 2) {
throw new Error("`matchSnapshot` cannot be called without a filename and snapshot name unless `setFilename` and `setTestName` have been called previously.");
throw new Error(
'`matchSnapshot` cannot be called without a filename and snapshot name unless `setFilename` and `setTestName` have been called previously.'
);
}
snapshotFilename = args[0];
snapshotName = args[1];
update = args[2] || false;
}

if (typeof process !== "undefined" && process.env && process.env.CHAI_JEST_SNAPSHOT_UPDATE_ALL) {
if (
typeof process !== 'undefined' &&
process.env &&
process.env.CHAI_JEST_SNAPSHOT_UPDATE_ALL
) {
update = true;
}

if (typeof process !== "undefined" && process.env && process.env.CI) {
if (typeof process !== 'undefined' && process.env && process.env.CI) {
ci = true;
}

Expand All @@ -47,4 +64,4 @@ module.exports = function determineConfig(args, config, getNameForSnapshotUsingT
update,
ci
};
}
};
42 changes: 26 additions & 16 deletions src/index.js
@@ -1,7 +1,7 @@
import buildMatchSnapshot from "./buildMatchSnapshot";
import buildConfigState from "./buildConfigState";
import determineConfig from "./determineConfig";
import { addSerializer } from "jest-snapshot";
import buildMatchSnapshot from './buildMatchSnapshot';
import buildConfigState from './buildConfigState';
import determineConfig from './determineConfig';
import { addSerializer } from 'jest-snapshot';

let hasChaiJestSnapshotBeenUsed = false;
let configuredSetFilename;
Expand All @@ -11,59 +11,69 @@ let configuredResetSnapshotRegistry;

function chaiJestSnapshot(chai, utils) {
if (hasChaiJestSnapshotBeenUsed) {
throw new Error("Running `chai.use(chaiJestSnapshot)` more than once is not supported.");
throw new Error(
'Running `chai.use(chaiJestSnapshot)` more than once is not supported.'
);
}

const {
setFilename,
setTestName,
configureUsingMochaContext,
parseArgs,
resetSnapshotRegistry,
resetSnapshotRegistry
} = buildConfigState(determineConfig);

const matchSnapshot = buildMatchSnapshot(utils, parseArgs);
chai.Assertion.addMethod("matchSnapshot", matchSnapshot);
chai.Assertion.addMethod('matchSnapshot', matchSnapshot);

configuredSetFilename = setFilename;
configuredSetTestName = setTestName;
configuredConfigureUsingMochaContext = configureUsingMochaContext;
configuredResetSnapshotRegistry = resetSnapshotRegistry;

hasChaiJestSnapshotBeenUsed = true;
};
}

chaiJestSnapshot.setFilename = function setFilename() {
if (configuredSetFilename) {
configuredSetFilename.apply(this, arguments);
} else {
throw new Error("Please run `chai.use(chaiJestSnapshot)` before using `chaiJestSnapshot.setFilename`.");
throw new Error(
'Please run `chai.use(chaiJestSnapshot)` before using `chaiJestSnapshot.setFilename`.'
);
}
}
};

chaiJestSnapshot.setTestName = function setTestName() {
if (configuredSetTestName) {
configuredSetTestName.apply(this, arguments);
} else {
throw new Error("Please run `chai.use(chaiJestSnapshot)` before using `chaiJestSnapshot.setTestName`.");
throw new Error(
'Please run `chai.use(chaiJestSnapshot)` before using `chaiJestSnapshot.setTestName`.'
);
}
}
};

chaiJestSnapshot.configureUsingMochaContext = function configureUsingMochaContext() {
if (configuredConfigureUsingMochaContext) {
configuredConfigureUsingMochaContext.apply(this, arguments);
} else {
throw new Error("Please run `chai.use(chaiJestSnapshot)` before using `chaiJestSnapshot.configureUsingMochaContext`.");
throw new Error(
'Please run `chai.use(chaiJestSnapshot)` before using `chaiJestSnapshot.configureUsingMochaContext`.'
);
}
}
};

chaiJestSnapshot.resetSnapshotRegistry = function resetSnapshotRegistry() {
if (configuredResetSnapshotRegistry) {
configuredResetSnapshotRegistry.apply(this, arguments);
} else {
throw new Error("Please run `chai.use(chaiJestSnapshot)` before using `chaiJestSnapshot.resetSnapshotRegistry`.");
throw new Error(
'Please run `chai.use(chaiJestSnapshot)` before using `chaiJestSnapshot.resetSnapshotRegistry`.'
);
}
}
};

chaiJestSnapshot.addSerializer = addSerializer;

Expand Down

0 comments on commit 965d047

Please sign in to comment.