Skip to content

Commit

Permalink
test: achieved 100% coverage for ArtifactsManager.js
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Jun 29, 2018
1 parent af57ca4 commit d00ef63
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 5 deletions.
1 change: 0 additions & 1 deletion detox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"src/artifacts/log",
"src/artifacts/screenshot",
"src/artifacts/video",
"src/artifacts/ArtifactsManager.js",
".*Driver.js",
"EmulatorTelnet.js",
"Emulator.js",
Expand Down
7 changes: 5 additions & 2 deletions detox/src/artifacts/ArtifactsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ class ArtifactsManager {
},

requestIdleCallback: (callback, caller) => {
callback._from = caller.name;
if (caller) {
callback._from = caller.name;
}

this._onIdleCallbacks.push(callback);

this._idlePromise = this._idlePromise.then(() => {
Expand Down Expand Up @@ -186,7 +189,7 @@ class ArtifactsManager {
}

log.info('ArtifactsManager', 'finalizing all artifacts, this can take some time');
await Promise.all(this._artifactPlugins.map(plugin => plugin.onTerminate()));
await this._emit('onTerminate', []);
await Promise.all(this._onIdleCallbacks.splice(0).map(this._executeIdleCallback));
await this._idlePromise;

Expand Down
57 changes: 56 additions & 1 deletion detox/src/artifacts/ArtifactsManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,17 @@ describe('ArtifactsManager', () => {

it('should catch errors, report them if callback fails, and move on ', async () => {
artifactsApi.requestIdleCallback(callbacks[0], testPlugin); await sleep(0);
await rejects[0](new Error());
await rejects[0](new Error('test onIdleCallback error'));

expect(proxy.npmlog.error.mock.calls).toMatchSnapshot();

artifactsApi.requestIdleCallback(callbacks[1], testPlugin); await sleep(0);
expect(callbacks[1]).toHaveBeenCalled();
});

it('should gracefully handle a case when plugin object is not passed and use "unknown" as a name placeholder', async () => {
artifactsApi.requestIdleCallback(callbacks[0]); await sleep(0);
await rejects[0](new Error('test onIdleCallback error'));

expect(proxy.npmlog.error.mock.calls).toMatchSnapshot();

Expand Down Expand Up @@ -278,6 +288,51 @@ describe('ArtifactsManager', () => {
});

describe('hooks', () => {
describe('error handling', () => {
function itShouldCatchErrorsOnPhase(eventNames, argFactory) {
const managerMethod = Array.isArray(eventNames) ? eventNames[0] : eventNames;
const pluginHook = Array.isArray(eventNames) ? eventNames[1] : eventNames;

it(`should catch .${pluginHook} errors`, async () => {
testPlugin[pluginHook].mockImplementation(() => {
throw new Error(`test ${pluginHook} error`);
});

await artifactsManager[managerMethod](argFactory());
expect(proxy.npmlog.error.mock.calls).toMatchSnapshot();
});
}

itShouldCatchErrorsOnPhase('onBeforeAll', () => undefined);

itShouldCatchErrorsOnPhase('onBeforeEach', () => testSummaries.running());

itShouldCatchErrorsOnPhase('onAfterEach', () => testSummaries.passed());

itShouldCatchErrorsOnPhase('onAfterAll', () => undefined);

itShouldCatchErrorsOnPhase('onTerminate', () => undefined);

itShouldCatchErrorsOnPhase('onBeforeResetDevice', () => ({
deviceId: 'testDeviceId'
}));

itShouldCatchErrorsOnPhase('onResetDevice', () => ({
deviceId: 'testDeviceId'
}));

itShouldCatchErrorsOnPhase(['onBeforeLaunchApp', 'onBeforeRelaunchApp'], () => ({
bundleId: 'testBundleId',
deviceId: 'testDeviceId',
}));

itShouldCatchErrorsOnPhase(['onLaunchApp', 'onRelaunchApp'], () => ({
bundleId: 'testBundleId',
deviceId: 'testDeviceId',
pid: 2018,
}));
});

describe('onBeforeAll', () => {
it('should call onBeforeAll in plugins', async () => {
expect(testPlugin.onBeforeAll).not.toHaveBeenCalled();
Expand Down
162 changes: 161 additions & 1 deletion detox/src/artifacts/__snapshots__/ArtifactsManager.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,167 @@ Array [
Array [
"ArtifactsManager",
"",
[Error],
[Error: test onIdleCallback error],
],
]
`;

exports[`ArtifactsManager .artifactsApi .requestIdleCallback() should gracefully handle a case when plugin object is not passed and use "unknown" as a name placeholder 1`] = `
Array [
Array [
"ArtifactsManager",
"Caught exception inside plugin (%s) at phase %s",
"unknown",
"onIdleCallback",
],
Array [
"ArtifactsManager",
"",
[Error: test onIdleCallback error],
],
]
`;

exports[`ArtifactsManager .artifactsApi hooks error handling should catch .onAfterAll errors 1`] = `
Array [
Array [
"ArtifactsManager",
"Caught exception inside plugin (%s) at phase %s",
"testPlugin",
"onAfterAll",
],
Array [
"ArtifactsManager",
"",
[Error: test onAfterAll error],
],
]
`;

exports[`ArtifactsManager .artifactsApi hooks error handling should catch .onAfterEach errors 1`] = `
Array [
Array [
"ArtifactsManager",
"Caught exception inside plugin (%s) at phase %s",
"testPlugin",
"onAfterEach",
],
Array [
"ArtifactsManager",
"",
[Error: test onAfterEach error],
],
]
`;

exports[`ArtifactsManager .artifactsApi hooks error handling should catch .onBeforeAll errors 1`] = `
Array [
Array [
"ArtifactsManager",
"Caught exception inside plugin (%s) at phase %s",
"testPlugin",
"onBeforeAll",
],
Array [
"ArtifactsManager",
"",
[Error: test onBeforeAll error],
],
]
`;

exports[`ArtifactsManager .artifactsApi hooks error handling should catch .onBeforeEach errors 1`] = `
Array [
Array [
"ArtifactsManager",
"Caught exception inside plugin (%s) at phase %s",
"testPlugin",
"onBeforeEach",
],
Array [
"ArtifactsManager",
"",
[Error: test onBeforeEach error],
],
]
`;

exports[`ArtifactsManager .artifactsApi hooks error handling should catch .onBeforeRelaunchApp errors 1`] = `
Array [
Array [
"ArtifactsManager",
"Caught exception inside plugin (%s) at phase %s",
"testPlugin",
"onBeforeRelaunchApp",
],
Array [
"ArtifactsManager",
"",
[Error: test onBeforeRelaunchApp error],
],
]
`;

exports[`ArtifactsManager .artifactsApi hooks error handling should catch .onBeforeResetDevice errors 1`] = `
Array [
Array [
"ArtifactsManager",
"Caught exception inside plugin (%s) at phase %s",
"testPlugin",
"onBeforeResetDevice",
],
Array [
"ArtifactsManager",
"",
[Error: test onBeforeResetDevice error],
],
]
`;

exports[`ArtifactsManager .artifactsApi hooks error handling should catch .onRelaunchApp errors 1`] = `
Array [
Array [
"ArtifactsManager",
"Caught exception inside plugin (%s) at phase %s",
"testPlugin",
"onRelaunchApp",
],
Array [
"ArtifactsManager",
"",
[Error: test onRelaunchApp error],
],
]
`;

exports[`ArtifactsManager .artifactsApi hooks error handling should catch .onResetDevice errors 1`] = `
Array [
Array [
"ArtifactsManager",
"Caught exception inside plugin (%s) at phase %s",
"testPlugin",
"onResetDevice",
],
Array [
"ArtifactsManager",
"",
[Error: test onResetDevice error],
],
]
`;

exports[`ArtifactsManager .artifactsApi hooks error handling should catch .onTerminate errors 1`] = `
Array [
Array [
"ArtifactsManager",
"Caught exception inside plugin (%s) at phase %s",
"testPlugin",
"onTerminate",
],
Array [
"ArtifactsManager",
"",
[Error: test onTerminate error],
],
]
`;
Expand Down

0 comments on commit d00ef63

Please sign in to comment.