test: fix unit test failures for certain random orders (#1094)

* test(Api): do not clobber global events.emit w/ spy

* test(Api): remove unnecessary rewiring

* test(check_reqs): add missing spyOn call

* test(check_reqs): fix process.env restoration

* test(check_reqs): restore module under test before each test
This commit is contained in:
Raphael von der Grün 2020-10-19 10:38:37 +02:00 committed by GitHub
parent b1f01d7a65
commit 335b0f2575
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 22 deletions

View File

@ -20,8 +20,9 @@
var os = require('os');
var path = require('path');
var common = require('cordova-common');
var rewire = require('rewire');
const EventEmitter = require('events');
var Api = require('../../bin/templates/cordova/Api');
var AndroidProject = require('../../bin/templates/cordova/lib/AndroidProject');
var PluginInfo = common.PluginInfo;
@ -31,11 +32,9 @@ var FAKE_PROJECT_DIR = path.join(os.tmpdir(), 'plugin-test-project');
describe('Api', () => {
describe('addPlugin method', function () {
var api, Api;
var api;
beforeEach(function () {
Api = rewire('../../bin/templates/cordova/Api');
var pluginManager = jasmine.createSpyObj('pluginManager', ['addPlugin']);
pluginManager.addPlugin.and.resolveTo();
spyOn(common.PluginManager, 'get').and.returnValue(pluginManager);
@ -43,12 +42,7 @@ describe('Api', () => {
var projectSpy = jasmine.createSpyObj('AndroidProject', ['getPackageName', 'write', 'isClean']);
spyOn(AndroidProject, 'getProjectFile').and.returnValue(projectSpy);
Api.__set__('Api.prototype.clean', async () => {});
// Prevent logging to avoid polluting the test reports
Api.__set__('selfEvents.emit', jasmine.createSpy());
api = new Api('android', FAKE_PROJECT_DIR);
api = new Api('android', FAKE_PROJECT_DIR, new EventEmitter());
spyOn(api._builder, 'prepBuildFiles');
});

View File

@ -18,7 +18,6 @@
*/
var rewire = require('rewire');
var check_reqs = rewire('../../bin/templates/cordova/lib/check_reqs');
var android_sdk = require('../../bin/templates/cordova/lib/android_sdk');
var fs = require('fs-extra');
var path = require('path');
@ -29,14 +28,22 @@ var which = require('which');
const DEFAULT_TARGET_API = 29;
describe('check_reqs', function () {
let check_reqs;
beforeEach(() => {
check_reqs = rewire('../../bin/templates/cordova/lib/check_reqs');
});
var original_env;
beforeAll(function () {
original_env = Object.create(process.env);
original_env = Object.assign({}, process.env);
});
afterEach(function () {
Object.keys(original_env).forEach(function (k) {
process.env[k] = original_env[k];
// process.env has some special behavior, so we do not
// replace it but only restore its original properties
Object.keys(process.env).forEach(k => {
delete process.env[k];
});
Object.assign(process.env, original_env);
});
describe('check_android', function () {
describe('find and set ANDROID_HOME when ANDROID_HOME and ANDROID_SDK_ROOT is not set', function () {
@ -55,9 +62,6 @@ describe('check_reqs', function () {
process.env.ProgramFiles = 'windows-program-files';
return check_reqs.check_android().then(function () {
expect(process.env.ANDROID_SDK_ROOT).toContain('windows-local-app-data');
}).finally(function () {
delete process.env.LOCALAPPDATA;
delete process.env.ProgramFiles;
});
});
it('it should set ANDROID_SDK_ROOT on Darwin', () => {
@ -66,8 +70,6 @@ describe('check_reqs', function () {
process.env.HOME = 'home is where the heart is';
return check_reqs.check_android().then(function () {
expect(process.env.ANDROID_SDK_ROOT).toContain('home is where the heart is');
}).finally(function () {
delete process.env.HOME;
});
});
});
@ -220,9 +222,6 @@ describe('check_reqs', function () {
process.env.ANDROID_SDK_ROOT = 'let the children play';
spyOn(fs, 'existsSync').and.returnValue(true);
});
afterEach(function () {
delete process.env.ANDROID_SDK_ROOT;
});
it('should add tools/bin,tools,platform-tools to PATH if `avdmanager`,`android`,`adb` is not found', () => {
return check_reqs.check_android().then(function () {
expect(process.env.PATH).toContain('let the children play' + path.sep + 'tools');
@ -348,6 +347,8 @@ describe('check_reqs', function () {
}
});
spyOn(events, 'emit');
getPreferenceSpy.and.returnValue(DEFAULT_TARGET_API - 1);
var target = check_reqs.get_target();