Skip to content

Commit 65a0b9c

Browse files
authored
Merge pull request #8 from erezrokah/fix/run_from_plugin_dir
fix: run from plugin directory
2 parents 2933749 + 99a8c8b commit 65a0b9c

File tree

9 files changed

+4532
-955
lines changed

9 files changed

+4532
-955
lines changed

applitools.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
showLogs: true,
3+
};

cypress.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fs = require('fs-extra');
2+
const cypress = require('cypress');
3+
4+
const main = async () => {
5+
const runOptions = await cypress.cli.parseRunArguments(process.argv.slice(2));
6+
const results = await cypress.run(runOptions);
7+
await fs.writeJSON('results.json', results);
8+
};
9+
10+
main();
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"integrationFolder": "visual-diff/integration",
33
"pluginsFile": "visual-diff/plugins",
4-
"supportFile": "visual-diff/support"
4+
"supportFile": "visual-diff/support",
5+
"video": false
56
}

index.js

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,54 @@ const ecstatic = require('ecstatic');
44
const fs = require('fs-extra');
55
const glob = require('glob');
66

7-
const APPLITOOLS_CONFIG = 'applitools.config.js';
8-
const VISUAL_DIFF = 'visual-diff';
7+
const createEnvFile = async ({ inputs, builtPages }) => {
8+
await fs.writeJSON(`${__dirname}/cypress.env.json`, {
9+
SITE_NAME: process.env.SITE_NAME || 'localhost-test',
10+
APPLITOOLS_BROWSERS: JSON.stringify(inputs.browser),
11+
APPLITOOLS_FAIL_BUILD_ON_DIFF: inputs.failBuildOnDiff,
12+
APPLITOOLS_SERVER_URL: inputs.serverUrl,
13+
APPLITOOLS_IGNORE_SELECTOR: inputs.ignoreSelector
14+
? inputs.ignoreSelector
15+
.split(',')
16+
.map((selector) => ({ selector: selector.trim() }))
17+
: [],
18+
APPLITOOLS_CONCURRENCY: inputs.concurrency,
19+
PAGES_TO_CHECK: builtPages,
20+
CYPRESS_CACHE_FOLDER: './node_modules/CypressBinary',
21+
});
22+
};
23+
24+
const runCypress = async ({ utils, port }) => {
25+
await utils.run(
26+
'node',
27+
['cypress.js', 'run', '--config', `baseUrl=http://localhost:${port}`],
28+
{ cwd: __dirname },
29+
);
30+
31+
return await fs.readJSON(`${__dirname}/results.json`);
32+
};
33+
34+
const shutdownServer = async ({ server }) => {
35+
await new Promise((resolve, reject) => {
36+
server.close((err) => {
37+
if (err) {
38+
return reject(err);
39+
}
40+
41+
resolve();
42+
});
43+
});
44+
};
945

1046
module.exports = {
1147
onPreBuild: async ({ utils }) => {
1248
// bail immediately if this isn’t a production build
1349
if (process.env.CONTEXT !== 'production') return;
1450

15-
await utils.run('cypress', ['install'], { stdio: 'ignore' });
51+
await utils.run('cypress', ['install'], {
52+
stdio: 'ignore',
53+
cwd: __dirname,
54+
});
1655
},
1756
onPostBuild: async ({ constants: { PUBLISH_DIR }, utils, inputs }) => {
1857
// bail immediately if this isn’t a production build
@@ -29,53 +68,13 @@ module.exports = {
2968
.createServer(ecstatic({ root: `${PUBLISH_DIR}` }))
3069
.listen(port);
3170

32-
const applitoolsConfig = {
33-
showLogs: true,
34-
};
35-
36-
await Promise.all([
37-
fs.writeFile(
38-
APPLITOOLS_CONFIG,
39-
`module.exports = ${JSON.stringify(applitoolsConfig)}`,
40-
'utf8',
41-
),
42-
fs.copy(`${__dirname}/template/${VISUAL_DIFF}`, VISUAL_DIFF),
43-
]);
44-
45-
const cypress = require('cypress');
4671
const builtPages = glob
4772
.sync(`${PUBLISH_DIR}/**/*.html`)
4873
.map((p) => path.dirname(p.replace(PUBLISH_DIR, '')));
4974

50-
const results = await cypress.run({
51-
configFile: `${VISUAL_DIFF}/visual-diff.json`,
52-
config: { baseUrl: `http://localhost:${port}`, video: false },
53-
env: {
54-
SITE_NAME: process.env.SITE_NAME || 'localhost-test',
55-
APPLITOOLS_BROWSERS: JSON.stringify(inputs.browser),
56-
APPLITOOLS_FAIL_BUILD_ON_DIFF: inputs.failBuildOnDiff,
57-
APPLITOOLS_SERVER_URL: inputs.serverUrl,
58-
APPLITOOLS_IGNORE_SELECTOR: inputs.ignoreSelector
59-
? inputs.ignoreSelector
60-
.split(',')
61-
.map((selector) => ({ selector: selector.trim() }))
62-
: [],
63-
APPLITOOLS_CONCURRENCY: inputs.concurrency,
64-
PAGES_TO_CHECK: builtPages,
65-
CYPRESS_CACHE_FOLDER: './node_modules/CypressBinary',
66-
},
67-
record: false,
68-
});
69-
70-
await new Promise((resolve, reject) => {
71-
server.close((err) => {
72-
if (err) {
73-
return reject(err);
74-
}
75-
76-
resolve();
77-
});
78-
});
75+
await createEnvFile({ inputs, builtPages });
76+
const results = await runCypress({ utils, port });
77+
await shutdownServer({ server });
7978

8079
if (results.failures) {
8180
utils.build.failPlugin(`Cypress had a problem`, {
@@ -103,10 +102,12 @@ module.exports = {
103102
},
104103
onEnd: async () => {
105104
// cleanup transient files
106-
await Promise.all([
107-
fs.remove(APPLITOOLS_CONFIG),
108-
fs.remove(VISUAL_DIFF),
109-
fs.remove('cypress'),
110-
]);
105+
await Promise.all(
106+
[
107+
`${__dirname}/cypress.env.json`,
108+
`${__dirname}/cypress`,
109+
`${__dirname}/results.json`,
110+
].map((pathToRemove) => fs.remove(pathToRemove)),
111+
);
111112
},
112113
};

0 commit comments

Comments
 (0)