Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to main action method #4

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
175 changes: 91 additions & 84 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 70 additions & 0 deletions src/action.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
describe('compileAction', () => {

afterEach(() => {
jest.resetModules();
});

it('should use compile locally with docker by default', async () => {
const dockerCheckMock = jest.fn();
const dockerBuildpackCompileMock = jest.fn();
jest.mock('./docker', () => ({
dockerCheck: dockerCheckMock,
dockerBuildpackCompile: dockerBuildpackCompileMock
}));
const { compileAction } = await import('./action');
await compileAction();
expect(dockerCheckMock).toHaveBeenCalled();
expect(dockerBuildpackCompileMock).toHaveBeenCalled();
});

it('should use compile in the cloud if access token is provided', async () => {
// mock particle-access-token input
jest.mock('@actions/core', () => ({
getInput: jest.fn().mockImplementation((name: string) => {
if (name === 'particle-access-token') {
return 'test-token';
}
return '';
}),
setFailed: jest.fn(),
info: jest.fn(),
setOutput: jest.fn()
}));

const particleCloudCompileMock = jest.fn().mockResolvedValue('test-binary-id');
const particleDownloadBinaryMock = jest.fn().mockResolvedValue('test-path');
jest.mock('./particle-api', () => ({
particleCloudCompile: particleCloudCompileMock,
particleDownloadBinary: particleDownloadBinaryMock
}));
const { compileAction } = await import('./action');
await compileAction();
expect(particleCloudCompileMock).toHaveBeenCalled();
expect(particleDownloadBinaryMock).toHaveBeenCalled();
});

it('should throw an error if cloud compilation fails', async () => {
// mock particle-access-token input
const setFailedMock = jest.fn();
jest.mock('@actions/core', () => ({
getInput: jest.fn().mockImplementation((name: string) => {
if (name === 'particle-access-token') {
return 'test-token';
}
return '';
}),
setFailed: setFailedMock,
info: jest.fn(),
setOutput: jest.fn()
}));

const particleCloudCompileMock = jest.fn().mockResolvedValue(null);
jest.mock('./particle-api', () => ({
particleCloudCompile: particleCloudCompileMock
}));
const { compileAction } = await import('./action');
await compileAction();
expect(setFailedMock).toHaveBeenCalled();
expect(setFailedMock).toHaveBeenCalledWith('Failed to compile code in cloud');
});
});
Loading