ftmocks-utils 1.0.6 → 1.0.7
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/package.json +1 -1
- package/src/index.js +20 -13
- package/src/recorder.js +6 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const nameToFolder = name => {
|
|
5
5
|
return name.replaceAll(' ', '_');
|
|
6
6
|
};
|
|
7
7
|
|
|
8
|
+
const getMockDir = config => {
|
|
9
|
+
if(!path.isAbsolute(config.MOCK_DIR)) {
|
|
10
|
+
return path.resolve( process.cwd(), config.MOCK_DIR);
|
|
11
|
+
}
|
|
12
|
+
return config.MOCK_DIR;
|
|
13
|
+
}
|
|
14
|
+
|
|
8
15
|
const areJsonEqual = (jsonObj1, jsonObj2) => {
|
|
9
16
|
// Check if both are objects and not null
|
|
10
17
|
if (typeof jsonObj1 === 'object' && jsonObj1 !== null &&
|
|
@@ -34,7 +41,7 @@ const areJsonEqual = (jsonObj1, jsonObj2) => {
|
|
|
34
41
|
}
|
|
35
42
|
|
|
36
43
|
const getDefaultMockDataFromConfig = (testConfig) => {
|
|
37
|
-
const defaultPath = path.join(testConfig
|
|
44
|
+
const defaultPath = path.join(getMockDir(testConfig), 'default.json');
|
|
38
45
|
|
|
39
46
|
try {
|
|
40
47
|
const defaultData = fs.readFileSync(defaultPath, 'utf8');
|
|
@@ -42,7 +49,7 @@ try {
|
|
|
42
49
|
|
|
43
50
|
// Read and attach mock data for each entry in parsedData
|
|
44
51
|
parsedData.forEach(entry => {
|
|
45
|
-
const mockFilePath = path.join(testConfig
|
|
52
|
+
const mockFilePath = path.join(getMockDir(testConfig), 'defaultMocks', `mock_${entry.id}.json`);;
|
|
46
53
|
try {
|
|
47
54
|
const mockData = fs.readFileSync(mockFilePath, 'utf8');
|
|
48
55
|
entry.fileContent = JSON.parse(mockData);
|
|
@@ -64,18 +71,18 @@ const loadMockDataFromConfig = (testConfig, _testName) => {
|
|
|
64
71
|
let testName = _testName;
|
|
65
72
|
if(!testName) {
|
|
66
73
|
// Read the test ID from mockServer.config.json
|
|
67
|
-
const configPath = path.join(testConfig
|
|
74
|
+
const configPath = path.join(getMockDir(testConfig), 'mockServer.config.json');
|
|
68
75
|
const configData = fs.readFileSync(configPath, 'utf8');
|
|
69
76
|
const config = JSON.parse(configData);
|
|
70
77
|
testName = config.testName;
|
|
71
78
|
}
|
|
72
79
|
// Read the tests from testConfig
|
|
73
|
-
const mocksPath = path.join(testConfig
|
|
80
|
+
const mocksPath = path.join(getMockDir(testConfig), `test_${nameToFolder(testName)}`, '_mock_list.json');
|
|
74
81
|
const mocksData = fs.readFileSync(mocksPath, 'utf8');
|
|
75
82
|
const mocks = JSON.parse(mocksData);
|
|
76
83
|
|
|
77
84
|
mocks.forEach(mock => {
|
|
78
|
-
const fileContent = JSON.parse(fs.readFileSync(path.join(testConfig
|
|
85
|
+
const fileContent = JSON.parse(fs.readFileSync(path.join(getMockDir(testConfig), `test_${nameToFolder(testName)}`, `mock_${mock.id}.json`), 'utf8'));
|
|
79
86
|
mock.fileContent = fileContent;
|
|
80
87
|
});
|
|
81
88
|
|
|
@@ -160,7 +167,7 @@ function getMatchingMockData({testMockData, defaultMockData, url, options, testC
|
|
|
160
167
|
let foundMock = matchedMocks.find(mock => !mock.fileContent.served) ? matchedMocks.find(mock => !mock.fileContent.served) : matchedMocks[matchedMocks.length - 1];
|
|
161
168
|
// updating stats to mock file
|
|
162
169
|
if(foundMock) {
|
|
163
|
-
const mockFilePath = path.join(testConfig
|
|
170
|
+
const mockFilePath = path.join(getMockDir(testConfig), `test_${nameToFolder(testName)}`, `mock_${foundMock.id}.json`);
|
|
164
171
|
foundMock.fileContent.served = true;
|
|
165
172
|
fs.writeFileSync(mockFilePath, JSON.stringify(foundMock.fileContent, null, 2));
|
|
166
173
|
}
|
|
@@ -177,7 +184,7 @@ function getMatchingMockData({testMockData, defaultMockData, url, options, testC
|
|
|
177
184
|
async function resetAllMockStats({testMockData, testConfig, testName}) {
|
|
178
185
|
for(let i=0; i<testMockData.length; i++) {
|
|
179
186
|
const tmd = testMockData[i];
|
|
180
|
-
const mockFilePath = path.join(testConfig
|
|
187
|
+
const mockFilePath = path.join(getMockDir(testConfig), `test_${nameToFolder(testName)}`, `mock_${tmd.id}.json`);
|
|
181
188
|
tmd.fileContent.served = false;
|
|
182
189
|
await fs.writeFileSync(mockFilePath, JSON.stringify(tmd.fileContent, null, 2));
|
|
183
190
|
}
|
|
@@ -301,8 +308,8 @@ function countFilesInDirectory(directoryPath) {
|
|
|
301
308
|
}
|
|
302
309
|
|
|
303
310
|
const saveSnap = async (html, ftmocksConifg, testName) => {
|
|
304
|
-
const snapFolder = path.join(ftmocksConifg
|
|
305
|
-
const snapTemplate = path.join(ftmocksConifg
|
|
311
|
+
const snapFolder = path.join(getMockDir(ftmocksConifg), `test_${nameToFolder(testName)}`, '_snaps');
|
|
312
|
+
const snapTemplate = path.join(getMockDir(ftmocksConifg), 'snap_template.html');
|
|
306
313
|
|
|
307
314
|
if (!fs.existsSync(snapFolder)) {
|
|
308
315
|
fs.mkdirSync(snapFolder);
|
|
@@ -318,7 +325,7 @@ const saveSnap = async (html, ftmocksConifg, testName) => {
|
|
|
318
325
|
};
|
|
319
326
|
|
|
320
327
|
const deleteAllSnaps = async (ftmocksConifg, testName) => {
|
|
321
|
-
const snapFolder = path.join(ftmocksConifg
|
|
328
|
+
const snapFolder = path.join(getMockDir(ftmocksConifg), `test_${nameToFolder(testName)}`, '_snaps');
|
|
322
329
|
fs.rmSync(snapFolder, { recursive: true, force: true });
|
|
323
330
|
};
|
|
324
331
|
|
package/src/recorder.js
CHANGED