Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Nightwatch / globals.js
1 import { spawn } from 'child_process';
2 import path from 'path';
3 import fs from 'fs';
4 import mkdirp from 'mkdirp';
5 import chromedriver from 'chromedriver';
6 import nightwatchSettings from './nightwatch.conf';
7
8 const commandAsWebserver = command => {
9   if (process.env.DRUPAL_TEST_WEBSERVER_USER) {
10     return `sudo -u ${process.env.DRUPAL_TEST_WEBSERVER_USER} ${command}`;
11   }
12   return command;
13 };
14
15 module.exports = {
16   before: done => {
17     if (JSON.parse(process.env.DRUPAL_TEST_CHROMEDRIVER_AUTOSTART)) {
18       chromedriver.start();
19     }
20     done();
21   },
22   after: done => {
23     if (JSON.parse(process.env.DRUPAL_TEST_CHROMEDRIVER_AUTOSTART)) {
24       chromedriver.stop();
25     }
26     done();
27   },
28   afterEach: (browser, done) => {
29     // Writes the console log - used by the "logAndEnd" command.
30     if (
31       browser.drupalLogConsole &&
32       (!browser.drupalLogConsoleOnlyOnError ||
33         browser.currentTest.results.errors > 0 ||
34         browser.currentTest.results.failed > 0)
35     ) {
36       const resultPath = path.join(
37         __dirname,
38         `../../../${nightwatchSettings.output_folder}/consoleLogs/${
39           browser.currentTest.module
40         }`,
41       );
42       const status =
43         browser.currentTest.results.errors > 0 ||
44         browser.currentTest.results.failed > 0
45           ? 'FAILED'
46           : 'PASSED';
47       mkdirp.sync(resultPath);
48       const now = new Date().toString().replace(/[\s]+/g, '-');
49       const testName = (
50         browser.currentTest.name || browser.currentTest.module
51       ).replace(/[\s/]+/g, '-');
52       browser
53         .getLog('browser', logEntries => {
54           const browserLog = JSON.stringify(logEntries, null, '  ');
55           fs.writeFileSync(
56             `${resultPath}/${testName}_${status}_${now}_console.json`,
57             browserLog,
58           );
59         })
60         .end(done);
61     } else {
62       browser.end(done);
63     }
64   },
65   commandAsWebserver,
66 };