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

test: versionInfo #1407

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import * as core from '@actions/core';
import {Agent, MockAgent, setGlobalDispatcher} from 'undici';
import versionInfo from './version';

const mockAgent = new MockAgent();

beforeAll(() => {
setGlobalDispatcher(mockAgent);
mockAgent.disableNetConnect();
});

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

afterAll(async () => {
await mockAgent.close();
setGlobalDispatcher(new Agent());
});

describe('versionInfo', () => {
const platform = 'linux';

test('should resolve requested version info', async () => {
const version = 'latest';
const coreInfoSpy = jest.spyOn(core, 'info');

mockAgent
.get('https://cli.codecov.io')
.intercept({
path: `/${platform}/${version}`,
})
.reply(200, {
version: 'v0.5.2',
});

await versionInfo(platform, version);

expect(coreInfoSpy).toHaveBeenCalledTimes(2);
expect(coreInfoSpy).toHaveBeenCalledWith('==> Running version latest');
expect(coreInfoSpy).toHaveBeenCalledWith('==> Running version v0.5.2');
});

test('should handle unsupported version', async () => {
const version = 'unsupported';
const coreInfoSpy = jest.spyOn(core, 'info');

mockAgent
.get('https://cli.codecov.io')
.intercept({
path: `/${platform}/${version}`,
})
.reply(404, 'MESSAGE');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any arbitrary message would work as the service returns XML, which can not be parsed by the .json() method


await versionInfo(platform, version);

expect(coreInfoSpy).toHaveBeenCalledTimes(2);
expect(coreInfoSpy).toHaveBeenCalledWith('==> Running version unsupported');
expect(coreInfoSpy).toHaveBeenCalledWith(
'Could not pull latest version information: SyntaxError: Unexpected token \'M\', "MESSAGE" is not valid JSON',
);
});
});