relay-test-utils-internal 0.0.0-main-83136765 → 0.0.0-main-3497ce7f
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/consoleError.js.flow +42 -19
- package/consoleErrorsAndWarnings.js.flow +116 -0
- package/index.js +1 -1
- package/index.js.flow +9 -1
- package/lib/consoleError.js +43 -21
- package/lib/consoleErrorsAndWarnings.js +103 -0
- package/lib/index.js +7 -1
- package/lib/warnings.js +18 -62
- package/package.json +2 -2
- package/relay-test-utils-internal.js +2 -2
- package/relay-test-utils-internal.min.js +2 -2
- package/warnings.js.flow +22 -60
package/consoleError.js.flow
CHANGED
@@ -11,32 +11,55 @@
|
|
11
11
|
|
12
12
|
'use strict';
|
13
13
|
|
14
|
-
/* global jest
|
14
|
+
/* global jest */
|
15
15
|
|
16
|
-
|
16
|
+
const {createConsoleInterceptionSystem} = require('./consoleErrorsAndWarnings');
|
17
|
+
|
18
|
+
const consoleErrorsSystem = createConsoleInterceptionSystem(
|
19
|
+
'error',
|
20
|
+
'expectConsoleError',
|
21
|
+
impl => {
|
22
|
+
jest.spyOn(console, 'error').mockImplementation(impl);
|
23
|
+
},
|
24
|
+
);
|
17
25
|
|
18
26
|
/**
|
19
|
-
*
|
20
|
-
*
|
27
|
+
* Mocks console.error so that errors printed to the console are instead thrown.
|
28
|
+
* Any expected errors need to be explicitly expected with `expectConsoleErrorWillFire(message)`.
|
29
|
+
*
|
30
|
+
* NOTE: This should be called on top of a test file. The test should NOT
|
31
|
+
* use `jest.resetModules()` or manually mock `console`.
|
21
32
|
*/
|
22
33
|
function disallowConsoleErrors(): void {
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
consoleErrorsSystem.disallowMessages();
|
35
|
+
}
|
36
|
+
|
37
|
+
/**
|
38
|
+
* Expect an error with the given message. If the message isn't fired in the
|
39
|
+
* current test, the test will fail.
|
40
|
+
*/
|
41
|
+
function expectConsoleErrorWillFire(message: string): void {
|
42
|
+
consoleErrorsSystem.expectMessageWillFire(message);
|
43
|
+
}
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Expect the callback `fn` to print an error with the message, and otherwise fail.
|
47
|
+
*/
|
48
|
+
function expectConsoleError<T>(message: string, fn: () => T): T {
|
49
|
+
return consoleErrorsSystem.expectMessage(message, fn);
|
50
|
+
}
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Expect the callback `fn` to trigger all console errors (in sequence),
|
54
|
+
* and otherwise fail.
|
55
|
+
*/
|
56
|
+
function expectConsoleErrorsMany<T>(messages: Array<string>, fn: () => T): T {
|
57
|
+
return consoleErrorsSystem.expectMessageMany(messages, fn);
|
38
58
|
}
|
39
59
|
|
40
60
|
module.exports = {
|
41
61
|
disallowConsoleErrors,
|
62
|
+
expectConsoleErrorWillFire,
|
63
|
+
expectConsoleError,
|
64
|
+
expectConsoleErrorsMany,
|
42
65
|
};
|
@@ -0,0 +1,116 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
* @emails oncall+relay
|
8
|
+
* @flow strict-local
|
9
|
+
* @format
|
10
|
+
*/
|
11
|
+
|
12
|
+
'use strict';
|
13
|
+
|
14
|
+
/* global jest, afterEach */
|
15
|
+
|
16
|
+
type API = $ReadOnly<{|
|
17
|
+
disallowMessages: () => void,
|
18
|
+
expectMessageWillFire: string => void,
|
19
|
+
expectMessage: <T>(string, () => T) => T,
|
20
|
+
expectMessageMany: <T>(Array<string>, () => T) => T,
|
21
|
+
|}>;
|
22
|
+
|
23
|
+
const originalConsoleError = console.error;
|
24
|
+
|
25
|
+
function createConsoleInterceptionSystem(
|
26
|
+
typename: string,
|
27
|
+
expectFunctionName: string,
|
28
|
+
setUpMock: ((string) => void) => void,
|
29
|
+
): API {
|
30
|
+
let installed = false;
|
31
|
+
const expectedMessages: Array<string> = [];
|
32
|
+
const contextualExpectedMessage: Array<string> = [];
|
33
|
+
|
34
|
+
const typenameCap = typename.charAt(0).toUpperCase() + typename.slice(1);
|
35
|
+
const typenameCapPlural = typenameCap + 's';
|
36
|
+
const installerName = `disallow${typenameCap}s`;
|
37
|
+
|
38
|
+
function handleMessage(message: string): void {
|
39
|
+
const index = expectedMessages.findIndex(expected =>
|
40
|
+
message.startsWith(expected),
|
41
|
+
);
|
42
|
+
if (
|
43
|
+
contextualExpectedMessage.length > 0 &&
|
44
|
+
contextualExpectedMessage[0] === message
|
45
|
+
) {
|
46
|
+
contextualExpectedMessage.shift();
|
47
|
+
} else if (index >= 0) {
|
48
|
+
expectedMessages.splice(index, 1);
|
49
|
+
} else {
|
50
|
+
// log to console in case the error gets swallowed somewhere
|
51
|
+
originalConsoleError(`Unexpected ${typenameCap}: ` + message);
|
52
|
+
throw new Error(`${typenameCap}: ` + message);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
function disallowMessages(): void {
|
57
|
+
if (installed) {
|
58
|
+
throw new Error(`${installerName} should be called only once.`);
|
59
|
+
}
|
60
|
+
installed = true;
|
61
|
+
setUpMock(handleMessage);
|
62
|
+
|
63
|
+
afterEach(() => {
|
64
|
+
contextualExpectedMessage.length = 0;
|
65
|
+
if (expectedMessages.length > 0) {
|
66
|
+
const error = new Error(
|
67
|
+
`Some expected ${typename}s where not triggered:\n\n` +
|
68
|
+
Array.from(expectedMessages, message => ` * ${message}`).join(
|
69
|
+
'\n',
|
70
|
+
) +
|
71
|
+
'\n',
|
72
|
+
);
|
73
|
+
expectedMessages.length = 0;
|
74
|
+
throw error;
|
75
|
+
}
|
76
|
+
});
|
77
|
+
}
|
78
|
+
|
79
|
+
function expectMessageWillFire(message: string): void {
|
80
|
+
if (!installed) {
|
81
|
+
throw new Error(
|
82
|
+
`${installerName} needs to be called before expect${typenameCapPlural}WillFire`,
|
83
|
+
);
|
84
|
+
}
|
85
|
+
expectedMessages.push(message);
|
86
|
+
}
|
87
|
+
|
88
|
+
function expectMessage<T>(message: string, fn: () => T): T {
|
89
|
+
return expectMessageMany([message], fn);
|
90
|
+
}
|
91
|
+
|
92
|
+
function expectMessageMany<T>(messages: Array<string>, fn: () => T): T {
|
93
|
+
if (contextualExpectedMessage.length > 0) {
|
94
|
+
throw new Error(`Cannot nest ${expectFunctionName}() calls.`);
|
95
|
+
}
|
96
|
+
contextualExpectedMessage.push(...messages);
|
97
|
+
const result = fn();
|
98
|
+
if (contextualExpectedMessage.length > 0) {
|
99
|
+
const notFired = contextualExpectedMessage.toString();
|
100
|
+
contextualExpectedMessage.length = 0;
|
101
|
+
throw new Error(`Expected ${typename} in callback: ${notFired}`);
|
102
|
+
}
|
103
|
+
return result;
|
104
|
+
}
|
105
|
+
|
106
|
+
return {
|
107
|
+
disallowMessages,
|
108
|
+
expectMessageWillFire,
|
109
|
+
expectMessage,
|
110
|
+
expectMessageMany,
|
111
|
+
};
|
112
|
+
}
|
113
|
+
|
114
|
+
module.exports = {
|
115
|
+
createConsoleInterceptionSystem,
|
116
|
+
};
|
package/index.js
CHANGED
package/index.js.flow
CHANGED
@@ -12,7 +12,12 @@
|
|
12
12
|
|
13
13
|
'use strict';
|
14
14
|
|
15
|
-
const {
|
15
|
+
const {
|
16
|
+
disallowConsoleErrors,
|
17
|
+
expectConsoleError,
|
18
|
+
expectConsoleErrorsMany,
|
19
|
+
expectConsoleErrorWillFire,
|
20
|
+
} = require('./consoleError');
|
16
21
|
const describeWithFeatureFlags = require('./describeWithFeatureFlags');
|
17
22
|
const {
|
18
23
|
FIXTURE_TAG,
|
@@ -53,6 +58,9 @@ module.exports = {
|
|
53
58
|
describeWithFeatureFlags,
|
54
59
|
disallowConsoleErrors,
|
55
60
|
disallowWarnings,
|
61
|
+
expectConsoleError,
|
62
|
+
expectConsoleErrorsMany,
|
63
|
+
expectConsoleErrorWillFire,
|
56
64
|
expectToWarn,
|
57
65
|
expectToWarnMany,
|
58
66
|
expectWarningWillFire,
|
package/lib/consoleError.js
CHANGED
@@ -9,33 +9,55 @@
|
|
9
9
|
* @format
|
10
10
|
*/
|
11
11
|
'use strict';
|
12
|
-
/* global jest
|
12
|
+
/* global jest */
|
13
13
|
|
14
|
-
var
|
14
|
+
var _require = require('./consoleErrorsAndWarnings'),
|
15
|
+
createConsoleInterceptionSystem = _require.createConsoleInterceptionSystem;
|
16
|
+
|
17
|
+
var consoleErrorsSystem = createConsoleInterceptionSystem('error', 'expectConsoleError', function (impl) {
|
18
|
+
jest.spyOn(console, 'error').mockImplementation(impl);
|
19
|
+
});
|
15
20
|
/**
|
16
|
-
*
|
17
|
-
*
|
21
|
+
* Mocks console.error so that errors printed to the console are instead thrown.
|
22
|
+
* Any expected errors need to be explicitly expected with `expectConsoleErrorWillFire(message)`.
|
23
|
+
*
|
24
|
+
* NOTE: This should be called on top of a test file. The test should NOT
|
25
|
+
* use `jest.resetModules()` or manually mock `console`.
|
18
26
|
*/
|
19
27
|
|
20
28
|
function disallowConsoleErrors() {
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
29
|
+
consoleErrorsSystem.disallowMessages();
|
30
|
+
}
|
31
|
+
/**
|
32
|
+
* Expect an error with the given message. If the message isn't fired in the
|
33
|
+
* current test, the test will fail.
|
34
|
+
*/
|
35
|
+
|
36
|
+
|
37
|
+
function expectConsoleErrorWillFire(message) {
|
38
|
+
consoleErrorsSystem.expectMessageWillFire(message);
|
39
|
+
}
|
40
|
+
/**
|
41
|
+
* Expect the callback `fn` to print an error with the message, and otherwise fail.
|
42
|
+
*/
|
43
|
+
|
44
|
+
|
45
|
+
function expectConsoleError(message, fn) {
|
46
|
+
return consoleErrorsSystem.expectMessage(message, fn);
|
47
|
+
}
|
48
|
+
/**
|
49
|
+
* Expect the callback `fn` to trigger all console errors (in sequence),
|
50
|
+
* and otherwise fail.
|
51
|
+
*/
|
52
|
+
|
53
|
+
|
54
|
+
function expectConsoleErrorsMany(messages, fn) {
|
55
|
+
return consoleErrorsSystem.expectMessageMany(messages, fn);
|
37
56
|
}
|
38
57
|
|
39
58
|
module.exports = {
|
40
|
-
disallowConsoleErrors: disallowConsoleErrors
|
59
|
+
disallowConsoleErrors: disallowConsoleErrors,
|
60
|
+
expectConsoleErrorWillFire: expectConsoleErrorWillFire,
|
61
|
+
expectConsoleError: expectConsoleError,
|
62
|
+
expectConsoleErrorsMany: expectConsoleErrorsMany
|
41
63
|
};
|
@@ -0,0 +1,103 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
* @emails oncall+relay
|
8
|
+
*
|
9
|
+
* @format
|
10
|
+
*/
|
11
|
+
'use strict';
|
12
|
+
/* global jest, afterEach */
|
13
|
+
|
14
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
15
|
+
|
16
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
17
|
+
|
18
|
+
var originalConsoleError = console.error;
|
19
|
+
|
20
|
+
function createConsoleInterceptionSystem(typename, expectFunctionName, setUpMock) {
|
21
|
+
var installed = false;
|
22
|
+
var expectedMessages = [];
|
23
|
+
var contextualExpectedMessage = [];
|
24
|
+
var typenameCap = typename.charAt(0).toUpperCase() + typename.slice(1);
|
25
|
+
var typenameCapPlural = typenameCap + 's';
|
26
|
+
var installerName = "disallow".concat(typenameCap, "s");
|
27
|
+
|
28
|
+
function handleMessage(message) {
|
29
|
+
var index = expectedMessages.findIndex(function (expected) {
|
30
|
+
return message.startsWith(expected);
|
31
|
+
});
|
32
|
+
|
33
|
+
if (contextualExpectedMessage.length > 0 && contextualExpectedMessage[0] === message) {
|
34
|
+
contextualExpectedMessage.shift();
|
35
|
+
} else if (index >= 0) {
|
36
|
+
expectedMessages.splice(index, 1);
|
37
|
+
} else {
|
38
|
+
// log to console in case the error gets swallowed somewhere
|
39
|
+
originalConsoleError("Unexpected ".concat(typenameCap, ": ") + message);
|
40
|
+
throw new Error("".concat(typenameCap, ": ") + message);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
function disallowMessages() {
|
45
|
+
if (installed) {
|
46
|
+
throw new Error("".concat(installerName, " should be called only once."));
|
47
|
+
}
|
48
|
+
|
49
|
+
installed = true;
|
50
|
+
setUpMock(handleMessage);
|
51
|
+
afterEach(function () {
|
52
|
+
contextualExpectedMessage.length = 0;
|
53
|
+
|
54
|
+
if (expectedMessages.length > 0) {
|
55
|
+
var error = new Error("Some expected ".concat(typename, "s where not triggered:\n\n") + Array.from(expectedMessages, function (message) {
|
56
|
+
return " * ".concat(message);
|
57
|
+
}).join('\n') + '\n');
|
58
|
+
expectedMessages.length = 0;
|
59
|
+
throw error;
|
60
|
+
}
|
61
|
+
});
|
62
|
+
}
|
63
|
+
|
64
|
+
function expectMessageWillFire(message) {
|
65
|
+
if (!installed) {
|
66
|
+
throw new Error("".concat(installerName, " needs to be called before expect").concat(typenameCapPlural, "WillFire"));
|
67
|
+
}
|
68
|
+
|
69
|
+
expectedMessages.push(message);
|
70
|
+
}
|
71
|
+
|
72
|
+
function expectMessage(message, fn) {
|
73
|
+
return expectMessageMany([message], fn);
|
74
|
+
}
|
75
|
+
|
76
|
+
function expectMessageMany(messages, fn) {
|
77
|
+
if (contextualExpectedMessage.length > 0) {
|
78
|
+
throw new Error("Cannot nest ".concat(expectFunctionName, "() calls."));
|
79
|
+
}
|
80
|
+
|
81
|
+
contextualExpectedMessage.push.apply(contextualExpectedMessage, (0, _toConsumableArray2["default"])(messages));
|
82
|
+
var result = fn();
|
83
|
+
|
84
|
+
if (contextualExpectedMessage.length > 0) {
|
85
|
+
var notFired = contextualExpectedMessage.toString();
|
86
|
+
contextualExpectedMessage.length = 0;
|
87
|
+
throw new Error("Expected ".concat(typename, " in callback: ").concat(notFired));
|
88
|
+
}
|
89
|
+
|
90
|
+
return result;
|
91
|
+
}
|
92
|
+
|
93
|
+
return {
|
94
|
+
disallowMessages: disallowMessages,
|
95
|
+
expectMessageWillFire: expectMessageWillFire,
|
96
|
+
expectMessage: expectMessage,
|
97
|
+
expectMessageMany: expectMessageMany
|
98
|
+
};
|
99
|
+
}
|
100
|
+
|
101
|
+
module.exports = {
|
102
|
+
createConsoleInterceptionSystem: createConsoleInterceptionSystem
|
103
|
+
};
|
package/lib/index.js
CHANGED
@@ -11,7 +11,10 @@
|
|
11
11
|
'use strict';
|
12
12
|
|
13
13
|
var _require = require('./consoleError'),
|
14
|
-
disallowConsoleErrors = _require.disallowConsoleErrors
|
14
|
+
disallowConsoleErrors = _require.disallowConsoleErrors,
|
15
|
+
expectConsoleError = _require.expectConsoleError,
|
16
|
+
expectConsoleErrorsMany = _require.expectConsoleErrorsMany,
|
17
|
+
expectConsoleErrorWillFire = _require.expectConsoleErrorWillFire;
|
15
18
|
|
16
19
|
var describeWithFeatureFlags = require('./describeWithFeatureFlags');
|
17
20
|
|
@@ -58,6 +61,9 @@ module.exports = {
|
|
58
61
|
describeWithFeatureFlags: describeWithFeatureFlags,
|
59
62
|
disallowConsoleErrors: disallowConsoleErrors,
|
60
63
|
disallowWarnings: disallowWarnings,
|
64
|
+
expectConsoleError: expectConsoleError,
|
65
|
+
expectConsoleErrorsMany: expectConsoleErrorsMany,
|
66
|
+
expectConsoleErrorWillFire: expectConsoleErrorWillFire,
|
61
67
|
expectToWarn: expectToWarn,
|
62
68
|
expectToWarnMany: expectToWarnMany,
|
63
69
|
expectWarningWillFire: expectWarningWillFire,
|
package/lib/warnings.js
CHANGED
@@ -9,29 +9,12 @@
|
|
9
9
|
* @format
|
10
10
|
*/
|
11
11
|
'use strict';
|
12
|
-
/* global jest
|
12
|
+
/* global jest */
|
13
13
|
|
14
|
-
var
|
14
|
+
var _require = require('./consoleErrorsAndWarnings'),
|
15
|
+
createConsoleInterceptionSystem = _require.createConsoleInterceptionSystem;
|
15
16
|
|
16
|
-
var
|
17
|
-
|
18
|
-
var installed = false;
|
19
|
-
var expectedWarnings = [];
|
20
|
-
var contextualExpectedWarning = [];
|
21
|
-
/**
|
22
|
-
* Mocks the `warning` module to turn warnings into errors. Any expected
|
23
|
-
* warnings need to be explicitly expected with `expectWarningWillFire(message)`.
|
24
|
-
*
|
25
|
-
* NOTE: This should be called on top of a test file. The test should NOT
|
26
|
-
* use `jest.resetModules()` or manually mock `warning`.
|
27
|
-
*/
|
28
|
-
|
29
|
-
function disallowWarnings() {
|
30
|
-
if (installed) {
|
31
|
-
throw new Error('`disallowWarnings` should be called at most once');
|
32
|
-
}
|
33
|
-
|
34
|
-
installed = true;
|
17
|
+
var warningsSystem = createConsoleInterceptionSystem('warning', 'expectToWarn', function (impl) {
|
35
18
|
jest.mock("fbjs/lib/warning", function () {
|
36
19
|
return jest.fn(function (condition, format) {
|
37
20
|
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
@@ -43,31 +26,21 @@ function disallowWarnings() {
|
|
43
26
|
var message = format.replace(/%s/g, function () {
|
44
27
|
return String(args[argIndex++]);
|
45
28
|
});
|
46
|
-
|
47
|
-
|
48
|
-
if (contextualExpectedWarning.length > 0 && contextualExpectedWarning[0] === message) {
|
49
|
-
contextualExpectedWarning.shift();
|
50
|
-
} else if (index >= 0) {
|
51
|
-
expectedWarnings.splice(index, 1);
|
52
|
-
} else {
|
53
|
-
// log to console in case the error gets swallowed somewhere
|
54
|
-
console.error('Unexpected Warning: ' + message);
|
55
|
-
throw new Error('Warning: ' + message);
|
56
|
-
}
|
29
|
+
impl(message);
|
57
30
|
}
|
58
31
|
});
|
59
32
|
});
|
60
|
-
|
61
|
-
|
33
|
+
});
|
34
|
+
/**
|
35
|
+
* Mocks the `warning` module to turn warnings into errors. Any expected
|
36
|
+
* warnings need to be explicitly expected with `expectWarningWillFire(message)`.
|
37
|
+
*
|
38
|
+
* NOTE: This should be called on top of a test file. The test should NOT
|
39
|
+
* use `jest.resetModules()` or manually mock `warning`.
|
40
|
+
*/
|
62
41
|
|
63
|
-
|
64
|
-
|
65
|
-
return " * ".concat(message);
|
66
|
-
}).join('\n') + '\n');
|
67
|
-
expectedWarnings.length = 0;
|
68
|
-
throw error;
|
69
|
-
}
|
70
|
-
});
|
42
|
+
function disallowWarnings() {
|
43
|
+
warningsSystem.disallowMessages();
|
71
44
|
}
|
72
45
|
/**
|
73
46
|
* Expect a warning with the given message. If the message isn't fired in the
|
@@ -76,11 +49,7 @@ function disallowWarnings() {
|
|
76
49
|
|
77
50
|
|
78
51
|
function expectWarningWillFire(message) {
|
79
|
-
|
80
|
-
throw new Error('`disallowWarnings` needs to be called before `expectWarningWillFire`');
|
81
|
-
}
|
82
|
-
|
83
|
-
expectedWarnings.push(message);
|
52
|
+
warningsSystem.expectMessageWillFire(message);
|
84
53
|
}
|
85
54
|
/**
|
86
55
|
* Expect the callback `fn` to trigger the warning message and otherwise fail.
|
@@ -88,7 +57,7 @@ function expectWarningWillFire(message) {
|
|
88
57
|
|
89
58
|
|
90
59
|
function expectToWarn(message, fn) {
|
91
|
-
return
|
60
|
+
return warningsSystem.expectMessage(message, fn);
|
92
61
|
}
|
93
62
|
/**
|
94
63
|
* Expect the callback `fn` to trigger all warning messages (in sequence)
|
@@ -97,20 +66,7 @@ function expectToWarn(message, fn) {
|
|
97
66
|
|
98
67
|
|
99
68
|
function expectToWarnMany(messages, fn) {
|
100
|
-
|
101
|
-
throw new Error('Cannot nest `expectToWarn()` calls.');
|
102
|
-
}
|
103
|
-
|
104
|
-
contextualExpectedWarning.push.apply(contextualExpectedWarning, (0, _toConsumableArray2["default"])(messages));
|
105
|
-
var result = fn();
|
106
|
-
|
107
|
-
if (contextualExpectedWarning.length > 0) {
|
108
|
-
var notFired = contextualExpectedWarning.toString();
|
109
|
-
contextualExpectedWarning.length = 0;
|
110
|
-
throw new Error("Expected callback to warn: ".concat(notFired));
|
111
|
-
}
|
112
|
-
|
113
|
-
return result;
|
69
|
+
return warningsSystem.expectMessageMany(messages, fn);
|
114
70
|
}
|
115
71
|
|
116
72
|
module.exports = {
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "relay-test-utils-internal",
|
3
3
|
"description": "Internal utilities for testing Relay.",
|
4
|
-
"version": "0.0.0-main-
|
4
|
+
"version": "0.0.0-main-3497ce7f",
|
5
5
|
"keywords": [
|
6
6
|
"graphql",
|
7
7
|
"relay"
|
@@ -17,7 +17,7 @@
|
|
17
17
|
"dependencies": {
|
18
18
|
"@babel/runtime": "^7.0.0",
|
19
19
|
"fbjs": "^3.0.2",
|
20
|
-
"relay-runtime": "0.0.0-main-
|
20
|
+
"relay-runtime": "0.0.0-main-3497ce7f"
|
21
21
|
},
|
22
22
|
"directories": {
|
23
23
|
"": "./"
|
@@ -1,4 +1,4 @@
|
|
1
1
|
/**
|
2
|
-
* Relay v0.0.0-main-
|
2
|
+
* Relay v0.0.0-main-3497ce7f
|
3
3
|
*/
|
4
|
-
module.exports=function(e){var
|
4
|
+
module.exports=function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=3)}([function(e,t){e.exports=require("@babel/runtime/helpers/interopRequireDefault")},function(e,t,n){"use strict";var r=n(0)(n(5)),o=console.error;e.exports={createConsoleInterceptionSystem:function(e,t,n){var c=!1,i=[],a=[],s=e.charAt(0).toUpperCase()+e.slice(1),u=s+"s",l="disallow".concat(s,"s");function f(e){var t=i.findIndex((function(t){return e.startsWith(t)}));if(a.length>0&&a[0]===e)a.shift();else{if(!(t>=0))throw o("Unexpected ".concat(s,": ")+e),new Error("".concat(s,": ")+e);i.splice(t,1)}}function p(n,o){if(a.length>0)throw new Error("Cannot nest ".concat(t,"() calls."));a.push.apply(a,(0,r.default)(n));var c=o();if(a.length>0){var i=a.toString();throw a.length=0,new Error("Expected ".concat(e," in callback: ").concat(i))}return c}return{disallowMessages:function(){if(c)throw new Error("".concat(l," should be called only once."));c=!0,n(f),afterEach((function(){if(a.length=0,i.length>0){var t=new Error("Some expected ".concat(e,"s where not triggered:\n\n")+Array.from(i,(function(e){return" * ".concat(e)})).join("\n")+"\n");throw i.length=0,t}}))},expectMessageWillFire:function(e){if(!c)throw new Error("".concat(l," needs to be called before expect").concat(u,"WillFire"));i.push(e)},expectMessage:function(e,t){return p([e],t)},expectMessageMany:p}}}},function(e,t){e.exports=require("relay-runtime")},function(e,t,n){"use strict";var r=n(4),o=r.disallowConsoleErrors,c=r.expectConsoleError,i=r.expectConsoleErrorsMany,a=r.expectConsoleErrorWillFire,s=n(6),u=n(8),l=u.FIXTURE_TAG,f=u.generateTestsFromFixtures,p=n(13),d=n(15),g=n(17),x=n(18),h=x.disallowWarnings,y=x.expectToWarn,v=x.expectToWarnMany,b=x.expectWarningWillFire,w=n(19),m=w.createMockEnvironment,E=w.unwrapContainer;e.exports={cannotReadPropertyOfUndefined__DEPRECATED:function(e){return process.version.match(/^v16\.(.+)$/)?"Cannot read properties of undefined (reading '".concat(e,"')"):"Cannot read property '".concat(e,"' of undefined")},createMockEnvironment:m,describeWithFeatureFlags:s,disallowConsoleErrors:o,disallowWarnings:h,expectConsoleError:c,expectConsoleErrorsMany:i,expectConsoleErrorWillFire:a,expectToWarn:y,expectToWarnMany:v,expectWarningWillFire:b,FIXTURE_TAG:l,generateTestsFromFixtures:f,matchers:p,printAST:d,simpleClone:g,unwrapContainer:E}},function(e,t,n){"use strict";var r=(0,n(1).createConsoleInterceptionSystem)("error","expectConsoleError",(function(e){jest.spyOn(console,"error").mockImplementation(e)}));e.exports={disallowConsoleErrors:function(){r.disallowMessages()},expectConsoleErrorWillFire:function(e){r.expectMessageWillFire(e)},expectConsoleError:function(e,t){return r.expectMessage(e,t)},expectConsoleErrorsMany:function(e,t){return r.expectMessageMany(e,t)}}},function(e,t){e.exports=require("@babel/runtime/helpers/toConsumableArray")},function(e,t,n){"use strict";var r=n(0)(n(7));e.exports=function(e,t,o){describe.each(e)("".concat(t," - Feature flags: %o"),(function(e){var t;beforeEach((function(){var o=n(2).RelayFeatureFlags;t=(0,r.default)({},o),Object.assign(o,e)})),afterEach((function(){var e=n(2).RelayFeatureFlags;Object.assign(e,t)})),o()}))}},function(e,t){e.exports=require("@babel/runtime/helpers/objectSpread2")},function(e,t,n){"use strict";var r=n(0)(n(9)),o=n(10),c=n(11),i=n(12),a=Symbol.for("FIXTURE_TAG");expect.addSnapshotSerializer({print:function(e){return Object.keys(e).map((function(t){return"~~~~~~~~~~ ".concat(t.toUpperCase()," ~~~~~~~~~~\n").concat(e[t])})).join("\n")},test:function(e){return e&&!0===e[a]}}),e.exports={generateTestsFromFixtures:function(e,t){var n=c.readdirSync(e);test("has fixtures in ".concat(e),(function(){expect(n.length>0).toBe(!0)}));var s=n.filter((function(e){return e.startsWith("only.")}));s.length&&(test.skip.each(n.filter((function(e){return!e.startsWith("only.")})))("matches expected output: %s",(function(){})),n=s),test.each(n)("matches expected output: %s",(function(n){var s,u=c.readFileSync(i.join(e,n),"utf8"),l=o(u,t,n);expect((s={},(0,r.default)(s,a,!0),(0,r.default)(s,"input",u),(0,r.default)(s,"output",l),s)).toMatchSnapshot()}))},FIXTURE_TAG:a}},function(e,t){e.exports=require("@babel/runtime/helpers/defineProperty")},function(e,t,n){"use strict";e.exports=function(e,t,n){if(/^# *expected-to-throw/.test(e)||/\.error\.\w+$/.test(n)){var r;try{r=t(e)}catch(e){return"THROWN EXCEPTION:\n\n".concat(e.toString())}throw new Error("Expected test file '".concat(n,"' to throw, but it passed:\n").concat(r))}return t(e)}},function(e,t){e.exports=require("fs")},function(e,t){e.exports=require("path")},function(e,t,n){"use strict";e.exports={toBeDeeplyFrozen:function(e){return function e(t){if(expect(Object.isFrozen(t)).toBe(!0),Array.isArray(t))t.forEach((function(t){return e(t)}));else if("object"==typeof t&&null!==t)for(var n in t)e(t[n])}(e),{pass:!0}},toWarn:function(e,t){var r=this.isNot;function o(e){return e instanceof RegExp?e.toString():JSON.stringify(e)}function c(e){return"["+e.map(o).join(", ")+"]"}function i(e){return e.length?e.map((function(e){return c([!!e[0]].concat(e.slice(1)))})).join(", "):"[]"}var a=n(14);if(!a.mock)throw new Error("toWarn(): Requires `jest.mock('warning')`.");var s=a.mock.calls.length;e();var u=a.mock.calls.slice(s);return t?(Array.isArray(t)||(t=[t]),{pass:!!u.find((function(e){return e.length===t.length+1&&e.every((function(e,n){if(!n)return!e;var r=t[n-1];return r instanceof RegExp?r.test(e):e===r}))})),message:function(){return"Expected ".concat(r?"not ":"","to warn: ")+"".concat(c([!1].concat(t))," but ")+"`warning` received the following calls: "+"".concat(i(u),".")}}):{pass:!!u.filter((function(e){return!e[0]})).length,message:function(){return"Expected ".concat(r?"not ":"","to warn but ")+"`warning` received the following calls: "+"".concat(i(u),".")}}}}},function(e,t){e.exports=require("fbjs/lib/warning")},function(e,t,n){"use strict";var r=n(0)(n(16));e.exports=function(e){return function e(t,n){switch(typeof t){case"undefined":return"undefined";case"object":if(null===t)return"null";if(Array.isArray(t)){if(0===t.length)return"[]";var o,c="[\n",i=n+" ",a=(0,r.default)(t);try{for(a.s();!(o=a.n()).done;){var s=o.value;c+=i+e(s,i)+",\n"}}catch(e){a.e(e)}finally{a.f()}return c+=n+"]"}if("string"==typeof t.kind){for(var u="".concat(t.kind," {\n"),l=n+" ",f=0,p=Object.entries(t);f<p.length;f++){var d=p[f],g=d[0],x=d[1];"kind"!==g&&(u+="".concat(l).concat(g,": ").concat(e(x,l),",\n"))}return u+=n+"}"}if("function"==typeof t.toJSON)return e(t.toJSON(),n);for(var h="{\n",y=n+" ",v=0,b=Object.entries(t);v<b.length;v++){var w=b[v],m=w[0],E=w[1];h+="".concat(y).concat(JSON.stringify(m),": ").concat(e(E,y),",\n")}return h+=n+"}";case"string":case"number":case"boolean":return JSON.stringify(t,null,2).replace("\n","\n"+n);default:throw new Error("printAST doesn't handle values where "+"typeof value === '".concat(typeof t,"'."))}}(e,"")}},function(e,t){e.exports=require("@babel/runtime/helpers/createForOfIteratorHelper")},function(e,t,n){"use strict";e.exports=function e(t){if(Array.isArray(t))return t.map(e);if(null!=t&&"object"==typeof t){var n={};for(var r in t)n[r]=e(t[r]);return n}return t}},function(e,t,n){"use strict";var r=(0,n(1).createConsoleInterceptionSystem)("warning","expectToWarn",(function(e){jest.mock("fbjs/lib/warning",(function(){return jest.fn((function(t,n){for(var r=arguments.length,o=new Array(r>2?r-2:0),c=2;c<r;c++)o[c-2]=arguments[c];if(!t){var i=0,a=n.replace(/%s/g,(function(){return String(o[i++])}));e(a)}}))}))}));e.exports={disallowWarnings:function(){r.disallowMessages()},expectWarningWillFire:function(e){r.expectMessageWillFire(e)},expectToWarn:function(e,t){return r.expectMessage(e,t)},expectToWarnMany:function(e,t){return r.expectMessageMany(e,t)}}},function(e,t){e.exports=require("relay-test-utils")}]);
|
@@ -1,9 +1,9 @@
|
|
1
1
|
/**
|
2
|
-
* Relay v0.0.0-main-
|
2
|
+
* Relay v0.0.0-main-3497ce7f
|
3
3
|
*
|
4
4
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
5
5
|
*
|
6
6
|
* This source code is licensed under the MIT license found in the
|
7
7
|
* LICENSE file in the root directory of this source tree.
|
8
8
|
*/
|
9
|
-
module.exports=function(e){var
|
9
|
+
module.exports=function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=3)}([function(e,t){e.exports=require("@babel/runtime/helpers/interopRequireDefault")},function(e,t,n){"use strict";var r=n(0)(n(5)),o=console.error;e.exports={createConsoleInterceptionSystem:function(e,t,n){var c=!1,i=[],a=[],s=e.charAt(0).toUpperCase()+e.slice(1),u=s+"s",l="disallow".concat(s,"s");function f(e){var t=i.findIndex((function(t){return e.startsWith(t)}));if(a.length>0&&a[0]===e)a.shift();else{if(!(t>=0))throw o("Unexpected ".concat(s,": ")+e),new Error("".concat(s,": ")+e);i.splice(t,1)}}function p(n,o){if(a.length>0)throw new Error("Cannot nest ".concat(t,"() calls."));a.push.apply(a,(0,r.default)(n));var c=o();if(a.length>0){var i=a.toString();throw a.length=0,new Error("Expected ".concat(e," in callback: ").concat(i))}return c}return{disallowMessages:function(){if(c)throw new Error("".concat(l," should be called only once."));c=!0,n(f),afterEach((function(){if(a.length=0,i.length>0){var t=new Error("Some expected ".concat(e,"s where not triggered:\n\n")+Array.from(i,(function(e){return" * ".concat(e)})).join("\n")+"\n");throw i.length=0,t}}))},expectMessageWillFire:function(e){if(!c)throw new Error("".concat(l," needs to be called before expect").concat(u,"WillFire"));i.push(e)},expectMessage:function(e,t){return p([e],t)},expectMessageMany:p}}}},function(e,t){e.exports=require("relay-runtime")},function(e,t,n){"use strict";var r=n(4),o=r.disallowConsoleErrors,c=r.expectConsoleError,i=r.expectConsoleErrorsMany,a=r.expectConsoleErrorWillFire,s=n(6),u=n(8),l=u.FIXTURE_TAG,f=u.generateTestsFromFixtures,p=n(13),d=n(15),g=n(17),x=n(18),h=x.disallowWarnings,y=x.expectToWarn,v=x.expectToWarnMany,b=x.expectWarningWillFire,w=n(19),m=w.createMockEnvironment,E=w.unwrapContainer;e.exports={cannotReadPropertyOfUndefined__DEPRECATED:function(e){return process.version.match(/^v16\.(.+)$/)?"Cannot read properties of undefined (reading '".concat(e,"')"):"Cannot read property '".concat(e,"' of undefined")},createMockEnvironment:m,describeWithFeatureFlags:s,disallowConsoleErrors:o,disallowWarnings:h,expectConsoleError:c,expectConsoleErrorsMany:i,expectConsoleErrorWillFire:a,expectToWarn:y,expectToWarnMany:v,expectWarningWillFire:b,FIXTURE_TAG:l,generateTestsFromFixtures:f,matchers:p,printAST:d,simpleClone:g,unwrapContainer:E}},function(e,t,n){"use strict";var r=(0,n(1).createConsoleInterceptionSystem)("error","expectConsoleError",(function(e){jest.spyOn(console,"error").mockImplementation(e)}));e.exports={disallowConsoleErrors:function(){r.disallowMessages()},expectConsoleErrorWillFire:function(e){r.expectMessageWillFire(e)},expectConsoleError:function(e,t){return r.expectMessage(e,t)},expectConsoleErrorsMany:function(e,t){return r.expectMessageMany(e,t)}}},function(e,t){e.exports=require("@babel/runtime/helpers/toConsumableArray")},function(e,t,n){"use strict";var r=n(0)(n(7));e.exports=function(e,t,o){describe.each(e)("".concat(t," - Feature flags: %o"),(function(e){var t;beforeEach((function(){var o=n(2).RelayFeatureFlags;t=(0,r.default)({},o),Object.assign(o,e)})),afterEach((function(){var e=n(2).RelayFeatureFlags;Object.assign(e,t)})),o()}))}},function(e,t){e.exports=require("@babel/runtime/helpers/objectSpread2")},function(e,t,n){"use strict";var r=n(0)(n(9)),o=n(10),c=n(11),i=n(12),a=Symbol.for("FIXTURE_TAG");expect.addSnapshotSerializer({print:function(e){return Object.keys(e).map((function(t){return"~~~~~~~~~~ ".concat(t.toUpperCase()," ~~~~~~~~~~\n").concat(e[t])})).join("\n")},test:function(e){return e&&!0===e[a]}}),e.exports={generateTestsFromFixtures:function(e,t){var n=c.readdirSync(e);test("has fixtures in ".concat(e),(function(){expect(n.length>0).toBe(!0)}));var s=n.filter((function(e){return e.startsWith("only.")}));s.length&&(test.skip.each(n.filter((function(e){return!e.startsWith("only.")})))("matches expected output: %s",(function(){})),n=s),test.each(n)("matches expected output: %s",(function(n){var s,u=c.readFileSync(i.join(e,n),"utf8"),l=o(u,t,n);expect((s={},(0,r.default)(s,a,!0),(0,r.default)(s,"input",u),(0,r.default)(s,"output",l),s)).toMatchSnapshot()}))},FIXTURE_TAG:a}},function(e,t){e.exports=require("@babel/runtime/helpers/defineProperty")},function(e,t,n){"use strict";e.exports=function(e,t,n){if(/^# *expected-to-throw/.test(e)||/\.error\.\w+$/.test(n)){var r;try{r=t(e)}catch(e){return"THROWN EXCEPTION:\n\n".concat(e.toString())}throw new Error("Expected test file '".concat(n,"' to throw, but it passed:\n").concat(r))}return t(e)}},function(e,t){e.exports=require("fs")},function(e,t){e.exports=require("path")},function(e,t,n){"use strict";e.exports={toBeDeeplyFrozen:function(e){return function e(t){if(expect(Object.isFrozen(t)).toBe(!0),Array.isArray(t))t.forEach((function(t){return e(t)}));else if("object"==typeof t&&null!==t)for(var n in t)e(t[n])}(e),{pass:!0}},toWarn:function(e,t){var r=this.isNot;function o(e){return e instanceof RegExp?e.toString():JSON.stringify(e)}function c(e){return"["+e.map(o).join(", ")+"]"}function i(e){return e.length?e.map((function(e){return c([!!e[0]].concat(e.slice(1)))})).join(", "):"[]"}var a=n(14);if(!a.mock)throw new Error("toWarn(): Requires `jest.mock('warning')`.");var s=a.mock.calls.length;e();var u=a.mock.calls.slice(s);return t?(Array.isArray(t)||(t=[t]),{pass:!!u.find((function(e){return e.length===t.length+1&&e.every((function(e,n){if(!n)return!e;var r=t[n-1];return r instanceof RegExp?r.test(e):e===r}))})),message:function(){return"Expected ".concat(r?"not ":"","to warn: ")+"".concat(c([!1].concat(t))," but ")+"`warning` received the following calls: "+"".concat(i(u),".")}}):{pass:!!u.filter((function(e){return!e[0]})).length,message:function(){return"Expected ".concat(r?"not ":"","to warn but ")+"`warning` received the following calls: "+"".concat(i(u),".")}}}}},function(e,t){e.exports=require("fbjs/lib/warning")},function(e,t,n){"use strict";var r=n(0)(n(16));e.exports=function(e){return function e(t,n){switch(typeof t){case"undefined":return"undefined";case"object":if(null===t)return"null";if(Array.isArray(t)){if(0===t.length)return"[]";var o,c="[\n",i=n+" ",a=(0,r.default)(t);try{for(a.s();!(o=a.n()).done;){var s=o.value;c+=i+e(s,i)+",\n"}}catch(e){a.e(e)}finally{a.f()}return c+=n+"]"}if("string"==typeof t.kind){for(var u="".concat(t.kind," {\n"),l=n+" ",f=0,p=Object.entries(t);f<p.length;f++){var d=p[f],g=d[0],x=d[1];"kind"!==g&&(u+="".concat(l).concat(g,": ").concat(e(x,l),",\n"))}return u+=n+"}"}if("function"==typeof t.toJSON)return e(t.toJSON(),n);for(var h="{\n",y=n+" ",v=0,b=Object.entries(t);v<b.length;v++){var w=b[v],m=w[0],E=w[1];h+="".concat(y).concat(JSON.stringify(m),": ").concat(e(E,y),",\n")}return h+=n+"}";case"string":case"number":case"boolean":return JSON.stringify(t,null,2).replace("\n","\n"+n);default:throw new Error("printAST doesn't handle values where "+"typeof value === '".concat(typeof t,"'."))}}(e,"")}},function(e,t){e.exports=require("@babel/runtime/helpers/createForOfIteratorHelper")},function(e,t,n){"use strict";e.exports=function e(t){if(Array.isArray(t))return t.map(e);if(null!=t&&"object"==typeof t){var n={};for(var r in t)n[r]=e(t[r]);return n}return t}},function(e,t,n){"use strict";var r=(0,n(1).createConsoleInterceptionSystem)("warning","expectToWarn",(function(e){jest.mock("fbjs/lib/warning",(function(){return jest.fn((function(t,n){for(var r=arguments.length,o=new Array(r>2?r-2:0),c=2;c<r;c++)o[c-2]=arguments[c];if(!t){var i=0,a=n.replace(/%s/g,(function(){return String(o[i++])}));e(a)}}))}))}));e.exports={disallowWarnings:function(){r.disallowMessages()},expectWarningWillFire:function(e){r.expectMessageWillFire(e)},expectToWarn:function(e,t){return r.expectMessage(e,t)},expectToWarnMany:function(e,t){return r.expectMessageMany(e,t)}}},function(e,t){e.exports=require("relay-test-utils")}]);
|
package/warnings.js.flow
CHANGED
@@ -11,11 +11,25 @@
|
|
11
11
|
|
12
12
|
'use strict';
|
13
13
|
|
14
|
-
/* global jest
|
14
|
+
/* global jest */
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
const
|
16
|
+
const {createConsoleInterceptionSystem} = require('./consoleErrorsAndWarnings');
|
17
|
+
|
18
|
+
const warningsSystem = createConsoleInterceptionSystem(
|
19
|
+
'warning',
|
20
|
+
'expectToWarn',
|
21
|
+
impl => {
|
22
|
+
jest.mock('warning', () =>
|
23
|
+
jest.fn((condition, format, ...args) => {
|
24
|
+
if (!condition) {
|
25
|
+
let argIndex = 0;
|
26
|
+
const message = format.replace(/%s/g, () => String(args[argIndex++]));
|
27
|
+
impl(message);
|
28
|
+
}
|
29
|
+
}),
|
30
|
+
);
|
31
|
+
},
|
32
|
+
);
|
19
33
|
|
20
34
|
/**
|
21
35
|
* Mocks the `warning` module to turn warnings into errors. Any expected
|
@@ -25,44 +39,7 @@ const contextualExpectedWarning: Array<string> = [];
|
|
25
39
|
* use `jest.resetModules()` or manually mock `warning`.
|
26
40
|
*/
|
27
41
|
function disallowWarnings(): void {
|
28
|
-
|
29
|
-
throw new Error('`disallowWarnings` should be called at most once');
|
30
|
-
}
|
31
|
-
installed = true;
|
32
|
-
jest.mock('warning', () => {
|
33
|
-
return jest.fn((condition, format, ...args) => {
|
34
|
-
if (!condition) {
|
35
|
-
let argIndex = 0;
|
36
|
-
const message = format.replace(/%s/g, () => String(args[argIndex++]));
|
37
|
-
const index = expectedWarnings.indexOf(message);
|
38
|
-
|
39
|
-
if (
|
40
|
-
contextualExpectedWarning.length > 0 &&
|
41
|
-
contextualExpectedWarning[0] === message
|
42
|
-
) {
|
43
|
-
contextualExpectedWarning.shift();
|
44
|
-
} else if (index >= 0) {
|
45
|
-
expectedWarnings.splice(index, 1);
|
46
|
-
} else {
|
47
|
-
// log to console in case the error gets swallowed somewhere
|
48
|
-
console.error('Unexpected Warning: ' + message);
|
49
|
-
throw new Error('Warning: ' + message);
|
50
|
-
}
|
51
|
-
}
|
52
|
-
});
|
53
|
-
});
|
54
|
-
afterEach(() => {
|
55
|
-
contextualExpectedWarning.length = 0;
|
56
|
-
if (expectedWarnings.length > 0) {
|
57
|
-
const error = new Error(
|
58
|
-
'Some expected warnings where not triggered:\n\n' +
|
59
|
-
Array.from(expectedWarnings, message => ` * ${message}`).join('\n') +
|
60
|
-
'\n',
|
61
|
-
);
|
62
|
-
expectedWarnings.length = 0;
|
63
|
-
throw error;
|
64
|
-
}
|
65
|
-
});
|
42
|
+
warningsSystem.disallowMessages();
|
66
43
|
}
|
67
44
|
|
68
45
|
/**
|
@@ -70,19 +47,14 @@ function disallowWarnings(): void {
|
|
70
47
|
* current test, the test will fail.
|
71
48
|
*/
|
72
49
|
function expectWarningWillFire(message: string): void {
|
73
|
-
|
74
|
-
throw new Error(
|
75
|
-
'`disallowWarnings` needs to be called before `expectWarningWillFire`',
|
76
|
-
);
|
77
|
-
}
|
78
|
-
expectedWarnings.push(message);
|
50
|
+
warningsSystem.expectMessageWillFire(message);
|
79
51
|
}
|
80
52
|
|
81
53
|
/**
|
82
54
|
* Expect the callback `fn` to trigger the warning message and otherwise fail.
|
83
55
|
*/
|
84
56
|
function expectToWarn<T>(message: string, fn: () => T): T {
|
85
|
-
return
|
57
|
+
return warningsSystem.expectMessage(message, fn);
|
86
58
|
}
|
87
59
|
|
88
60
|
/**
|
@@ -90,17 +62,7 @@ function expectToWarn<T>(message: string, fn: () => T): T {
|
|
90
62
|
* or otherwise fail.
|
91
63
|
*/
|
92
64
|
function expectToWarnMany<T>(messages: Array<string>, fn: () => T): T {
|
93
|
-
|
94
|
-
throw new Error('Cannot nest `expectToWarn()` calls.');
|
95
|
-
}
|
96
|
-
contextualExpectedWarning.push(...messages);
|
97
|
-
const result = fn();
|
98
|
-
if (contextualExpectedWarning.length > 0) {
|
99
|
-
const notFired = contextualExpectedWarning.toString();
|
100
|
-
contextualExpectedWarning.length = 0;
|
101
|
-
throw new Error(`Expected callback to warn: ${notFired}`);
|
102
|
-
}
|
103
|
-
return result;
|
65
|
+
return warningsSystem.expectMessageMany(messages, fn);
|
104
66
|
}
|
105
67
|
|
106
68
|
module.exports = {
|