Skip to content

Commit

Permalink
test: fix unit test failures for certain random orders (#1094)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
raphinesse authored Oct 19, 2020
1 parent b1f01d7 commit 335b0f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
14 changes: 4 additions & 10 deletions spec/unit/Api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,24 +32,17 @@ 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);

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');
});

Expand Down
25 changes: 13 additions & 12 deletions spec/unit/check_reqs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 () {
Expand All @@ -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', () => {
Expand All @@ -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;
});
});
});
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -348,6 +347,8 @@ describe('check_reqs', function () {
}
});

spyOn(events, 'emit');

getPreferenceSpy.and.returnValue(DEFAULT_TARGET_API - 1);

var target = check_reqs.get_target();
Expand Down

0 comments on commit 335b0f2

Please sign in to comment.