Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adjust tests to the new env approach
  • Loading branch information
Alexey committed Oct 13, 2017
commit efe3b39be74c2ad00545019edeb0b71cff383201
47 changes: 22 additions & 25 deletions detox/src/Detox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ describe('Detox', () => {
let fs;
let Detox;
let detox;
let minimist;
let clientMockData = {lastConstructorArguments: null};
let deviceMockData = {lastConstructorArguments: null};
const clientMockData = {lastConstructorArguments: null};
const deviceMockData = {lastConstructorArguments: null};

beforeEach(async () => {
function setCustomMock(modulePath, dataObject) {
const JestMock = jest.genMockFromModule(modulePath);
class FinalMock extends JestMock {
constructor() {
super(...arguments);
dataObject.lastConstructorArguments = arguments;
constructor(...rest) {
super(rest);
dataObject.lastConstructorArguments = rest;
}
}
jest.setMock(modulePath, FinalMock);
Expand All @@ -23,12 +22,12 @@ describe('Detox', () => {
jest.mock('npmlog');
jest.mock('fs');
fs = require('fs');
jest.mock('minimist');
minimist = require('minimist');
jest.mock('./ios/expect');
setCustomMock('./client/Client', clientMockData);
setCustomMock('./devices/Device', deviceMockData);

process.env = {};

jest.mock('./devices/IosDriver');
jest.mock('./devices/SimulatorDriver');
jest.mock('./devices/Device');
Expand Down Expand Up @@ -66,7 +65,7 @@ describe('Detox', () => {
});

it(`Passing --cleanup should shutdown the currently running device`, async () => {
mockCommandLineArgs({cleanup: true});
process.env.cleanup = true;
Detox = require('./Detox');

detox = new Detox(schemes.validOneDeviceNoSession);
Expand Down Expand Up @@ -99,7 +98,7 @@ describe('Detox', () => {
});

it(`Two valid devices, detox should init with the device passed in '--configuration' cli option`, async () => {
mockCommandLineArgs({configuration: 'ios.sim.debug'});
process.env.configuration = 'ios.sim.debug';
Detox = require('./Detox');

detox = new Detox(schemes.validTwoDevicesNoSession);
Expand All @@ -108,7 +107,7 @@ describe('Detox', () => {
});

it(`Two valid devices, detox should throw if device passed in '--configuration' cli option doesn't exist`, async () => {
mockCommandLineArgs({configuration: 'nonexistent'});
process.env.configuration = 'nonexistent';
Detox = require('./Detox');

detox = new Detox(schemes.validTwoDevicesNoSession);
Expand All @@ -121,7 +120,7 @@ describe('Detox', () => {
});

it(`Two valid devices, detox should throw if device passed in '--configuration' cli option doesn't exist`, async () => {
mockCommandLineArgs({configuration: 'nonexistent'});
process.env.configuration = 'nonexistent';
Detox = require('./Detox');

detox = new Detox(schemes.validTwoDevicesNoSession);
Expand Down Expand Up @@ -165,22 +164,22 @@ describe('Detox', () => {
});

it(`Detox should use session defined per configuration - none`, async () => {
mockCommandLineArgs({configuration: 'ios.sim.none'});
process.env.configuration = 'ios.sim.none';
Detox = require('./Detox');
detox = new Detox(schemes.sessionPerConfiguration);
await detox.init();

let expectedSession = schemes.sessionPerConfiguration.configurations['ios.sim.none'].session;
const expectedSession = schemes.sessionPerConfiguration.configurations['ios.sim.none'].session;
expect(clientMockData.lastConstructorArguments[0]).toBe(expectedSession);
});

it(`Detox should use session defined per configuration - release`, async () => {
mockCommandLineArgs({configuration: 'ios.sim.release'});
process.env.configuration = 'ios.sim.release';
Detox = require('./Detox');
detox = new Detox(schemes.sessionPerConfiguration);
await detox.init();

let expectedSession = schemes.sessionPerConfiguration.configurations['ios.sim.release'].session;
const expectedSession = schemes.sessionPerConfiguration.configurations['ios.sim.release'].session;
expect(clientMockData.lastConstructorArguments[0]).toBe(expectedSession);
});

Expand All @@ -189,12 +188,12 @@ describe('Detox', () => {
detox = new Detox(schemes.sessionInCommonAndInConfiguration);
await detox.init();

let expectedSession = schemes.sessionInCommonAndInConfiguration.configurations['ios.sim.none'].session;
const expectedSession = schemes.sessionInCommonAndInConfiguration.configurations['ios.sim.none'].session;
expect(clientMockData.lastConstructorArguments[0]).toBe(expectedSession);
});

it(`beforeEach() - should set device artifacts destination`, async () => {
mockCommandLineArgs({'artifacts-location': '/tmp'});
process.env.artifactsLocation = '/tmp';
Detox = require('./Detox');
detox = new Detox(schemes.validOneDeviceAndSession);
await detox.init();
Expand All @@ -211,7 +210,7 @@ describe('Detox', () => {
});

it(`afterEach() - should call device.finalizeArtifacts`, async () => {
mockCommandLineArgs({'artifacts-location': '/tmp'});
process.env.artifactsLocation = '/tmp';
Detox = require('./Detox');
detox = new Detox(schemes.validOneDeviceAndSession);
await detox.init();
Expand All @@ -228,13 +227,11 @@ describe('Detox', () => {
});

it(`the constructor should catch exception from ArtifactsPathsProvider`, async () => {
mockCommandLineArgs({'artifacts-location': '/tmp'});
fs.mkdirSync = jest.fn(() => {throw 'Could not create artifacts root dir'});
process.env.artifactsLocation = '/tmp';
fs.mkdirSync = jest.fn(() => {
throw Error('Could not create artifacts root dir');
});
Detox = require('./Detox');
detox = new Detox(schemes.validOneDeviceAndSession);
});

function mockCommandLineArgs(args) {
minimist.mockReturnValue(args);
}
});
2 changes: 1 addition & 1 deletion detox/src/utils/argparse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function getArgValue(key) {
process.env[key];
return process.env[key];
}

module.exports = {
Expand Down
6 changes: 2 additions & 4 deletions detox/src/utils/argparse.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const _ = require('lodash');
jest.unmock('process');

describe('argparse', () => {
let argparse;

beforeEach(() => {
jest.mock('minimist');
const minimist = require('minimist');
minimist.mockReturnValue({test: 'a value'});
process.env.test = 'a value';
argparse = require('./argparse');
});

Expand Down