@fgv/ts-utils 5.0.1-9 → 5.0.1
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/dist/index.js +32 -0
- package/dist/packlets/base/brand.js +23 -0
- package/dist/packlets/base/index.js +28 -0
- package/dist/packlets/base/mapResults.js +217 -0
- package/dist/packlets/base/messageAggregator.js +110 -0
- package/dist/packlets/base/normalize.js +132 -0
- package/dist/packlets/base/result.js +611 -0
- package/dist/packlets/base/utils.js +185 -0
- package/dist/packlets/collections/collectible.js +82 -0
- package/dist/packlets/collections/collector.js +189 -0
- package/dist/packlets/collections/collectorValidator.js +94 -0
- package/dist/packlets/collections/common.js +23 -0
- package/dist/packlets/collections/convertingCollector.js +118 -0
- package/dist/packlets/collections/convertingCollectorValidator.js +95 -0
- package/dist/packlets/collections/index.js +37 -0
- package/dist/packlets/collections/keyValueConverters.js +100 -0
- package/dist/packlets/collections/readonlyResultMap.js +23 -0
- package/dist/packlets/collections/resultMap.js +214 -0
- package/dist/packlets/collections/resultMapValidator.js +122 -0
- package/dist/packlets/collections/utils.js +31 -0
- package/dist/packlets/collections/validatingCollector.js +63 -0
- package/dist/packlets/collections/validatingConvertingCollector.js +64 -0
- package/dist/packlets/collections/validatingResultMap.js +57 -0
- package/dist/packlets/conversion/baseConverter.js +242 -0
- package/dist/packlets/conversion/converter.js +2 -0
- package/dist/packlets/conversion/converters.js +637 -0
- package/dist/packlets/conversion/defaultingConverter.js +149 -0
- package/dist/packlets/conversion/index.js +29 -0
- package/dist/packlets/conversion/objectConverter.js +141 -0
- package/dist/packlets/conversion/stringConverter.js +93 -0
- package/dist/packlets/hash/crcNormalizer.js +114 -0
- package/dist/packlets/hash/hashingNormalizer.js +92 -0
- package/dist/packlets/hash/index.js +24 -0
- package/dist/packlets/logging/index.js +24 -0
- package/dist/packlets/logging/logReporter.js +114 -0
- package/dist/packlets/logging/logger.js +258 -0
- package/dist/packlets/validation/array.js +64 -0
- package/dist/packlets/validation/boolean.js +52 -0
- package/dist/packlets/validation/classes.js +31 -0
- package/dist/packlets/validation/common.js +23 -0
- package/dist/packlets/validation/field.js +67 -0
- package/dist/packlets/validation/genericValidator.js +160 -0
- package/dist/packlets/validation/index.js +29 -0
- package/dist/packlets/validation/number.js +52 -0
- package/dist/packlets/validation/object.js +134 -0
- package/dist/packlets/validation/oneOf.js +58 -0
- package/dist/packlets/validation/string.js +52 -0
- package/dist/packlets/validation/traits.js +56 -0
- package/dist/packlets/validation/typeGuard.js +59 -0
- package/dist/packlets/validation/validator.js +23 -0
- package/dist/packlets/validation/validatorBase.js +37 -0
- package/dist/packlets/validation/validators.js +191 -0
- package/dist/test/helpers/jest/helpers/fsHelpers.js +139 -0
- package/dist/test/helpers/jest/helpers/index.js +2 -0
- package/dist/test/helpers/jest/index.js +17 -0
- package/dist/test/helpers/jest/matchers/index.js +14 -0
- package/dist/test/helpers/jest/matchers/toFail/index.js +23 -0
- package/dist/test/helpers/jest/matchers/toFail/predicate.js +5 -0
- package/dist/test/helpers/jest/matchers/toFailTest/index.js +28 -0
- package/dist/test/helpers/jest/matchers/toFailTest/predicate.js +8 -0
- package/dist/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/index.js +24 -0
- package/dist/test/helpers/jest/matchers/toFailTestAndMatchSnapshot/predicate.js +11 -0
- package/dist/test/helpers/jest/matchers/toFailTestWith/index.js +29 -0
- package/dist/test/helpers/jest/matchers/toFailTestWith/predicate.js +26 -0
- package/dist/test/helpers/jest/matchers/toFailWith/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toFailWith/predicate.js +16 -0
- package/dist/test/helpers/jest/matchers/toFailWithDetail/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toFailWithDetail/predicate.js +22 -0
- package/dist/test/helpers/jest/matchers/toSucceed/index.js +23 -0
- package/dist/test/helpers/jest/matchers/toSucceed/predicate.js +5 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndMatchInlineSnapshot/index.js +24 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndMatchSnapshot/index.js +24 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndSatisfy/index.js +53 -0
- package/dist/test/helpers/jest/matchers/toSucceedAndSatisfy/predicate.js +17 -0
- package/dist/test/helpers/jest/matchers/toSucceedWith/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toSucceedWith/predicate.js +12 -0
- package/dist/test/helpers/jest/matchers/toSucceedWithDetail/index.js +27 -0
- package/dist/test/helpers/jest/matchers/toSucceedWithDetail/predicate.js +17 -0
- package/dist/test/helpers/jest/resolvers/cli.js +10 -0
- package/dist/test/helpers/jest/resolvers/ide.js +10 -0
- package/dist/test/helpers/jest/ts-utils.js +2 -0
- package/dist/test/helpers/jest/types/index.js +3 -0
- package/dist/test/helpers/jest/utils/matcherHelpers.js +47 -0
- package/dist/test/helpers/jest/utils/snapshotResolver.js +11 -0
- package/dist/test/unit/collections/helpers.js +106 -0
- package/dist/test/unit/hashTextEncodeCompat.js +23 -0
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/packlets/hash/crcNormalizer.js +42 -2
- package/package.json +12 -2
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
import { fail, isKeyOf, MessageAggregator } from '../base';
|
|
23
|
+
import { ArrayValidator } from './array';
|
|
24
|
+
import { ObjectValidator } from './object';
|
|
25
|
+
import { TypeGuardValidator } from './typeGuard';
|
|
26
|
+
import { BooleanValidator } from './boolean';
|
|
27
|
+
import { GenericValidator } from './genericValidator';
|
|
28
|
+
import { NumberValidator } from './number';
|
|
29
|
+
import { OneOfValidator } from './oneOf';
|
|
30
|
+
import { StringValidator } from './string';
|
|
31
|
+
/**
|
|
32
|
+
* A {@link Validation.Classes.StringValidator | StringValidator} which validates a string in place.
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
export const string = new StringValidator();
|
|
36
|
+
/**
|
|
37
|
+
* A {@link Validation.Classes.NumberValidator | NumberValidator} which validates a number in place.
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export const number = new NumberValidator();
|
|
41
|
+
/**
|
|
42
|
+
* A {@link Validation.Classes.BooleanValidator | BooleanValidator} which validates a boolean in place.
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export const boolean = new BooleanValidator();
|
|
46
|
+
/**
|
|
47
|
+
* Helper function to create a {@link Validation.Classes.ObjectValidator | ObjectValidator} which validates
|
|
48
|
+
* an object in place.
|
|
49
|
+
* @param fields - A {@link Validation.Classes.FieldValidators | field validator definition}
|
|
50
|
+
* describing the validations to be applied.
|
|
51
|
+
* @param params - Optional {@link Validation.Classes.ObjectValidatorConstructorParams | parameters}
|
|
52
|
+
* to refine the behavior of the resulting {@link Validation.Validator | validator}.
|
|
53
|
+
* @returns A new {@link Validation.Validator | Validator} which validates the desired
|
|
54
|
+
* object in place.
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
57
|
+
export function object(fields, params) {
|
|
58
|
+
return new ObjectValidator(Object.assign({ fields }, (params !== null && params !== void 0 ? params : {})));
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Helper function to create a {@link Validation.Classes.ArrayValidator | ArrayValidator} which
|
|
62
|
+
* validates an array in place.
|
|
63
|
+
* @param validateElement - A {@link Validation.Validator | validator} which validates each element.
|
|
64
|
+
* @returns A new {@link Validation.Classes.ArrayValidator | ArrayValidator } which validates the desired
|
|
65
|
+
* array in place.
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
export function arrayOf(validateElement, params) {
|
|
69
|
+
return new ArrayValidator(Object.assign({ validateElement }, (params !== null && params !== void 0 ? params : {})));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* A helper function to create a {@link Validation.Validator | Validator} which validates the `string`-keyed properties
|
|
73
|
+
* using a supplied {@link Validation.Validator | Validator<T, TC>} to produce a `Record<TK, T>`.
|
|
74
|
+
* @remarks
|
|
75
|
+
* If present, the supplied {@link Validators.IRecordOfValidatorOptions | options} can provide a strongly-typed
|
|
76
|
+
* validator for keys and/or control the handling of elements that fail validation.
|
|
77
|
+
* @param validator - {@link Validation.Validator | Validator} used for each item in the source object.
|
|
78
|
+
* @param options - Optional {@link Validators.IRecordOfValidatorOptions | IRecordOfValidatorOptions<TK, TC>} which
|
|
79
|
+
* supplies a key validator and/or error-handling options.
|
|
80
|
+
* @returns A {@link Validation.Validator | Validator} which validates `Record<TK, T>`.
|
|
81
|
+
* @public
|
|
82
|
+
*/
|
|
83
|
+
export function recordOf(validator, options) {
|
|
84
|
+
const opts = Object.assign({ onError: 'fail' }, options);
|
|
85
|
+
return new GenericValidator({
|
|
86
|
+
validator: (from, context, self) => {
|
|
87
|
+
if (typeof from !== 'object' || from === null || Array.isArray(from)) {
|
|
88
|
+
return fail(`Not a string-keyed object: ${JSON.stringify(from)}`);
|
|
89
|
+
}
|
|
90
|
+
const errors = new MessageAggregator();
|
|
91
|
+
for (const key in from) {
|
|
92
|
+
if (isKeyOf(key, from)) {
|
|
93
|
+
// Validate key if keyValidator is provided
|
|
94
|
+
if (opts.keyValidator) {
|
|
95
|
+
const keyResult = opts.keyValidator.validate(key, context);
|
|
96
|
+
if (!keyResult.isSuccess()) {
|
|
97
|
+
if (opts.onError === 'ignore') {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
errors.addMessage(`Key "${key}": ${keyResult.message}`);
|
|
101
|
+
if (opts.onError === 'fail') {
|
|
102
|
+
return fail(keyResult.message);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Validate value
|
|
107
|
+
const valueResult = validator.validate(from[key], context);
|
|
108
|
+
if (!valueResult.isSuccess()) {
|
|
109
|
+
if (opts.onError === 'ignore') {
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
errors.addMessage(`Property "${key}": ${valueResult.message}`);
|
|
113
|
+
if (opts.onError === 'fail') {
|
|
114
|
+
return fail(valueResult.message);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/* c8 ignore next 1 - defense in depth */
|
|
120
|
+
return errors.hasMessages ? fail(errors.toString()) : true;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Helper function to create a {@link Validation.Validator} which validates an enumerated
|
|
126
|
+
* value in place.
|
|
127
|
+
* @public
|
|
128
|
+
*/
|
|
129
|
+
export function enumeratedValue(values) {
|
|
130
|
+
return new GenericValidator({
|
|
131
|
+
validator: (from, context, self) => {
|
|
132
|
+
if (typeof from === 'string') {
|
|
133
|
+
const v = context !== null && context !== void 0 ? context : values;
|
|
134
|
+
const index = v.indexOf(from);
|
|
135
|
+
return index >= 0 ? true : fail(`Invalid enumerated value "${from}" - expected: (${v.join(', ')})`);
|
|
136
|
+
}
|
|
137
|
+
return fail(`Not a string: "${JSON.stringify(from, undefined, 2)}`);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Helper function to create a {@link Validation.Validator} which validates a literal value.
|
|
143
|
+
* @param value - the literal value to be validated
|
|
144
|
+
* @public
|
|
145
|
+
*/
|
|
146
|
+
export function literal(value) {
|
|
147
|
+
return new GenericValidator({
|
|
148
|
+
validator: (from, context, self) => {
|
|
149
|
+
return from === value
|
|
150
|
+
? true
|
|
151
|
+
: fail(`Expected literal ${String(value)}, found "${JSON.stringify(from, undefined, 2)}`);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Helper function to create a {@link Validation.Validator | Validator} which validates one
|
|
157
|
+
* of several possible validated values.
|
|
158
|
+
* @param validators - the {@link Validation.Validator | validators} to be considered.
|
|
159
|
+
* @param params - Optional {@link Validation.Classes.OneOfValidatorConstructorParams | params} used to construct the validator.
|
|
160
|
+
* @returns A new {@link Validator | Validator} which validates values that match any of
|
|
161
|
+
* the supplied validators.
|
|
162
|
+
* @public
|
|
163
|
+
*/
|
|
164
|
+
export function oneOf(validators, params) {
|
|
165
|
+
return new OneOfValidator(Object.assign(Object.assign({}, (params !== null && params !== void 0 ? params : {})), { validators }));
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Helper function to create a {@link Validation.Classes.TypeGuardValidator | TypeGuardValidator} which
|
|
169
|
+
* validates a value or object in place.
|
|
170
|
+
* @param description - a description of the thing to be validated for use in error messages
|
|
171
|
+
* @param guard - a {@link Validation.TypeGuardWithContext} which performs the validation.
|
|
172
|
+
* @returns A new {@link Validation.Classes.TypeGuardValidator | TypeGuardValidator } which validates
|
|
173
|
+
* the values using the supplied type guard.
|
|
174
|
+
* @public
|
|
175
|
+
*/
|
|
176
|
+
export function isA(description, guard, params) {
|
|
177
|
+
return new TypeGuardValidator(Object.assign({ description, guard }, (params !== null && params !== void 0 ? params : {})));
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Helper function to create a {@link Validation.Validator | Validator} using a
|
|
181
|
+
* supplied {@link Validation.ValidatorFunc | validator function}.
|
|
182
|
+
* @param validator - A {@link Validation.ValidatorFunc | validator function} that a
|
|
183
|
+
* supplied unknown value matches some condition.
|
|
184
|
+
* @returns A new {@link Validation.Validator | Validator} which validates the desired
|
|
185
|
+
* value using the supplied function.
|
|
186
|
+
* @public
|
|
187
|
+
*/
|
|
188
|
+
export function generic(validator) {
|
|
189
|
+
return new GenericValidator({ validator });
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=validators.js.map
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
import fs from 'fs';
|
|
23
|
+
import * as path from 'path';
|
|
24
|
+
/**
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export class ReadWriteSpies {
|
|
28
|
+
constructor(read, write) {
|
|
29
|
+
this.read = read;
|
|
30
|
+
this.write = write;
|
|
31
|
+
}
|
|
32
|
+
clear() {
|
|
33
|
+
this.read.mockClear();
|
|
34
|
+
this.write.mockClear();
|
|
35
|
+
}
|
|
36
|
+
restore() {
|
|
37
|
+
this.read.mockRestore();
|
|
38
|
+
this.write.mockRestore();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export class MockFileSystem {
|
|
45
|
+
constructor(configs, options) {
|
|
46
|
+
this._extraWrites = [];
|
|
47
|
+
this._config = new Map();
|
|
48
|
+
this._data = new Map();
|
|
49
|
+
this._options = options;
|
|
50
|
+
this._realReadFileSync = fs.readFileSync;
|
|
51
|
+
for (const config of configs) {
|
|
52
|
+
const fullPath = path.resolve(config.path);
|
|
53
|
+
this._config.set(fullPath, config);
|
|
54
|
+
if (config.backingFile) {
|
|
55
|
+
this.readMockFileSync(fullPath);
|
|
56
|
+
}
|
|
57
|
+
else if (config.payload) {
|
|
58
|
+
this._data.set(fullPath, config.payload);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
readMockFileSync(wanted) {
|
|
63
|
+
var _a;
|
|
64
|
+
const fullPathWanted = path.resolve(wanted);
|
|
65
|
+
if (!this._data.has(fullPathWanted)) {
|
|
66
|
+
const config = this._config.get(fullPathWanted);
|
|
67
|
+
if ((config === null || config === void 0 ? void 0 : config.backingFile) === undefined) {
|
|
68
|
+
if (((_a = this._options) === null || _a === void 0 ? void 0 : _a.mockWriteOnly) !== true) {
|
|
69
|
+
throw new Error(`Mock file not found: ${wanted}`);
|
|
70
|
+
}
|
|
71
|
+
const body = this._realReadFileSync(fullPathWanted).toString();
|
|
72
|
+
this._data.set(fullPathWanted, body);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const fullBackingPath = path.resolve(config.backingFile);
|
|
76
|
+
const body = this._realReadFileSync(fullBackingPath).toString();
|
|
77
|
+
this._data.set(fullPathWanted, body);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const payload = this._data.get(fullPathWanted);
|
|
81
|
+
// not actually reproducible right now
|
|
82
|
+
/* c8 ignore next 3 */
|
|
83
|
+
if (payload === undefined) {
|
|
84
|
+
throw new Error(`Mock file ${wanted} payload is undefined.`);
|
|
85
|
+
}
|
|
86
|
+
return payload;
|
|
87
|
+
}
|
|
88
|
+
writeMockFileSync(wanted, body) {
|
|
89
|
+
var _a;
|
|
90
|
+
const fullPathWanted = path.resolve(wanted);
|
|
91
|
+
const config = this._config.get(fullPathWanted);
|
|
92
|
+
if (config === undefined) {
|
|
93
|
+
if (((_a = this._options) === null || _a === void 0 ? void 0 : _a.allowUnknownMockWrite) !== true) {
|
|
94
|
+
throw new Error(`Mock path not found: ${wanted}`);
|
|
95
|
+
}
|
|
96
|
+
this._extraWrites.push(fullPathWanted);
|
|
97
|
+
}
|
|
98
|
+
else if (config.writable !== true) {
|
|
99
|
+
throw new Error(`Mock permission denied: ${wanted}`);
|
|
100
|
+
}
|
|
101
|
+
this._data.set(fullPathWanted, body);
|
|
102
|
+
}
|
|
103
|
+
getExtraFilesWritten() {
|
|
104
|
+
return Array.from(this._extraWrites);
|
|
105
|
+
}
|
|
106
|
+
tryGetPayload(want) {
|
|
107
|
+
return this._data.get(path.resolve(want));
|
|
108
|
+
}
|
|
109
|
+
reset() {
|
|
110
|
+
const writable = Array.from(this._config.values()).filter((c) => c.writable === true);
|
|
111
|
+
for (const config of writable) {
|
|
112
|
+
this._data.delete(path.resolve(config.path));
|
|
113
|
+
if (config.backingFile) {
|
|
114
|
+
this.readMockFileSync(path.resolve(config.path));
|
|
115
|
+
}
|
|
116
|
+
else if (config.payload) {
|
|
117
|
+
this._data.set(path.resolve(config.path), config.payload);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
for (const path of this._extraWrites) {
|
|
121
|
+
this._data.delete(path);
|
|
122
|
+
}
|
|
123
|
+
this._extraWrites.splice(0, this._extraWrites.length);
|
|
124
|
+
}
|
|
125
|
+
startSpies() {
|
|
126
|
+
return new ReadWriteSpies(jest.spyOn(fs, 'readFileSync').mockImplementation((wanted) => {
|
|
127
|
+
if (typeof wanted !== 'string') {
|
|
128
|
+
throw new Error('readFileSync mock supports only string parameters');
|
|
129
|
+
}
|
|
130
|
+
return this.readMockFileSync(wanted);
|
|
131
|
+
}), jest.spyOn(fs, 'writeFileSync').mockImplementation((wanted, payload) => {
|
|
132
|
+
if (typeof wanted !== 'string' || typeof payload !== 'string') {
|
|
133
|
+
throw new Error('writeFileSync mock supports only string parameters');
|
|
134
|
+
}
|
|
135
|
+
return this.writeMockFileSync(wanted, payload);
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=fsHelpers.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import matchers from './matchers';
|
|
2
|
+
import './types';
|
|
3
|
+
export { MockFileSystem } from './helpers';
|
|
4
|
+
function isJestGlobal(g) {
|
|
5
|
+
return g.hasOwnProperty('expect');
|
|
6
|
+
}
|
|
7
|
+
/* c8 ignore else */
|
|
8
|
+
if (isJestGlobal(global)) {
|
|
9
|
+
global.expect.extend(matchers);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
console.error([
|
|
13
|
+
"Unable to find Jest's global expect",
|
|
14
|
+
'Please check that you have added ts-utils-jest correctly to your jest configuration.'
|
|
15
|
+
].join('\n'));
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import ToFail from './toFail';
|
|
2
|
+
import ToFailTest from './toFailTest';
|
|
3
|
+
import ToFailTestAndMatchSnapshot from './toFailTestAndMatchSnapshot';
|
|
4
|
+
import ToFailTestWith from './toFailTestWith';
|
|
5
|
+
import ToFailWith from './toFailWith';
|
|
6
|
+
import ToFailWithDetail from './toFailWithDetail';
|
|
7
|
+
import ToSucceed from './toSucceed';
|
|
8
|
+
import ToSucceedAndMatchInlineSnapshot from './toSucceedAndMatchInlineSnapshot';
|
|
9
|
+
import ToSucceedAndMatchSnapshot from './toSucceedAndMatchSnapshot';
|
|
10
|
+
import ToSucceedAndSatisfy from './toSucceedAndSatisfy';
|
|
11
|
+
import ToSucceedWith from './toSucceedWith';
|
|
12
|
+
import toSucceedWithDetail from './toSucceedWithDetail';
|
|
13
|
+
export default Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, ToFail), ToFailTest), ToFailTestAndMatchSnapshot), ToFailTestWith), ToFailWith), ToFailWithDetail), ToSucceed), ToSucceedAndMatchInlineSnapshot), ToSucceedAndMatchSnapshot), ToSucceedAndSatisfy), ToSucceedWith), toSucceedWithDetail);
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
function passMessage(received) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`),
|
|
7
|
+
printExpectedResult('failure', false),
|
|
8
|
+
printReceivedResult(received)
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(received) {
|
|
12
|
+
return () => [matcherHint(`${matcherName}`), printExpectedResult('failure', true), printReceivedResult(received)].join('\n');
|
|
13
|
+
}
|
|
14
|
+
export default {
|
|
15
|
+
toFail: function (received) {
|
|
16
|
+
const pass = predicate(received);
|
|
17
|
+
if (pass) {
|
|
18
|
+
return { pass: true, message: passMessage(received) };
|
|
19
|
+
}
|
|
20
|
+
return { pass: false, message: failMessage(received) };
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
import { printExpectedResult } from '../../utils/matcherHelpers';
|
|
5
|
+
function passMessage() {
|
|
6
|
+
return () => [
|
|
7
|
+
matcherHint(`.not.${matcherName}`, 'callback'),
|
|
8
|
+
printExpectedResult('failure', false),
|
|
9
|
+
' Received: Test failed'
|
|
10
|
+
].join('\n');
|
|
11
|
+
}
|
|
12
|
+
function failMessage() {
|
|
13
|
+
return () => [
|
|
14
|
+
matcherHint(`${matcherName}`, 'callback'),
|
|
15
|
+
printExpectedResult('failure', true),
|
|
16
|
+
' Received: Test passed'
|
|
17
|
+
].join('\n');
|
|
18
|
+
}
|
|
19
|
+
export default {
|
|
20
|
+
toFailTest: function (cb) {
|
|
21
|
+
const pass = predicate(cb);
|
|
22
|
+
if (pass) {
|
|
23
|
+
return { pass: true, message: passMessage() };
|
|
24
|
+
}
|
|
25
|
+
return { pass: false, message: failMessage() };
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { captureResult } from '../../ts-utils';
|
|
3
|
+
export const matcherName = 'toFailTest';
|
|
4
|
+
export function predicate(cb) {
|
|
5
|
+
const cbResult = captureResult(() => cb());
|
|
6
|
+
return cbResult.isFailure();
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
import { toMatchSnapshot } from 'jest-snapshot';
|
|
5
|
+
export default {
|
|
6
|
+
toFailTestAndMatchSnapshot: function (cb) {
|
|
7
|
+
const context = this;
|
|
8
|
+
const cbResult = predicate(cb);
|
|
9
|
+
if (cbResult.isFailure()) {
|
|
10
|
+
return {
|
|
11
|
+
pass: false,
|
|
12
|
+
message: () => {
|
|
13
|
+
return [
|
|
14
|
+
matcherHint(`${matcherName}`, 'callback'),
|
|
15
|
+
' Expected: Callback to fail with an error that matches snapshot',
|
|
16
|
+
' Received: Callback succeeded'
|
|
17
|
+
].join('\n');
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return toMatchSnapshot.call(context, cbResult.value, 'toFailTestAndMatchSnapshot');
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { captureResult, fail, succeed } from '../../ts-utils';
|
|
3
|
+
export const matcherName = 'toFailTestAndMatchSnapshot';
|
|
4
|
+
export function predicate(cb) {
|
|
5
|
+
const cbResult = captureResult(() => cb());
|
|
6
|
+
if (cbResult.isFailure()) {
|
|
7
|
+
return succeed(cbResult.message);
|
|
8
|
+
}
|
|
9
|
+
return fail('Callback did not fail');
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { matcherName, predicate } from './predicate';
|
|
2
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
3
|
+
import { printExpectedResult } from '../../utils/matcherHelpers';
|
|
4
|
+
function passMessage(cbResult, expected) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`, 'callback', 'expectedMessage'),
|
|
7
|
+
printExpectedResult('failure', false, expected),
|
|
8
|
+
` Received: Callback failed with:\n>>>>\n${cbResult.value}\n<<<<`
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(cbResult, expected) {
|
|
12
|
+
return () => [
|
|
13
|
+
matcherHint(`${matcherName}`, 'callback', 'expectedMessage'),
|
|
14
|
+
printExpectedResult('failure', true, expected),
|
|
15
|
+
cbResult.message === ''
|
|
16
|
+
? ' Received: Callback succeeded'
|
|
17
|
+
: ` Received: Callback failed with:\n>>>>\n${cbResult.message}\n<<<<`
|
|
18
|
+
].join('\n');
|
|
19
|
+
}
|
|
20
|
+
export default {
|
|
21
|
+
toFailTestWith: function (cb, expected) {
|
|
22
|
+
const cbResult = predicate(cb, expected);
|
|
23
|
+
if (cbResult.isSuccess()) {
|
|
24
|
+
return { pass: true, message: passMessage(cbResult, expected) };
|
|
25
|
+
}
|
|
26
|
+
return { pass: false, message: failMessage(cbResult, expected) };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { equals } from '@jest/expect-utils';
|
|
3
|
+
import { stringify } from 'jest-matcher-utils';
|
|
4
|
+
import { captureResult, fail, succeed } from '../../ts-utils';
|
|
5
|
+
export const matcherName = 'toFailTestWith';
|
|
6
|
+
export function predicate(cb, expected) {
|
|
7
|
+
const cbResult = captureResult(() => cb());
|
|
8
|
+
if (cbResult.isFailure()) {
|
|
9
|
+
let success = false;
|
|
10
|
+
if (expected instanceof RegExp) {
|
|
11
|
+
success = cbResult.message.match(expected) !== null;
|
|
12
|
+
}
|
|
13
|
+
else if (typeof expected === 'string') {
|
|
14
|
+
success = cbResult.message === expected;
|
|
15
|
+
}
|
|
16
|
+
else if (Array.isArray(expected)) {
|
|
17
|
+
success = equals(cbResult.message.split('\n'), expected);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
return fail(`Unsupported expected value "${stringify(expected)}" for toFailTestWith`);
|
|
21
|
+
}
|
|
22
|
+
return success ? succeed(cbResult.message) : fail(cbResult.message);
|
|
23
|
+
}
|
|
24
|
+
return fail('');
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
function passMessage(received, expected) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`),
|
|
7
|
+
printExpectedResult('failure', false, expected),
|
|
8
|
+
printReceivedResult(received)
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(received, expected) {
|
|
12
|
+
return () => [
|
|
13
|
+
matcherHint(`${matcherName}`),
|
|
14
|
+
printExpectedResult('failure', true, expected),
|
|
15
|
+
printReceivedResult(received)
|
|
16
|
+
].join('\n');
|
|
17
|
+
}
|
|
18
|
+
export default {
|
|
19
|
+
toFailWith: function (received, expected) {
|
|
20
|
+
const pass = predicate(received, expected);
|
|
21
|
+
if (pass) {
|
|
22
|
+
return { pass: true, message: passMessage(received, expected) };
|
|
23
|
+
}
|
|
24
|
+
return { pass: false, message: failMessage(received, expected) };
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const matcherName = 'toFailWith';
|
|
2
|
+
export function predicate(received, expected) {
|
|
3
|
+
if (received.isFailure()) {
|
|
4
|
+
if (expected === undefined) {
|
|
5
|
+
return received.message === undefined;
|
|
6
|
+
}
|
|
7
|
+
else if (expected instanceof RegExp) {
|
|
8
|
+
return received.message.match(expected) !== null;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
return received.message === expected;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { printExpectedDetailedResult, printReceivedDetailedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
function passMessage(received, expectedMessage, expectedDetail) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`),
|
|
7
|
+
printExpectedDetailedResult('failure', false, expectedMessage, expectedDetail),
|
|
8
|
+
printReceivedDetailedResult(received)
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(received, expectedMessage, expectedDetail) {
|
|
12
|
+
return () => [
|
|
13
|
+
matcherHint(`${matcherName}`),
|
|
14
|
+
printExpectedDetailedResult('failure', true, expectedMessage, expectedDetail),
|
|
15
|
+
printReceivedDetailedResult(received)
|
|
16
|
+
].join('\n');
|
|
17
|
+
}
|
|
18
|
+
export default {
|
|
19
|
+
toFailWithDetail: function (received, expectedMessage, expectedDetail) {
|
|
20
|
+
const pass = predicate(received, expectedMessage, expectedDetail);
|
|
21
|
+
if (pass) {
|
|
22
|
+
return { pass: true, message: passMessage(received, expectedMessage, expectedDetail) };
|
|
23
|
+
}
|
|
24
|
+
return { pass: false, message: failMessage(received, expectedMessage, expectedDetail) };
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { equals } from '@jest/expect-utils';
|
|
2
|
+
export const matcherName = 'toFailWith';
|
|
3
|
+
export function predicate(received, expectedResult, expectedDetail) {
|
|
4
|
+
if (received.isFailure()) {
|
|
5
|
+
if (expectedResult === undefined) {
|
|
6
|
+
if (received.message !== undefined) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
else if (expectedResult instanceof RegExp) {
|
|
11
|
+
if (received.message.match(expectedResult) === null) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
else if (received.message !== expectedResult) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return equals(received.detail, expectedDetail);
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
function passMessage(received) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`),
|
|
7
|
+
printExpectedResult('success', false),
|
|
8
|
+
printReceivedResult(received)
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(received) {
|
|
12
|
+
return () => [matcherHint(`${matcherName}`), printExpectedResult('success', true), printReceivedResult(received)].join('\n');
|
|
13
|
+
}
|
|
14
|
+
export default {
|
|
15
|
+
toSucceed: function (received) {
|
|
16
|
+
const pass = predicate(received);
|
|
17
|
+
if (pass) {
|
|
18
|
+
return { pass: true, message: passMessage(received) };
|
|
19
|
+
}
|
|
20
|
+
return { pass: false, message: failMessage(received) };
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=index.js.map
|