merge 21447: closes #21447
[arvados.git] / services / workbench2 / scripts / test.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 'use strict';
6
7 // Do this as the first thing so that any code reading it knows the right env.
8 process.env.BABEL_ENV = 'test';
9 process.env.NODE_ENV = 'test';
10 process.env.PUBLIC_URL = '';
11
12 // Makes the script crash on unhandled rejections instead of silently
13 // ignoring them. In the future, promise rejections that are not handled will
14 // terminate the Node.js process with a non-zero exit code.
15 process.on('unhandledRejection', err => {
16   throw err;
17 });
18
19 // Ensure environment variables are read.
20 require('../config/env');
21
22 const jest = require('jest');
23 const execSync = require('child_process').execSync;
24 let argv = process.argv.slice(2);
25
26 function isInGitRepository() {
27   try {
28     execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
29     return true;
30   } catch (e) {
31     return false;
32   }
33 }
34
35 function isInMercurialRepository() {
36   try {
37     execSync('hg --cwd . root', { stdio: 'ignore' });
38     return true;
39   } catch (e) {
40     return false;
41   }
42 }
43
44 // Watch unless on CI or explicitly running all tests
45 if (
46   !process.env.CI &&
47   argv.indexOf('--watchAll') === -1 &&
48   argv.indexOf('--watchAll=false') === -1
49 ) {
50   // https://github.com/facebook/create-react-app/issues/5210
51   const hasSourceControl = isInGitRepository() || isInMercurialRepository();
52   argv.push(hasSourceControl ? '--watch' : '--watchAll');
53 }
54
55
56 jest.run(argv);