relay-test-utils-internal 13.1.1 → 14.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
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 */
15
+
16
+ import type {WillFireOptions} from './consoleErrorsAndWarnings';
17
+
18
+ const {createConsoleInterceptionSystem} = require('./consoleErrorsAndWarnings');
19
+
20
+ const consoleErrorsSystem = createConsoleInterceptionSystem(
21
+ 'error',
22
+ 'expectConsoleError',
23
+ impl => {
24
+ jest.spyOn(console, 'error').mockImplementation(impl);
25
+ },
26
+ );
27
+
28
+ /**
29
+ * Mocks console.error so that errors printed to the console are instead thrown.
30
+ * Any expected errors need to be explicitly expected with `expectConsoleErrorWillFire(message)`.
31
+ *
32
+ * NOTE: This should be called on top of a test file. The test should NOT
33
+ * use `jest.resetModules()` or manually mock `console`.
34
+ */
35
+ function disallowConsoleErrors(): void {
36
+ consoleErrorsSystem.disallowMessages();
37
+ }
38
+
39
+ /**
40
+ * Expect an error with the given message. If the message isn't fired in the
41
+ * current test, the test will fail.
42
+ */
43
+ function expectConsoleErrorWillFire(
44
+ message: string,
45
+ options?: WillFireOptions,
46
+ ): void {
47
+ consoleErrorsSystem.expectMessageWillFire(message, options);
48
+ }
49
+
50
+ /**
51
+ * Expect the callback `fn` to print an error with the message, and otherwise fail.
52
+ */
53
+ function expectConsoleError<T>(message: string, fn: () => T): T {
54
+ return consoleErrorsSystem.expectMessage(message, fn);
55
+ }
56
+
57
+ /**
58
+ * Expect the callback `fn` to trigger all console errors (in sequence),
59
+ * and otherwise fail.
60
+ */
61
+ function expectConsoleErrorsMany<T>(messages: Array<string>, fn: () => T): T {
62
+ return consoleErrorsSystem.expectMessageMany(messages, fn);
63
+ }
64
+
65
+ module.exports = {
66
+ disallowConsoleErrors,
67
+ expectConsoleErrorWillFire,
68
+ expectConsoleError,
69
+ expectConsoleErrorsMany,
70
+ };
@@ -0,0 +1,134 @@
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 afterEach */
15
+
16
+ export type WillFireOptions = {
17
+ count?: number,
18
+ optional?: boolean,
19
+ };
20
+
21
+ type API = $ReadOnly<{
22
+ disallowMessages: () => void,
23
+ expectMessageWillFire: (string, void | WillFireOptions) => void,
24
+ expectMessage: <T>(string, () => T) => T,
25
+ expectMessageMany: <T>(Array<string>, () => T) => T,
26
+ }>;
27
+
28
+ const originalConsoleError = console.error;
29
+
30
+ function createConsoleInterceptionSystem(
31
+ typename: string,
32
+ expectFunctionName: string,
33
+ setUpMock: ((string) => void) => void,
34
+ ): API {
35
+ let installed = false;
36
+ const expectedMessages: Array<string> = [];
37
+ const optionalMessages: Array<string> = [];
38
+ const contextualExpectedMessage: Array<string> = [];
39
+
40
+ const typenameCap = typename.charAt(0).toUpperCase() + typename.slice(1);
41
+ const typenameCapPlural = typenameCap + 's';
42
+ const installerName = `disallow${typenameCap}s`;
43
+
44
+ function handleMessage(message: string): void {
45
+ const index = expectedMessages.findIndex(expected =>
46
+ message.startsWith(expected),
47
+ );
48
+ const optionalIndex = optionalMessages.findIndex(expected =>
49
+ message.startsWith(expected),
50
+ );
51
+ if (
52
+ contextualExpectedMessage.length > 0 &&
53
+ message.startsWith(contextualExpectedMessage[0])
54
+ ) {
55
+ contextualExpectedMessage.shift();
56
+ } else if (index >= 0) {
57
+ expectedMessages.splice(index, 1);
58
+ } else if (optionalIndex >= 0) {
59
+ optionalMessages.splice(optionalIndex, 1);
60
+ } else {
61
+ // log to console in case the error gets swallowed somewhere
62
+ originalConsoleError(`Unexpected ${typenameCap}: ` + message);
63
+ throw new Error(`${typenameCap}: ` + message);
64
+ }
65
+ }
66
+
67
+ function disallowMessages(): void {
68
+ if (installed) {
69
+ throw new Error(`${installerName} should be called only once.`);
70
+ }
71
+ installed = true;
72
+ setUpMock(handleMessage);
73
+
74
+ afterEach(() => {
75
+ optionalMessages.length = 0;
76
+ contextualExpectedMessage.length = 0;
77
+ if (expectedMessages.length > 0) {
78
+ const error = new Error(
79
+ `Some ${expectedMessages.length} expected ${typename}s where not triggered:\n\n` +
80
+ Array.from(expectedMessages, message => ` * ${message}`).join(
81
+ '\n',
82
+ ) +
83
+ '\n',
84
+ );
85
+ expectedMessages.length = 0;
86
+ throw error;
87
+ }
88
+ });
89
+ }
90
+
91
+ function expectMessageWillFire(
92
+ message: string,
93
+ options?: WillFireOptions,
94
+ ): void {
95
+ if (!installed) {
96
+ throw new Error(
97
+ `${installerName} needs to be called before expect${typenameCapPlural}WillFire`,
98
+ );
99
+ }
100
+ const optional = options?.optional === true; // avoid "sketchy null check"
101
+ for (let i = 0; i < (options?.count ?? 1); i++) {
102
+ (optional ? optionalMessages : expectedMessages).push(message);
103
+ }
104
+ }
105
+
106
+ function expectMessage<T>(message: string, fn: () => T): T {
107
+ return expectMessageMany([message], fn);
108
+ }
109
+
110
+ function expectMessageMany<T>(messages: Array<string>, fn: () => T): T {
111
+ if (contextualExpectedMessage.length > 0) {
112
+ throw new Error(`Cannot nest ${expectFunctionName}() calls.`);
113
+ }
114
+ contextualExpectedMessage.push(...messages);
115
+ const result = fn();
116
+ if (contextualExpectedMessage.length > 0) {
117
+ const notFired = contextualExpectedMessage.toString();
118
+ contextualExpectedMessage.length = 0;
119
+ throw new Error(`Expected ${typename} in callback: ${notFired}`);
120
+ }
121
+ return result;
122
+ }
123
+
124
+ return {
125
+ disallowMessages,
126
+ expectMessageWillFire,
127
+ expectMessage,
128
+ expectMessageMany,
129
+ };
130
+ }
131
+
132
+ module.exports = {
133
+ createConsoleInterceptionSystem,
134
+ };
@@ -0,0 +1,70 @@
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 */
15
+
16
+ import type {WillFireOptions} from './consoleErrorsAndWarnings';
17
+
18
+ const {createConsoleInterceptionSystem} = require('./consoleErrorsAndWarnings');
19
+
20
+ const consoleWarningsSystem = createConsoleInterceptionSystem(
21
+ 'warning',
22
+ 'expectConsoleWarning',
23
+ impl => {
24
+ jest.spyOn(console, 'warn').mockImplementation(impl);
25
+ },
26
+ );
27
+
28
+ /**
29
+ * Mocks console.warn so that warnings printed to the console are instead thrown.
30
+ * Any expected warnings need to be explicitly expected with `expectConsoleWarningWillFire(message)`.
31
+ *
32
+ * NOTE: This should be called on top of a test file. The test should NOT
33
+ * use `jest.resetModules()` or manually mock `console`.
34
+ */
35
+ function disallowConsoleWarnings(): void {
36
+ consoleWarningsSystem.disallowMessages();
37
+ }
38
+
39
+ /**
40
+ * Expect a warning with the given message. If the message isn't fired in the
41
+ * current test, the test will fail.
42
+ */
43
+ function expectConsoleWarningWillFire(
44
+ message: string,
45
+ options?: WillFireOptions,
46
+ ): void {
47
+ consoleWarningsSystem.expectMessageWillFire(message, options);
48
+ }
49
+
50
+ /**
51
+ * Expect the callback `fn` to print a warning with the message, and otherwise fail.
52
+ */
53
+ function expectConsoleWarning<T>(message: string, fn: () => T): T {
54
+ return consoleWarningsSystem.expectMessage(message, fn);
55
+ }
56
+
57
+ /**
58
+ * Expect the callback `fn` to trigger all console warnings (in sequence),
59
+ * and otherwise fail.
60
+ */
61
+ function expectConsoleWarningsMany<T>(messages: Array<string>, fn: () => T): T {
62
+ return consoleWarningsSystem.expectMessageMany(messages, fn);
63
+ }
64
+
65
+ module.exports = {
66
+ disallowConsoleWarnings,
67
+ expectConsoleWarningWillFire,
68
+ expectConsoleWarning,
69
+ expectConsoleWarningsMany,
70
+ };
@@ -13,8 +13,6 @@
13
13
  * Beware that calling jest.resetModules() within the suite may break this.
14
14
  */
15
15
 
16
- // flowlint ambiguous-object-type:error
17
-
18
16
  'use strict';
19
17
 
20
18
  import type {FeatureFlags} from '../relay-runtime/util/RelayFeatureFlags';
@@ -8,8 +8,6 @@
8
8
  * @format
9
9
  */
10
10
 
11
- // flowlint ambiguous-object-type:error
12
-
13
11
  'use strict';
14
12
 
15
13
  function getOutputForFixture(
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Relay v13.1.1
2
+ * Relay v14.1.0
3
3
  *
4
4
  * Copyright (c) Meta Platforms, Inc. and affiliates.
5
5
  *
package/index.js.flow CHANGED
@@ -8,10 +8,20 @@
8
8
  * @format
9
9
  */
10
10
 
11
- // flowlint ambiguous-object-type:error
12
-
13
11
  'use strict';
14
12
 
13
+ const {
14
+ disallowConsoleErrors,
15
+ expectConsoleError,
16
+ expectConsoleErrorsMany,
17
+ expectConsoleErrorWillFire,
18
+ } = require('./consoleError');
19
+ const {
20
+ disallowConsoleWarnings,
21
+ expectConsoleWarning,
22
+ expectConsoleWarningsMany,
23
+ expectConsoleWarningWillFire,
24
+ } = require('./consoleWarning');
15
25
  const describeWithFeatureFlags = require('./describeWithFeatureFlags');
16
26
  const {
17
27
  FIXTURE_TAG,
@@ -20,6 +30,7 @@ const {
20
30
  const Matchers = require('./Matchers');
21
31
  const printAST = require('./printAST');
22
32
  const simpleClone = require('./simpleClone');
33
+ const trackRetentionForEnvironment = require('./trackRetentionForEnvironment');
23
34
  const {
24
35
  disallowWarnings,
25
36
  expectToWarn,
@@ -50,14 +61,23 @@ module.exports = {
50
61
  cannotReadPropertyOfUndefined__DEPRECATED,
51
62
  createMockEnvironment,
52
63
  describeWithFeatureFlags,
64
+ disallowConsoleErrors,
65
+ disallowConsoleWarnings,
66
+ disallowWarnings,
67
+ expectConsoleError,
68
+ expectConsoleErrorsMany,
69
+ expectConsoleErrorWillFire,
70
+ expectConsoleWarningWillFire,
71
+ expectConsoleWarning,
72
+ expectConsoleWarningsMany,
53
73
  expectToWarn,
54
74
  expectToWarnMany,
55
75
  expectWarningWillFire,
56
- disallowWarnings,
57
76
  FIXTURE_TAG,
58
77
  generateTestsFromFixtures,
59
78
  matchers: Matchers,
60
79
  printAST,
61
80
  simpleClone,
81
+ trackRetentionForEnvironment,
62
82
  unwrapContainer,
63
83
  };
@@ -0,0 +1,63 @@
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 */
13
+
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
+ });
20
+ /**
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`.
26
+ */
27
+
28
+ function disallowConsoleErrors() {
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, options) {
38
+ consoleErrorsSystem.expectMessageWillFire(message, options);
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);
56
+ }
57
+
58
+ module.exports = {
59
+ disallowConsoleErrors: disallowConsoleErrors,
60
+ expectConsoleErrorWillFire: expectConsoleErrorWillFire,
61
+ expectConsoleError: expectConsoleError,
62
+ expectConsoleErrorsMany: expectConsoleErrorsMany
63
+ };
@@ -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
+ *
9
+ * @format
10
+ */
11
+ 'use strict';
12
+ /* global afterEach */
13
+
14
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
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 optionalMessages = [];
24
+ var contextualExpectedMessage = [];
25
+ var typenameCap = typename.charAt(0).toUpperCase() + typename.slice(1);
26
+ var typenameCapPlural = typenameCap + 's';
27
+ var installerName = "disallow".concat(typenameCap, "s");
28
+
29
+ function handleMessage(message) {
30
+ var index = expectedMessages.findIndex(function (expected) {
31
+ return message.startsWith(expected);
32
+ });
33
+ var optionalIndex = optionalMessages.findIndex(function (expected) {
34
+ return message.startsWith(expected);
35
+ });
36
+
37
+ if (contextualExpectedMessage.length > 0 && message.startsWith(contextualExpectedMessage[0])) {
38
+ contextualExpectedMessage.shift();
39
+ } else if (index >= 0) {
40
+ expectedMessages.splice(index, 1);
41
+ } else if (optionalIndex >= 0) {
42
+ optionalMessages.splice(optionalIndex, 1);
43
+ } else {
44
+ // log to console in case the error gets swallowed somewhere
45
+ originalConsoleError("Unexpected ".concat(typenameCap, ": ") + message);
46
+ throw new Error("".concat(typenameCap, ": ") + message);
47
+ }
48
+ }
49
+
50
+ function disallowMessages() {
51
+ if (installed) {
52
+ throw new Error("".concat(installerName, " should be called only once."));
53
+ }
54
+
55
+ installed = true;
56
+ setUpMock(handleMessage);
57
+ afterEach(function () {
58
+ optionalMessages.length = 0;
59
+ contextualExpectedMessage.length = 0;
60
+
61
+ if (expectedMessages.length > 0) {
62
+ var error = new Error("Some ".concat(expectedMessages.length, " expected ").concat(typename, "s where not triggered:\n\n") + Array.from(expectedMessages, function (message) {
63
+ return " * ".concat(message);
64
+ }).join('\n') + '\n');
65
+ expectedMessages.length = 0;
66
+ throw error;
67
+ }
68
+ });
69
+ }
70
+
71
+ function expectMessageWillFire(message, options) {
72
+ if (!installed) {
73
+ throw new Error("".concat(installerName, " needs to be called before expect").concat(typenameCapPlural, "WillFire"));
74
+ }
75
+
76
+ var optional = (options === null || options === void 0 ? void 0 : options.optional) === true; // avoid "sketchy null check"
77
+
78
+ for (var i = 0; i < ((_options$count = options === null || options === void 0 ? void 0 : options.count) !== null && _options$count !== void 0 ? _options$count : 1); i++) {
79
+ var _options$count;
80
+
81
+ (optional ? optionalMessages : expectedMessages).push(message);
82
+ }
83
+ }
84
+
85
+ function expectMessage(message, fn) {
86
+ return expectMessageMany([message], fn);
87
+ }
88
+
89
+ function expectMessageMany(messages, fn) {
90
+ if (contextualExpectedMessage.length > 0) {
91
+ throw new Error("Cannot nest ".concat(expectFunctionName, "() calls."));
92
+ }
93
+
94
+ contextualExpectedMessage.push.apply(contextualExpectedMessage, (0, _toConsumableArray2["default"])(messages));
95
+ var result = fn();
96
+
97
+ if (contextualExpectedMessage.length > 0) {
98
+ var notFired = contextualExpectedMessage.toString();
99
+ contextualExpectedMessage.length = 0;
100
+ throw new Error("Expected ".concat(typename, " in callback: ").concat(notFired));
101
+ }
102
+
103
+ return result;
104
+ }
105
+
106
+ return {
107
+ disallowMessages: disallowMessages,
108
+ expectMessageWillFire: expectMessageWillFire,
109
+ expectMessage: expectMessage,
110
+ expectMessageMany: expectMessageMany
111
+ };
112
+ }
113
+
114
+ module.exports = {
115
+ createConsoleInterceptionSystem: createConsoleInterceptionSystem
116
+ };
@@ -0,0 +1,63 @@
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 */
13
+
14
+ var _require = require('./consoleErrorsAndWarnings'),
15
+ createConsoleInterceptionSystem = _require.createConsoleInterceptionSystem;
16
+
17
+ var consoleWarningsSystem = createConsoleInterceptionSystem('warning', 'expectConsoleWarning', function (impl) {
18
+ jest.spyOn(console, 'warn').mockImplementation(impl);
19
+ });
20
+ /**
21
+ * Mocks console.warn so that warnings printed to the console are instead thrown.
22
+ * Any expected warnings need to be explicitly expected with `expectConsoleWarningWillFire(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`.
26
+ */
27
+
28
+ function disallowConsoleWarnings() {
29
+ consoleWarningsSystem.disallowMessages();
30
+ }
31
+ /**
32
+ * Expect a warning with the given message. If the message isn't fired in the
33
+ * current test, the test will fail.
34
+ */
35
+
36
+
37
+ function expectConsoleWarningWillFire(message, options) {
38
+ consoleWarningsSystem.expectMessageWillFire(message, options);
39
+ }
40
+ /**
41
+ * Expect the callback `fn` to print a warning with the message, and otherwise fail.
42
+ */
43
+
44
+
45
+ function expectConsoleWarning(message, fn) {
46
+ return consoleWarningsSystem.expectMessage(message, fn);
47
+ }
48
+ /**
49
+ * Expect the callback `fn` to trigger all console warnings (in sequence),
50
+ * and otherwise fail.
51
+ */
52
+
53
+
54
+ function expectConsoleWarningsMany(messages, fn) {
55
+ return consoleWarningsSystem.expectMessageMany(messages, fn);
56
+ }
57
+
58
+ module.exports = {
59
+ disallowConsoleWarnings: disallowConsoleWarnings,
60
+ expectConsoleWarningWillFire: expectConsoleWarningWillFire,
61
+ expectConsoleWarning: expectConsoleWarning,
62
+ expectConsoleWarningsMany: expectConsoleWarningsMany
63
+ };
@@ -12,10 +12,9 @@
12
12
  * Run a test suite under multiple sets of feature flags.
13
13
  * Beware that calling jest.resetModules() within the suite may break this.
14
14
  */
15
- // flowlint ambiguous-object-type:error
16
15
  'use strict';
17
16
 
18
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
17
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
19
18
 
20
19
  var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
21
20
 
@@ -8,7 +8,7 @@
8
8
  */
9
9
  'use strict';
10
10
 
11
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
11
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"];
12
12
 
13
13
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
14
14
 
@@ -7,7 +7,6 @@
7
7
  *
8
8
  * @format
9
9
  */
10
- // flowlint ambiguous-object-type:error
11
10
  'use strict';
12
11
 
13
12
  function getOutputForFixture(input, operation, file) {