|
| 1 | +const {expect} = require('chai'); |
| 2 | + |
| 3 | +const helper = require('../../../src/utils/helper'); |
| 4 | + |
| 5 | +describe('parseBooleanSetting', () => { |
| 6 | + it('returns undefined when the setting is absent', () => { |
| 7 | + expect(helper.parseBooleanSetting(undefined)).to.eq(undefined); |
| 8 | + expect(helper.parseBooleanSetting(null)).to.eq(undefined); |
| 9 | + }); |
| 10 | + |
| 11 | + it('passes booleans through', () => { |
| 12 | + expect(helper.parseBooleanSetting(true)).to.eq(true); |
| 13 | + expect(helper.parseBooleanSetting(false)).to.eq(false); |
| 14 | + }); |
| 15 | + |
| 16 | + it('accepts the string forms a YAML or env-derived config produces', () => { |
| 17 | + ['true', 'True', ' TRUE ', '1', 'yes', 'on'].forEach((value) => { |
| 18 | + expect(helper.parseBooleanSetting(value), value).to.eq(true); |
| 19 | + }); |
| 20 | + ['false', 'False', ' FALSE ', '0', 'no', 'off', ''].forEach((value) => { |
| 21 | + expect(helper.parseBooleanSetting(value), value).to.eq(false); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('accepts numeric forms', () => { |
| 26 | + expect(helper.parseBooleanSetting(1)).to.eq(true); |
| 27 | + expect(helper.parseBooleanSetting(0)).to.eq(false); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe('isTestHubBuild at build start', () => { |
| 32 | + const envKeys = ['BROWSERSTACK_TEST_OBSERVABILITY', 'BROWSERSTACK_TEST_REPORTING', 'BROWSERSTACK_ACCESSIBILITY']; |
| 33 | + let saved; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + saved = {}; |
| 37 | + envKeys.forEach((key) => { |
| 38 | + saved[key] = process.env[key]; |
| 39 | + delete process.env[key]; |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + envKeys.forEach((key) => { |
| 45 | + if (saved[key] === undefined) { |
| 46 | + delete process.env[key]; |
| 47 | + } else { |
| 48 | + process.env[key] = saved[key]; |
| 49 | + } |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('starts a build whenever reporting resolved to enabled', () => { |
| 54 | + process.env.BROWSERSTACK_TEST_OBSERVABILITY = 'true'; |
| 55 | + expect(helper.isTestHubBuild({}, true)).to.eq(true); |
| 56 | + }); |
| 57 | + |
| 58 | + it('does not start a build when reporting resolved to disabled', () => { |
| 59 | + process.env.BROWSERSTACK_TEST_OBSERVABILITY = 'false'; |
| 60 | + process.env.BROWSERSTACK_TEST_REPORTING = 'false'; |
| 61 | + expect(helper.isTestHubBuild({test_observability: {enabled: true}}, true)).to.eq(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it('starts a build for a truthy accessibility setting', () => { |
| 65 | + process.env.BROWSERSTACK_TEST_OBSERVABILITY = 'false'; |
| 66 | + process.env.BROWSERSTACK_TEST_REPORTING = 'false'; |
| 67 | + expect(helper.isTestHubBuild({accessibility: 'true'}, true)).to.eq(true); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('TestObservability.configure normalises enabled', () => { |
| 72 | + const envKeys = ['BROWSERSTACK_TEST_OBSERVABILITY', 'BROWSERSTACK_TEST_REPORTING']; |
| 73 | + let saved; |
| 74 | + let TestObservability; |
| 75 | + |
| 76 | + before(() => { |
| 77 | + TestObservability = require('../../../src/testObservability'); |
| 78 | + }); |
| 79 | + |
| 80 | + beforeEach(() => { |
| 81 | + saved = {}; |
| 82 | + envKeys.forEach((key) => { |
| 83 | + saved[key] = process.env[key]; |
| 84 | + delete process.env[key]; |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + afterEach(() => { |
| 89 | + envKeys.forEach((key) => { |
| 90 | + if (saved[key] === undefined) { |
| 91 | + delete process.env[key]; |
| 92 | + } else { |
| 93 | + process.env[key] = saved[key]; |
| 94 | + } |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + // configure() disables reporting outright when credentials are absent, so every |
| 99 | + // case below has to carry them for the `enabled` resolution to be observable. |
| 100 | + const withCredentials = (observability = {}) => Object.assign({user: 'USER', key: 'KEY'}, observability); |
| 101 | + |
| 102 | + const configure = (pluginSettings) => { |
| 103 | + new TestObservability().configure({'@nightwatch/browserstack': pluginSettings}); |
| 104 | + }; |
| 105 | + |
| 106 | + it('writes true for a string enabled', () => { |
| 107 | + configure({test_observability: withCredentials({enabled: 'true'})}); |
| 108 | + expect(process.env.BROWSERSTACK_TEST_OBSERVABILITY).to.eq('true'); |
| 109 | + expect(process.env.BROWSERSTACK_TEST_REPORTING).to.eq('true'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('writes true for a numeric enabled', () => { |
| 113 | + configure({test_observability: withCredentials({enabled: 1})}); |
| 114 | + expect(process.env.BROWSERSTACK_TEST_OBSERVABILITY).to.eq('true'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('writes false for a string false', () => { |
| 118 | + configure({test_observability: withCredentials({enabled: 'false'})}); |
| 119 | + expect(process.env.BROWSERSTACK_TEST_OBSERVABILITY).to.eq('false'); |
| 120 | + expect(process.env.BROWSERSTACK_TEST_REPORTING).to.eq('false'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('leaves the default on when enabled is absent', () => { |
| 124 | + configure({test_observability: withCredentials()}); |
| 125 | + expect(process.env.BROWSERSTACK_TEST_OBSERVABILITY).to.eq('true'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('honours a top-level string flag', () => { |
| 129 | + new TestObservability().configure({ |
| 130 | + testObservability: 'false', |
| 131 | + '@nightwatch/browserstack': {test_observability: withCredentials()} |
| 132 | + }); |
| 133 | + expect(process.env.BROWSERSTACK_TEST_OBSERVABILITY).to.eq('false'); |
| 134 | + }); |
| 135 | +}); |
0 commit comments