Skip to content

Commit

Permalink
test(unit): fix XCUITestRunner tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
asafkorem committed May 2, 2024
1 parent ef56373 commit 37a0ee4
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions detox/src/ios/XCUITestRunner.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
const XCUITestRunner = require('./XCUITestRunner');

jest.mock('./childProcess/exec');
jest.mock('./environment');
jest.mock('child-process-promise', () => {
return {
exec: jest.fn(),
};
});

const { exec } = jest.requireMock('child-process-promise');
const environment = jest.requireMock('../utils/environment');

const { execWithRetriesAndLogs } = jest.requireMock('./childProcess/exec');
const environment = jest.requireMock('./environment');
jest.mock('../utils/environment');

describe('XCUITestRunner', () => {
const simulatorId = 'simulator-id';
Expand All @@ -15,18 +20,16 @@ describe('XCUITestRunner', () => {

beforeEach(() => {
environment.getXCUITestRunnerPath.mockResolvedValue(runnerPath);
exec.mockClear();
});

it('should execute XCUITest runner with given invocation params', async () => {
await runner.execute(invocationParams);

expect(execWithRetriesAndLogs).toHaveBeenCalledWith(
`TEST_RUNNER_PARAMS="${base64InvocationParams}" xcodebuild`,
expect.objectContaining({
args: expect.stringContaining(`-xctestrun ${runnerPath}`),
}
)
);
const command = `TEST_RUNNER_PARAMS="${base64InvocationParams}" xcodebuild -xctestrun ${runnerPath} -sdk iphonesimulator -destination "platform=iOS Simulator,id=${simulatorId}" test-without-building`;
exec.mockResolvedValue({ stdout: 'success' });

await runner.execute(invocationParams);

expect(exec).toHaveBeenCalledWith(command);
});

it('should throw error when runner path is not found', async () => {
Expand All @@ -37,18 +40,16 @@ describe('XCUITestRunner', () => {

it('should handle execution errors and throw error with an extracted inner error', async () => {
const errorOutput = 'DTXError: Test failure';
const execError = { stdout: Buffer.from(errorOutput) };
execWithRetriesAndLogs.mockRejectedValue(execError);
exec.mockRejectedValue({ stdout: Buffer.from(errorOutput) });

await expect(runner.execute(invocationParams)).rejects.toThrow(/Test failure/);
});

it('should handle execution errors with no specific error message', async () => {
const errorOutput = 'Unknown error occurred';
const execError = { stdout: Buffer.from(errorOutput) };
execWithRetriesAndLogs.mockRejectedValue(execError);
exec.mockRejectedValue({ stdout: Buffer.from(errorOutput) });

await expect(runner.execute(invocationParams)).rejects
.toThrow(/XCUITest runner failed with no error message. Runner stdout: Unknown error occurred/);
.toThrow(/XCUITest runner failed with no error message. Runner stdout: Unknown error occurred/);
});
});

0 comments on commit 37a0ee4

Please sign in to comment.