Skip to content

Commit

Permalink
fixes #88, handle connection errors for analytics (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
swrdfish authored May 17, 2023
1 parent fb6a1e4 commit 559a58d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ export class NightwatchInit {
}

if (answers.allowAnonymousMetrics) {
this.pushAnonymousMetrics(answers);
try {
this.pushAnonymousMetrics(answers);
} catch (err) {
// do nothing
}
}
}

Expand Down Expand Up @@ -1279,6 +1283,9 @@ export class NightwatchInit {

const req = https.request(options);
req.write(data);
req.on('error', () => {
// ignore connection errors
});
req.end();
}
}
57 changes: 57 additions & 0 deletions test/e2e_tests/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,61 @@ describe('e2e tests for init', function() {
});
});

it('make sure there are now errors even if analytics request fails', async function() {
const consoleOutput = [];
mockLogger(consoleOutput);

const commandsExecuted = [];
mockery.registerMock('child_process', {
execSync(command, options) {
commandsExecuted.push(command);
}
});

const answers = {
testingType: ['e2e'],
language: 'ts',
runner: 'nightwatch',
backend: 'both',
cloudProvider: 'other',
browsers: ['firefox'],
remoteBrowsers: ['chrome'],
baseUrl: 'https://nightwatchjs.org',
testsLocation: 'tests',
allowAnonymousMetrics: true
};

const scope = nock('https://www.google-analytics.com')
.post('/mp/collect?api_secret=XuPojOTwQ6yTO758EV4hBg&measurement_id=G-DEKPKZSLXS')
.replyWithError({
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED'
});

const {NightwatchInit} = require('../../lib/init');
const nightwatchInit = new NightwatchInit(rootDir, {'generate-config': true});

nightwatchInit.askQuestions = function() {
return answers;
};

const configPath = path.join(rootDir, 'nightwatch.conf.js');
nightwatchInit.getConfigDestPath = function() {
return configPath;
};

await nightwatchInit.run();

new Promise(resolve => {
setTimeout(function() {
assert.ok(scope.isDone());
resolve();
}, 0);

rmDirSync(rootDir);
});
});

it('make sure we do not send analytics data if allowAnalytics is set to false', async function() {
const consoleOutput = [];
mockLogger(consoleOutput);
Expand Down Expand Up @@ -1919,4 +1974,6 @@ describe('e2e tests for init', function() {

rmDirSync(rootDir);
});


});

0 comments on commit 559a58d

Please sign in to comment.