only-changed-jest-watch-plugin
Version:
Jest watch plugin for running either only the modified test (for TDD), or tests of dependant modules
105 lines (83 loc) • 2.9 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.setMatchers = exports.getMatchers = exports.setState = exports.getState = exports.INTERNAL_MATCHER_FLAG = undefined;
var _asymmetric_matchers = require('./asymmetric_matchers');
// Global matchers object holds the list of available matchers and
// the state, that can hold matcher specific values that change over time.
const JEST_MATCHERS_OBJECT = Symbol.for('$$jest-matchers-object');
// Notes a built-in/internal Jest matcher.
// Jest may override the stack trace of Errors thrown by internal matchers.
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*
*/
const INTERNAL_MATCHER_FLAG = (exports.INTERNAL_MATCHER_FLAG = Symbol.for(
'$$jest-internal-matcher'
));
if (!global[JEST_MATCHERS_OBJECT]) {
Object.defineProperty(global, JEST_MATCHERS_OBJECT, {
value: {
matchers: Object.create(null),
state: {
assertionCalls: 0,
expectedAssertionsNumber: null,
isExpectingAssertions: false,
suppressedErrors: [] // errors that are not thrown immediately.
}
}
});
}
const getState = (exports.getState = () => global[JEST_MATCHERS_OBJECT].state);
const setState = (exports.setState = state => {
Object.assign(global[JEST_MATCHERS_OBJECT].state, state);
});
const getMatchers = (exports.getMatchers = () =>
global[JEST_MATCHERS_OBJECT].matchers);
const setMatchers = (exports.setMatchers = (matchers, isInternal, expect) => {
Object.keys(matchers).forEach(key => {
const matcher = matchers[key];
Object.defineProperty(matcher, INTERNAL_MATCHER_FLAG, {
value: isInternal
});
if (!isInternal) {
// expect is defined
class CustomMatcher extends _asymmetric_matchers.AsymmetricMatcher {
constructor(sample) {
let inverse =
arguments.length > 1 && arguments[1] !== undefined
? arguments[1]
: false;
super();
this.sample = sample;
this.inverse = inverse;
}
asymmetricMatch(other) {
var _ref = matcher(other, this.sample);
const pass = _ref.pass;
return this.inverse ? !pass : pass;
}
toString() {
return `${this.inverse ? 'not.' : ''}${key}`;
}
getExpectedType() {
return 'any';
}
toAsymmetricMatcher() {
return `${this.toString()}<${this.sample}>`;
}
}
expect[key] = sample => new CustomMatcher(sample);
if (!expect.not) {
expect.not = {};
}
expect.not[key] = sample => new CustomMatcher(sample, true);
}
});
Object.assign(global[JEST_MATCHERS_OBJECT].matchers, matchers);
});