data-provider-temporary
Version:
Library that helps with server-to-client synchronization of data
149 lines (112 loc) • 4.61 kB
JavaScript
; /**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*
*/var _require =
require('jest-matchers');const getState = _require.getState,setState = _require.setState;var _require2 =
require('jest-snapshot');const SnapshotState = _require2.SnapshotState,addSerializer = _require2.addSerializer;var _require3 =
require('jest-matcher-utils');const EXPECTED_COLOR = _require3.EXPECTED_COLOR,RECEIVED_COLOR = _require3.RECEIVED_COLOR,matcherHint = _require3.matcherHint,pluralize = _require3.pluralize;
// Get suppressed errors form jest-matchers that weren't throw during
// test execution and add them to the test result, potentially failing
// a passing test.
const addSuppressedErrors = result => {var _getState =
getState();const suppressedErrors = _getState.suppressedErrors;
setState({ suppressedErrors: [] });
if (suppressedErrors.length) {
result.status = 'failed';
result.failedExpectations = suppressedErrors.map(error => ({
actual: '',
// passing error for custom test reporters
error,
expected: '',
message: error.message,
passed: false,
stack: error.stack }));
}
};
function addAssertionErrors(result) {var _getState2 =
getState();const assertionCalls = _getState2.assertionCalls,expectedAssertionsNumber = _getState2.expectedAssertionsNumber,isExpectingAssertions = _getState2.isExpectingAssertions;
setState({
assertionCalls: 0,
expectedAssertionsNumber: null });
if (
typeof expectedAssertionsNumber === 'number' &&
assertionCalls !== expectedAssertionsNumber)
{
const expected = EXPECTED_COLOR(
pluralize('assertion', expectedAssertionsNumber));
const message = new Error(
matcherHint('.assertions', '', String(expectedAssertionsNumber), {
isDirectExpectCall: true }) +
'\n\n' +
`Expected ${expected} to be called but only received ` +
RECEIVED_COLOR(pluralize('assertion call', assertionCalls || 0)) +
'.').
stack;
result.status = 'failed';
result.failedExpectations.push({
actual: assertionCalls,
expected: expectedAssertionsNumber,
message,
passed: false });
}
if (isExpectingAssertions && assertionCalls === 0) {
const expected = EXPECTED_COLOR('at least one assertion');
const received = RECEIVED_COLOR('received none');
const message = new Error(
matcherHint('.hasAssertions', '', '', {
isDirectExpectCall: true }) +
'\n\n' +
`Expected ${expected} to be called but ${received}.`).
stack;
result.status = 'failed';
result.failedExpectations.push({
actual: 'none',
expected: 'at least one',
message,
passed: false });
}
}
const patchJasmine = () => {
global.jasmine.Spec = (realSpec => {
const Spec = function Spec(attr) {
const resultCallback = attr.resultCallback;
attr.resultCallback = function (result) {
addSuppressedErrors(result);
addAssertionErrors(result);
resultCallback.call(attr, result);
};
const onStart = attr.onStart;
attr.onStart = context => {
setState({ currentTestName: context.getFullName() });
onStart && onStart.call(attr, context);
};
realSpec.call(this, attr);
};
Spec.prototype = realSpec.prototype;
for (const statics in realSpec) {
if (Object.prototype.hasOwnProperty.call(realSpec, statics)) {
Spec[statics] = realSpec[statics];
}
}
return Spec;
})(global.jasmine.Spec);
};
module.exports = (_ref) =>
{let config = _ref.config,globalConfig = _ref.globalConfig,localRequire = _ref.localRequire,testPath = _ref.testPath;
// Jest tests snapshotSerializers in order preceding built-in serializers.
// Therefore, add in reverse because the last added is the first tested.
config.snapshotSerializers.concat().reverse().forEach(path => {
addSerializer(localRequire(path));
});
patchJasmine();const
expand = globalConfig.expand,updateSnapshot = globalConfig.updateSnapshot;
const snapshotState = new SnapshotState(testPath, { expand, updateSnapshot });
setState({ snapshotState, testPath });
// Return it back to the outer scope (test runner outside the VM).
return snapshotState;
};