@fgv/ts-utils-jest 5.0.0-30 → 5.0.0-31
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/ts-utils-jest.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @public
|
|
3
5
|
*/
|
|
@@ -31,7 +33,7 @@ declare class MockFileSystem {
|
|
|
31
33
|
getExtraFilesWritten(): string[];
|
|
32
34
|
tryGetPayload(want: string): string | undefined;
|
|
33
35
|
reset(): void;
|
|
34
|
-
startSpies(): ReadWriteSpies;
|
|
36
|
+
startSpies(fsModule?: typeof fs): ReadWriteSpies;
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
declare namespace MockFs {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
1
2
|
/**
|
|
2
3
|
* @public
|
|
3
4
|
*/
|
|
@@ -46,7 +47,7 @@ export declare class MockFileSystem {
|
|
|
46
47
|
getExtraFilesWritten(): string[];
|
|
47
48
|
tryGetPayload(want: string): string | undefined;
|
|
48
49
|
reset(): void;
|
|
49
|
-
startSpies(): ReadWriteSpies;
|
|
50
|
+
startSpies(fsModule?: typeof fs): ReadWriteSpies;
|
|
50
51
|
}
|
|
51
52
|
export {};
|
|
52
53
|
//# sourceMappingURL=fsHelpers.d.ts.map
|
package/lib/helpers/fsHelpers.js
CHANGED
|
@@ -20,46 +20,13 @@
|
|
|
20
20
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
* SOFTWARE.
|
|
22
22
|
*/
|
|
23
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
24
|
-
if (k2 === undefined) k2 = k;
|
|
25
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
26
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
27
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
28
|
-
}
|
|
29
|
-
Object.defineProperty(o, k2, desc);
|
|
30
|
-
}) : (function(o, m, k, k2) {
|
|
31
|
-
if (k2 === undefined) k2 = k;
|
|
32
|
-
o[k2] = m[k];
|
|
33
|
-
}));
|
|
34
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
35
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
36
|
-
}) : function(o, v) {
|
|
37
|
-
o["default"] = v;
|
|
38
|
-
});
|
|
39
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
40
|
-
var ownKeys = function(o) {
|
|
41
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
42
|
-
var ar = [];
|
|
43
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
44
|
-
return ar;
|
|
45
|
-
};
|
|
46
|
-
return ownKeys(o);
|
|
47
|
-
};
|
|
48
|
-
return function (mod) {
|
|
49
|
-
if (mod && mod.__esModule) return mod;
|
|
50
|
-
var result = {};
|
|
51
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
52
|
-
__setModuleDefault(result, mod);
|
|
53
|
-
return result;
|
|
54
|
-
};
|
|
55
|
-
})();
|
|
56
23
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
57
24
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
58
25
|
};
|
|
59
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
60
27
|
exports.MockFileSystem = exports.ReadWriteSpies = void 0;
|
|
61
28
|
const fs_1 = __importDefault(require("fs"));
|
|
62
|
-
const
|
|
29
|
+
const path_1 = __importDefault(require("path"));
|
|
63
30
|
/**
|
|
64
31
|
* @public
|
|
65
32
|
*/
|
|
@@ -89,7 +56,7 @@ class MockFileSystem {
|
|
|
89
56
|
this._options = options;
|
|
90
57
|
this._realReadFileSync = fs_1.default.readFileSync;
|
|
91
58
|
for (const config of configs) {
|
|
92
|
-
const fullPath =
|
|
59
|
+
const fullPath = path_1.default.resolve(config.path);
|
|
93
60
|
this._config.set(fullPath, config);
|
|
94
61
|
if (config.backingFile) {
|
|
95
62
|
this.readMockFileSync(fullPath);
|
|
@@ -101,7 +68,7 @@ class MockFileSystem {
|
|
|
101
68
|
}
|
|
102
69
|
readMockFileSync(wanted) {
|
|
103
70
|
var _a;
|
|
104
|
-
const fullPathWanted =
|
|
71
|
+
const fullPathWanted = path_1.default.resolve(wanted);
|
|
105
72
|
if (!this._data.has(fullPathWanted)) {
|
|
106
73
|
const config = this._config.get(fullPathWanted);
|
|
107
74
|
if ((config === null || config === void 0 ? void 0 : config.backingFile) === undefined) {
|
|
@@ -112,7 +79,7 @@ class MockFileSystem {
|
|
|
112
79
|
this._data.set(fullPathWanted, body);
|
|
113
80
|
}
|
|
114
81
|
else {
|
|
115
|
-
const fullBackingPath =
|
|
82
|
+
const fullBackingPath = path_1.default.resolve(config.backingFile);
|
|
116
83
|
const body = this._realReadFileSync(fullBackingPath).toString();
|
|
117
84
|
this._data.set(fullPathWanted, body);
|
|
118
85
|
}
|
|
@@ -127,7 +94,7 @@ class MockFileSystem {
|
|
|
127
94
|
}
|
|
128
95
|
writeMockFileSync(wanted, body) {
|
|
129
96
|
var _a;
|
|
130
|
-
const fullPathWanted =
|
|
97
|
+
const fullPathWanted = path_1.default.resolve(wanted);
|
|
131
98
|
const config = this._config.get(fullPathWanted);
|
|
132
99
|
if (config === undefined) {
|
|
133
100
|
if (((_a = this._options) === null || _a === void 0 ? void 0 : _a.allowUnknownMockWrite) !== true) {
|
|
@@ -144,17 +111,17 @@ class MockFileSystem {
|
|
|
144
111
|
return Array.from(this._extraWrites);
|
|
145
112
|
}
|
|
146
113
|
tryGetPayload(want) {
|
|
147
|
-
return this._data.get(
|
|
114
|
+
return this._data.get(path_1.default.resolve(want));
|
|
148
115
|
}
|
|
149
116
|
reset() {
|
|
150
117
|
const writable = Array.from(this._config.values()).filter((c) => c.writable === true);
|
|
151
118
|
for (const config of writable) {
|
|
152
|
-
this._data.delete(
|
|
119
|
+
this._data.delete(path_1.default.resolve(config.path));
|
|
153
120
|
if (config.backingFile) {
|
|
154
|
-
this.readMockFileSync(
|
|
121
|
+
this.readMockFileSync(path_1.default.resolve(config.path));
|
|
155
122
|
}
|
|
156
123
|
else if (config.payload) {
|
|
157
|
-
this._data.set(
|
|
124
|
+
this._data.set(path_1.default.resolve(config.path), config.payload);
|
|
158
125
|
}
|
|
159
126
|
}
|
|
160
127
|
for (const path of this._extraWrites) {
|
|
@@ -162,13 +129,15 @@ class MockFileSystem {
|
|
|
162
129
|
}
|
|
163
130
|
this._extraWrites.splice(0, this._extraWrites.length);
|
|
164
131
|
}
|
|
165
|
-
startSpies() {
|
|
166
|
-
|
|
132
|
+
startSpies(fsModule) {
|
|
133
|
+
// Dynamic import of fs at runtime to avoid babel instrumentation timing issues
|
|
134
|
+
const fsToMock = fsModule || require('fs');
|
|
135
|
+
return new ReadWriteSpies(jest.spyOn(fsToMock, 'readFileSync').mockImplementation((wanted) => {
|
|
167
136
|
if (typeof wanted !== 'string') {
|
|
168
137
|
throw new Error('readFileSync mock supports only string parameters');
|
|
169
138
|
}
|
|
170
139
|
return this.readMockFileSync(wanted);
|
|
171
|
-
}), jest.spyOn(
|
|
140
|
+
}), jest.spyOn(fsToMock, 'writeFileSync').mockImplementation((wanted, payload) => {
|
|
172
141
|
if (typeof wanted !== 'string' || typeof payload !== 'string') {
|
|
173
142
|
throw new Error('writeFileSync mock supports only string parameters');
|
|
174
143
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-utils-jest",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-31",
|
|
4
4
|
"description": "Custom matchers for ts-utils result class",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -42,10 +42,10 @@
|
|
|
42
42
|
"@types/heft-jest": "1.0.6",
|
|
43
43
|
"eslint-plugin-n": "^17.21.3",
|
|
44
44
|
"eslint-plugin-tsdoc": "~0.4.0",
|
|
45
|
-
"@fgv/ts-utils": "5.0.0-
|
|
45
|
+
"@fgv/ts-utils": "5.0.0-31"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@fgv/ts-utils": "5.0.0-
|
|
48
|
+
"@fgv/ts-utils": "5.0.0-31"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "heft build --clean",
|