forked from percy/percy-cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-tests.js
56 lines (47 loc) · 1.83 KB
/
run-tests.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
const fs = require('fs');
const httpServer = require('http-server');
const port = process.env.PORT_NUMBER || 8000;
const spawn = require('child_process').spawn;
const platform = require('os').platform();
// Helper to make sure directories we need exist.
function ensureDirExists(dir) {
if (! fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
}
// Copy our healthcheck script to node_modules, where it will be
// when this package is installed as a dependency.
const src = 'lib/percy-healthcheck'
const copyDst = /^win/.test(platform)
? `${process.cwd()}\\node_modules\\@percy\\cypress\\dist\\percy-healthcheck`
: `${__dirname}/node_modules/@percy/cypress/dist/percy-healthcheck`;
console.log(`[run-tests] Copying ${src} to ${copyDst}`);
// The 'recursive' fs.mkdirSync() option is only available in Node >10.12.0;
// create all directories ourselves to accommodate Node 8.
ensureDirExists('./node_modules/@percy/cypress')
ensureDirExists('./node_modules/@percy/cypress/dist')
fs.copyFileSync(src, copyDst);
// We need to change the command path based on the platform they're using
const cmd = /^win/.test(platform)
? `${process.cwd()}\\node_modules\\.bin\\cypress.cmd`
: `cypress`;
const server = httpServer.createServer({ root: './cypress/testapp' })
server.listen(port)
console.log(`[run-tests] Server is listening on http://localhost:${port}`)
const tests = spawn(cmd, ['run'], {
stdio: "inherit",
windowsVerbatimArguments: true,
});
// Propagate non-zero exit code from test process.
tests.on('exit', code => {
if (code !== 0) {
console.log(`[run-tests] Tests exited with code ${code}. `)
process.exit(code)
}
});
tests.on('close', () => {
console.log(`[run-tests] Tests completed! Closing server http://localhost:${port}`)
server.close()
console.log(`[run-tests] Deleting ${copyDst}`)
fs.unlinkSync(copyDst)
});