forked from Nanciee/cypress-autorecord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
69 lines (56 loc) · 1.76 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const path = require('path');
module.exports = (on, config, fs) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
const mocksFolder = path.resolve(config.fixturesFolder, '../mocks');
const readFile = (filePath) => {
if (fs.existsSync(filePath)) {
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
}
return null;
};
const deleteFile = (filePath) => {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
return true;
}
return null;
};
const cleanMocks = () => {
// TODO: create error handling
const specFiles = fs.readdirSync(config.integrationFolder);
const mockFiles = fs.readdirSync(mocksFolder);
mockFiles.forEach(mockName => {
const isMockUsed = specFiles.find(specName => specName.split('.')[0] === mockName.split('.')[0]);
if (!isMockUsed) {
const mockData = readFile(path.join(mocksFolder, mockName));
Object.keys(mockData).forEach(testName => {
mockData[testName].forEach((route) => {
if (route.fixtureId) {
deleteFile(path.join(config.fixturesFolder, `${route.fixtureId}.json`));
}
});
});
deleteFile(path.join(mocksFolder, mockName));
}
});
return null;
};
const removeAllMocks = () => {
const fixtureFiles = fs.readdirSync(config.fixturesFolder);
const mockFiles = fs.readdirSync(mocksFolder);
fixtureFiles.forEach(fileName => {
deleteFile(path.join(config.fixturesFolder, fileName));
});
mockFiles.forEach(fileName => {
deleteFile(path.join(mocksFolder, fileName));
});
return null;
};
on('task', {
readFile,
deleteFile,
cleanMocks,
removeAllMocks,
});
};