Skip to content

Commit c0658a8

Browse files
committed
refactor(config): unify local-cli and detox.init
1 parent e90a1c5 commit c0658a8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1553
-1236
lines changed

detox/__tests__/setupJest.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,29 @@ const path = require('path');
55

66
function callCli(modulePath, cmd) {
77
return new Promise((resolve, reject) => {
8-
try {
9-
yargs
10-
.scriptName('detox')
11-
.command(require(path.join(__dirname, "../local-cli", modulePath)))
12-
.exitProcess(false)
13-
.fail((msg, err) => reject(err || msg))
14-
.parse(cmd, (err, argv, output) => {
15-
err ? reject(err) : setImmediate(() => resolve(output));
16-
});
17-
} catch (e) {
18-
reject(e);
19-
}
20-
});
21-
}
8+
const originalModule = require(path.join(__dirname, "../local-cli", modulePath));
9+
const originalHandler = originalModule.handler;
10+
const spiedModule = {
11+
...originalModule,
12+
handler: async program => {
13+
try {
14+
return await originalHandler(program);
15+
} catch (e) {
16+
reject(e);
17+
} finally {
18+
resolve();
19+
}
20+
}
21+
};
2222

23-
function mockDetoxConfig(mockContent) {
24-
jest.mock('cosmiconfig', () => ({
25-
cosmiconfigSync: (key) => ({
26-
search: () => ({ config: {
27-
detox: mockContent
28-
}[key]
29-
})
30-
})
31-
}));
23+
return yargs
24+
.scriptName('detox')
25+
.command(spiedModule)
26+
.exitProcess(false)
27+
.fail((msg, err) => reject(err || msg))
28+
.parse(cmd, (err) => err && reject(err));
29+
});
3230
}
3331

34-
global.mockDetoxConfig = mockDetoxConfig;
3532
global.callCli = callCli;
3633
global.IS_RUNNING_DETOX_UNIT_TESTS = true;
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`build fails with multiple configs if none is selected 1`] = `
4-
"Cannot determine which configuration to use. Use --configuration to choose one of the following:
5-
* only
6-
* myconf"
7-
`;
8-
9-
exports[`build fails without build script 1`] = `"Could not find build script in detox.configurations[\\"only\\"].build"`;
10-
11-
exports[`build fails without build script and configuration 1`] = `"Could not find build script in detox.configurations[\\"only\\"].build"`;
12-
13-
exports[`build fails without configurations 1`] = `"There are no \\"configurations\\" in \\"detox\\" section of package.json"`;
3+
exports[`build fails with an error if a build script has not been found 1`] = `"Could not find build script for \\"testConfig\\" in Detox config at: /etc/detox/config"`;
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`test fails with a different runner 1`] = `"ava is not supported in detox cli tools. You can still run your tests with the runner's own cli tool"`;
3+
exports[`test fails with a different runner 1`] = `
4+
"\\"ava\\" is not supported in Detox CLI tools.
5+
6+
HINT: You can still run your tests with the runner's own CLI tool"
7+
`;

detox/local-cli/build.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
const _ = require('lodash');
22
const cp = require('child_process');
33
const log = require('../src/utils/logger').child({ __filename });
4-
const {getDefaultConfiguration, getConfigurationByKey} = require('./utils/configurationUtils');
4+
const {composeDetoxConfig} = require('../src/configuration');
55

66
module.exports.command = 'build';
77
module.exports.desc = "Convenience method. Run the command defined in 'build' property of the specified configuration.";
88
module.exports.builder = {
9+
C: {
10+
alias: 'config-path',
11+
group: 'Configuration:',
12+
describe: 'Specify Detox config file path. If not supplied, detox searches for .detoxrc[.js] or "detox" section in package.json',
13+
},
914
c: {
1015
alias: 'configuration',
1116
group: 'Configuration:',
1217
describe:
1318
"Select a device configuration from your defined configurations, if not supplied, and there's only one configuration, detox will default to it",
14-
default: getDefaultConfiguration(),
15-
}
19+
},
1620
};
1721

1822
module.exports.handler = async function build(argv) {
19-
const buildScript = getConfigurationByKey(argv.configuration).build;
23+
const { meta, deviceConfig } = await composeDetoxConfig({ argv });
24+
25+
const buildScript = deviceConfig.build;
2026

2127
if (buildScript) {
2228
log.info(buildScript);
2329
cp.execSync(buildScript, { stdio: 'inherit' });
2430
} else {
25-
throw new Error(`Could not find build script in detox.configurations["${argv.configuration}"].build`);
31+
throw new Error(`Could not find build script for "${meta.configuration}" in Detox config at: ${meta.location}`);
2632
}
2733
};

detox/local-cli/build.test.js

Lines changed: 31 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,47 @@
1+
jest.mock('child_process');
12
jest.mock('../src/utils/logger');
3+
jest.mock('../src/configuration');
24

35
describe('build', () => {
4-
let mockExec;
6+
let execSync, composeDetoxConfig, detoxConfig;
7+
58
beforeEach(() => {
6-
mockExec = jest.fn();
7-
jest.mock('child_process', () => ({
8-
execSync: mockExec
9-
}));
9+
detoxConfig = {
10+
meta: {
11+
configuration: 'testConfig',
12+
location: '/etc/detox/config',
13+
},
14+
artifactsConfig: {},
15+
behaviorConfig: {},
16+
deviceConfig: {},
17+
sessionConfig: {},
18+
};
19+
20+
execSync = require('child_process').execSync;
21+
composeDetoxConfig = require('../src/configuration').composeDetoxConfig;
22+
composeDetoxConfig.mockReturnValue(Promise.resolve(detoxConfig));
1023
});
1124

12-
it('runs the build script if there is only one config', async () => {
13-
mockDetoxConfig({
14-
configurations: {
15-
only: {
16-
build: 'echo "only"'
17-
}
18-
}
19-
});
20-
21-
await callCli('./build', 'build');
22-
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('only'), expect.anything());
23-
});
25+
it('passes argv to composeConfig', async () => {
26+
await callCli('./build', 'build -C /etc/.detoxrc.js -c myconf').catch(() => {});
2427

25-
it('runs the build script of selected config', async () => {
26-
mockDetoxConfig({
27-
configurations: {
28-
only: {
29-
build: 'echo "only"'
30-
},
31-
myconf: {
32-
build: 'echo "myconf"'
33-
}
34-
}
28+
expect(composeDetoxConfig).toHaveBeenCalledWith({
29+
argv: expect.objectContaining({
30+
'config-path': '/etc/.detoxrc.js',
31+
'configuration': 'myconf',
32+
}),
3533
});
36-
37-
await callCli('./build', 'build -c myconf');
38-
expect(mockExec).toHaveBeenCalledWith(expect.stringContaining('myconf'), expect.anything());
3934
});
4035

41-
it('fails with multiple configs if none is selected', async () => {
42-
mockDetoxConfig({
43-
configurations: {
44-
only: {
45-
build: 'echo "only"'
46-
},
47-
myconf: {
48-
build: 'echo "myconf"'
49-
}
50-
}
51-
});
52-
53-
await expect(callCli('./build', 'build')).rejects.toThrowErrorMatchingSnapshot();
54-
expect(mockExec).not.toHaveBeenCalled();
55-
});
36+
it('runs the build script from the composed device config', async () => {
37+
detoxConfig.deviceConfig.build = 'yet another command';
5638

57-
it('fails without configurations', async () => {
58-
mockDetoxConfig({});
59-
60-
await expect(callCli('./build', 'build')).rejects.toThrowErrorMatchingSnapshot();
61-
expect(mockExec).not.toHaveBeenCalled();
62-
});
63-
64-
it('fails without build script', async () => {
65-
mockDetoxConfig({
66-
configurations: {
67-
only: {}
68-
}
69-
});
70-
71-
await expect(callCli('./build', 'build -c only')).rejects.toThrowErrorMatchingSnapshot();
72-
expect(mockExec).not.toHaveBeenCalled();
39+
await callCli('./build', 'build');
40+
expect(execSync).toHaveBeenCalledWith('yet another command', expect.anything());
7341
});
7442

75-
it('fails without build script and configuration', async () => {
76-
mockDetoxConfig({
77-
configurations: {
78-
only: {}
79-
}
80-
});
81-
43+
it('fails with an error if a build script has not been found', async () => {
44+
delete detoxConfig.deviceConfig.build;
8245
await expect(callCli('./build', 'build')).rejects.toThrowErrorMatchingSnapshot();
83-
expect(mockExec).not.toHaveBeenCalled();
8446
});
8547
});

0 commit comments

Comments
 (0)