Skip to content

Commit

Permalink
fix: make Detox runtime config key names identical to Detox static co…
Browse files Browse the repository at this point in the history
…nfig
  • Loading branch information
noomorph committed Aug 15, 2022
1 parent 6eb5c15 commit b0d1f2e
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 81 deletions.
21 changes: 13 additions & 8 deletions detox/internals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,19 @@ declare global {
type RuntimeConfig = Readonly<{
configurationName: string;

appsConfig: Record<string, Readonly<Detox.DetoxAppConfig>>;
artifactsConfig: Detox.DetoxArtifactsConfig;
behaviorConfig: Detox.DetoxBehaviorConfig;
cliConfig: DetoxCLIConfig;
deviceConfig: Detox.DetoxDeviceConfig;
loggerConfig: Detox.DetoxLoggerConfig;
runnerConfig: Detox.DetoxTestRunnerConfig;
sessionConfig: Detox.DetoxSessionConfig;
/**
* Dictionary of app configurations,
* where the keys are defined by {@link Detox.DetoxAppConfig#name}
* or equal to "default" if the name is not configured.
*/
apps: Record<string, Readonly<Detox.DetoxAppConfig>>;
artifacts: Readonly<Detox.DetoxArtifactsConfig>;
behavior: Readonly<Detox.DetoxBehaviorConfig>;
cli: Readonly<DetoxCLIConfig>;
device: Readonly<Detox.DetoxDeviceConfig>;
logger: Readonly<Detox.DetoxLoggerConfig>;
testRunner: Readonly<Detox.DetoxTestRunnerConfig>;
session: Readonly<Detox.DetoxSessionConfig>;
}>;

type DetoxCLIConfig = Readonly<Partial<{
Expand Down
2 changes: 1 addition & 1 deletion detox/local-cli/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports.builder = {
};

module.exports.handler = async function build(argv) {
const { appsConfig, errorComposer } = await detox.resolveConfig({ argv });
const { apps: appsConfig, errorComposer } = await detox.resolveConfig({ argv });
const apps = _.entries(appsConfig);

for (const [appName, app] of apps) {
Expand Down
28 changes: 14 additions & 14 deletions detox/local-cli/build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ describe('build', () => {
const DetoxConfigErrorComposer = require('../src/errors/DetoxConfigErrorComposer');

const config = {
appsConfig: {},
artifactsConfig: {},
behaviorConfig: {},
apps: {},
artifacts: {},
behavior: {},
errorComposer: new DetoxConfigErrorComposer(),
deviceConfig: {},
sessionConfig: {}
device: {},
session: {}
};

return ({
Expand All @@ -44,14 +44,14 @@ describe('build', () => {
});

it('runs the build script from the composed device config', async () => {
detox.config.appsConfig.default = { build: 'yet another command' };
detox.config.apps.default = { build: 'yet another command' };

await callCli('./build', 'build');
expect(execSync).toHaveBeenCalledWith('yet another command', expect.anything());
});

it('skips building the app if the binary exists and --if-missing flag is set', async () => {
detox.config.appsConfig.default = { build: 'yet another command', binaryPath: __filename };
detox.config.apps.default = { build: 'yet another command', binaryPath: __filename };

await callCli('./build', 'build -i');
expect(execSync).not.toHaveBeenCalled();
Expand All @@ -63,40 +63,40 @@ describe('build', () => {
});

it('fails with an error if a build script has not been found', async () => {
detox.config.appsConfig.default = {};
detox.config.apps.default = {};
await expect(callCli('./build', 'build')).rejects.toThrowError(/Failed to build/);
});

it('should ignore missing build command with -s, --silent flag', async () => {
detox.config.appsConfig.default = {};
detox.config.apps.default = {};
await expect(callCli('./build', 'build -s')).resolves.not.toThrowError();
expect(detox.log.warn).not.toHaveBeenCalled();
});

it('should print a warning upon user build script failure', async () => {
detox.config.appsConfig.default = { build: 'a command' };
detox.config.apps.default = { build: 'a command' };
execSync.mockImplementation(() => { throw new Error('Build failure'); });
await expect(callCli('./build', 'build')).rejects.toThrowError(/Build failure/);
expect(detox.log.warn).toHaveBeenCalledWith(expect.stringContaining('You are responsible'));
});

it('should print a warning if app is not found at binary path', async () => {
detox.config.appsConfig.default = { binaryPath: tempfile() };
detox.config.apps.default = { binaryPath: tempfile() };
await expect(callCli('./build', 'build -s')).resolves.not.toThrowError();
expect(detox.log.warn).toHaveBeenCalledWith(expect.stringContaining('could not find your app at the given binary path'));
});

it('should print extra message with the app name before building (in a multi-app configuration)', async () => {
detox.config.appsConfig.app1 = { binaryPath: tempfile(), build: ':' };
detox.config.appsConfig.app2 = { binaryPath: tempfile(), build: ':' };
detox.config.apps.app1 = { binaryPath: tempfile(), build: ':' };
detox.config.apps.app2 = { binaryPath: tempfile(), build: ':' };

await expect(callCli('./build', 'build -s')).resolves.not.toThrowError();
expect(detox.log.info).toHaveBeenCalledWith(expect.stringContaining('app1'));
expect(detox.log.info).toHaveBeenCalledWith(expect.stringContaining('app2'));
});

it('should not print that extra message when the app is single', async () => {
detox.config.appsConfig.default = { binaryPath: tempfile(), build: ':' };
detox.config.apps.default = { binaryPath: tempfile(), build: ':' };

await expect(callCli('./build', 'build -s')).resolves.not.toThrowError();
expect(detox.log.info).not.toHaveBeenCalledWith(expect.stringContaining('default'));
Expand Down
6 changes: 3 additions & 3 deletions detox/local-cli/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ module.exports.handler = async function test({ detoxArgs, runnerArgs }) {
});

const runnerCommand = new TestRunnerCommand()
.setRunnerConfig(detox.config.runnerConfig)
.setDeviceConfig(detox.config.deviceConfig)
.replicateCLIConfig(detox.config.cliConfig);
.setRunnerConfig(detox.config.testRunner)
.setDeviceConfig(detox.config.device)
.replicateCLIConfig(detox.config.cli);

await runnerCommand.execute();
} finally {
Expand Down
2 changes: 1 addition & 1 deletion detox/local-cli/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('CLI', () => {
});
});

test('should use runnerConfig.specs as default specs', async () => {
test('should use testRunner.args._ as default specs', async () => {
detoxConfig.testRunner.args._ = ['e2e/sanity'];
await run();
expect(_.last(cliCall().argv)).toEqual('e2e/sanity');
Expand Down
2 changes: 1 addition & 1 deletion detox/runners/jest/testEnvironment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DetoxCircusEnvironment extends NodeEnvironment {
/** @protected */
this.testEventListeners = [];
/** @protected */
this.initTimeout = detox.config.runnerConfig.jest.initTimeout;
this.initTimeout = detox.config.testRunner.jest.initTimeout;
}

/** @override */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DetoxCoreListener {

async setup() {
// Workaround to override Jest's expect
if (detoxInternals.config.behaviorConfig.init.exposeGlobals) {
if (detoxInternals.config.behavior.init.exposeGlobals) {
this._env.global.expect = detox.expect;
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ class DetoxCoreListener {

async run_finish(_event, state) {
if (this._hasFailedTests(state.rootDescribeBlock)) {
const handledByJestCircus = this._testRunTimes > 1 && !detoxInternals.config.runnerConfig.jest.retryAfterCircusRetries;
const handledByJestCircus = this._testRunTimes > 1 && !detoxInternals.config.testRunner.jest.retryAfterCircusRetries;
await detoxInternals.reportFailedTests([this._env.testPath], handledByJestCircus);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SpecReporter {
}

get enabled() {
const jestSection = config.runnerConfig.jest;
const jestSection = config.testRunner.jest;
const reportSpecs = jestSection && jestSection.reportSpecs;

return reportSpecs !== undefined ? reportSpecs : session.workersCount === 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WorkerAssignReporter {
}

run_start() {
if (config.runnerConfig.jest.reportWorkerAssign) {
if (config.testRunner.jest.reportWorkerAssign) {
log.info({ event: 'WORKER_ASSIGN' }, `${this._formatTestName()} is assigned to ${this._formatDeviceName()}`);
}
}
Expand Down
8 changes: 7 additions & 1 deletion detox/src/DetoxWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ class DetoxWorker {
async init() {
if (this._isCleaningUp) return;

const { appsConfig, artifactsConfig, behaviorConfig, deviceConfig, sessionConfig } = this._config;
const {
apps: appsConfig,
artifacts: artifactsConfig,
behavior: behaviorConfig,
device: deviceConfig,
session: sessionConfig
} = this._config;
this._appsConfig = appsConfig;
this._artifactsConfig = artifactsConfig;
this._behaviorConfig = behaviorConfig;
Expand Down
32 changes: 16 additions & 16 deletions detox/src/DetoxWorker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('DetoxWorker', () => {
expect(envValidator.validate).toHaveBeenCalled());

it('should allocate a device', () => {
expect(deviceAllocator.allocate).toHaveBeenCalledWith(detoxConfig.deviceConfig);
expect(deviceAllocator.allocate).toHaveBeenCalledWith(detoxConfig.device);
});

it('should create a runtime-device based on the allocation result (cookie)', () =>
Expand All @@ -140,9 +140,9 @@ describe('DetoxWorker', () => {
runtimeErrorComposer: expect.any(Object),
},
{
appsConfig: detoxConfig.appsConfig,
behaviorConfig: detoxConfig.behaviorConfig,
deviceConfig: detoxConfig.deviceConfig,
appsConfig: detoxConfig.apps,
behaviorConfig: detoxConfig.behavior,
deviceConfig: detoxConfig.device,
sessionConfig: expect.any(Object),
},
));
Expand All @@ -168,7 +168,7 @@ describe('DetoxWorker', () => {
expect(global.globalMatcher).toBe(mockGlobalMatcher));

it('should create artifacts manager', () =>
expect(artifactsManagerFactory.createArtifactsManager).toHaveBeenCalledWith(detoxConfig.artifactsConfig, expect.objectContaining({
expect(artifactsManagerFactory.createArtifactsManager).toHaveBeenCalledWith(detoxConfig.artifacts, expect.objectContaining({
client: client(),
eventEmitter: eventEmitter(),
})));
Expand All @@ -189,12 +189,12 @@ describe('DetoxWorker', () => {

describe('with multiple apps', () => {
beforeEach(() => {
detoxConfig.appsConfig['extraApp'] = {
detoxConfig.apps['extraApp'] = {
type: 'ios.app',
binaryPath: 'path/to/app',
};

detoxConfig.appsConfig['extraAppWithAnotherArguments'] = {
detoxConfig.apps['extraAppWithAnotherArguments'] = {
type: 'ios.app',
binaryPath: 'path/to/app',
launchArgs: {
Expand All @@ -218,9 +218,9 @@ describe('DetoxWorker', () => {
});
});

describe('with behaviorConfig.init.exposeGlobals = false', () => {
describe('with behavior.init.exposeGlobals = false', () => {
beforeEach(() => {
detoxConfig.behaviorConfig.init.exposeGlobals = false;
detoxConfig.behavior.init.exposeGlobals = false;
});

beforeEach(init);
Expand All @@ -239,9 +239,9 @@ describe('DetoxWorker', () => {
expect(global.globalMatcher).toBe(undefined));
});

describe('with behaviorConfig.init.reinstallApp = false', () => {
describe('with behavior.init.reinstallApp = false', () => {
beforeEach(() => {
detoxConfig.behaviorConfig.init.reinstallApp = false;
detoxConfig.behavior.init.reinstallApp = false;
});

beforeEach(init);
Expand Down Expand Up @@ -521,9 +521,9 @@ describe('DetoxWorker', () => {
});
});

describe('when behaviorConfig.cleanup.shutdownDevice = true', () => {
describe('when behavior.cleanup.shutdownDevice = true', () => {
beforeEach(async () => {
detoxConfig.behaviorConfig.cleanup.shutdownDevice = true;
detoxConfig.behavior.cleanup.shutdownDevice = true;
detox = await new Detox(detoxContext).init();
});

Expand Down Expand Up @@ -570,7 +570,7 @@ describe('DetoxWorker', () => {
// TODO: move it to the root realm
describe.skip('global context', () => {
const configs = {
deviceConfig: {
device: {
mock: 'config',
},
};
Expand All @@ -594,7 +594,7 @@ describe('DetoxWorker', () => {

await Detox.globalInit(configs);
expect(lifecycleHandler.globalInit).toHaveBeenCalled();
expect(environmentFactory.createGlobalLifecycleHandler).toHaveBeenCalledWith(configs.deviceConfig);
expect(environmentFactory.createGlobalLifecycleHandler).toHaveBeenCalledWith(configs.device);
});

it(`should not invoke init if no handler was resolved`, async () => {
Expand All @@ -607,7 +607,7 @@ describe('DetoxWorker', () => {

await Detox.globalCleanup(configs);
expect(lifecycleHandler.globalCleanup).toHaveBeenCalled();
expect(environmentFactory.createGlobalLifecycleHandler).toHaveBeenCalledWith(configs.deviceConfig);
expect(environmentFactory.createGlobalLifecycleHandler).toHaveBeenCalledWith(configs.device);
});

it(`should not invoke cleanup if no handler was resolved`, async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ HINT: Check that in your Detox config.
`;

exports[`composeRunnerConfig unhappy scenarios non-string and non-object "testRunner" in global config 1`] = `
"testRunner should be an object, not a function
"testRunner should be an object, not a number
HINT: Check that in your Detox config.
{
testRunner: [Function (anonymous)],
testRunner: 0,
configurations: [Object]
}"
`;
2 changes: 1 addition & 1 deletion detox/src/configuration/composeRunnerConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ describe('composeRunnerConfig', () => {

describe('unhappy scenarios', () => {
test('non-string and non-object "testRunner" in global config', () => {
globalConfig['testRunner'] = () => ({ jest: {} });
globalConfig['testRunner'] = 0;
expect(composeRunnerConfig).toThrowErrorMatchingSnapshot();
});

Expand Down
17 changes: 9 additions & 8 deletions detox/src/configuration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,17 @@ async function composeDetoxConfig({
});

const result = {
appsConfig,
artifactsConfig,
behaviorConfig,
cliConfig,
configurationName,
deviceConfig,
errorComposer,
loggerConfig,
runnerConfig,
sessionConfig,

apps: appsConfig,
artifacts: artifactsConfig,
behavior: behaviorConfig,
cli: cliConfig,
device: deviceConfig,
logger: loggerConfig,
testRunner: runnerConfig,
session: sessionConfig,
};

return result;
Expand Down

0 comments on commit b0d1f2e

Please sign in to comment.