forked from kuceb/cypress-plugin-snapshots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.js
71 lines (60 loc) · 1.59 KB
/
commands.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
70
71
/* globals Cypress, before, after, cy */
/* eslint-env browser */
const {
merge,
cloneDeep
} = require('lodash');
const { initUi } = require('./src/ui');
const commands = require('./src/commands/index');
const cleanUpSnapshots = require('./src/utils/commands/cleanupSnapshots');
const getConfig = require('./src/utils/commands/getConfig');
const { NO_LOG } = require('./src/constants');
function addCommand(commandName, method) {
Cypress.Commands.add(commandName, {
prevSubject: true
}, (commandSubject, taskOptions) => {
if (!commandSubject) {
return commandSubject;
}
const options = merge({}, cloneDeep(getConfig()), taskOptions);
return cy.wrap(commandSubject, NO_LOG)
.then((subject) => method(subject, options));
});
}
function initCommands() {
// Initialize config by getting it once
getConfig();
// Inject CSS & JavaScript
before(() => {
initUi();
});
function closeSnapshotModal() {
try {
if (window.top.closeSnapshotModal) {
window.top.closeSnapshotModal();
}
} catch(ex) {
window.console.error(ex);
}
}
function clearFileCache() {
Cypress.__readFileCache__ = {};
}
// Close snapshot modal and reset image files cache before all test restart
Cypress.on('window:before:unload', () => {
closeSnapshotModal()
clearFileCache()
});
// Clean up unused snapshots
after(() => {
cleanUpSnapshots();
});
// Add commands
Object.keys(commands).forEach(key => addCommand(key, commands[key]));
}
module.exports = {
initCommands,
};
if (!process.env.JEST_WORKER_ID) {
initCommands();
}