Skip to content

Commit

Permalink
Fix jest
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywhalecc committed Oct 8, 2024
1 parent 61e3bd6 commit 9da9e98
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 35 deletions.
45 changes: 11 additions & 34 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const core = require('@actions/core');
const main = require('../src/main');

// Mock the GitHub Actions core library
const debugMock = jest.spyOn(core, 'debug').mockImplementation();
// const debugMock = jest.spyOn(core, 'debug').mockImplementation();
const getInputMock = jest.spyOn(core, 'getInput').mockImplementation();
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation();
const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation();
Expand All @@ -25,64 +25,41 @@ describe('action', () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return '500';
case 'sapi':
return 'cli,fpm';
default:
return '';
}
});

await main.run();
expect(runMock).toHaveReturned();

// Verify that all of the core library functions were called correctly
expect(debugMock).toHaveBeenNthCalledWith(
1,
'Waiting 500 milliseconds ...'
);
expect(debugMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching(timeRegex)
);
expect(debugMock).toHaveBeenNthCalledWith(
3,
expect.stringMatching(timeRegex)
);
expect(setOutputMock).toHaveBeenNthCalledWith(
1,
'time',
expect.stringMatching(timeRegex)
);
expect(setOutputMock).toHaveBeenNthCalledWith(1, 'sapi', 'cli,fpm');
});

it('sets a failed status', async () => {
it('sets the wrong sapi name', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return 'this is not a number';
case 'sapi':
return 'foo';
default:
return '';
}
});

await main.run();
expect(runMock).toHaveReturned();

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'milliseconds not a number'
);
expect(setFailedMock).toHaveBeenNthCalledWith(1, 'Invalid sapi');
});

it('fails if no input is provided', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
case 'sapi':
throw new Error(
'Input required and not supplied: milliseconds'
'Need to input a valid sapi: cli, fpm, micro, embed'
);
default:
return '';
Expand All @@ -95,7 +72,7 @@ describe('action', () => {
// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
1,
'Input required and not supplied: milliseconds'
'Need to input a valid sapi: cli, fpm, micro, embed'
);
});
});
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ inputs:
sapi:
description: 'PHP SAPI'
required: true
default: 'cli'
default: 'cli,micro'

# Define your outputs here.
outputs:
Expand Down
4 changes: 4 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const core = require('@actions/core');
async function run() {
try {
const ms = core.getInput('sapi', { required: true });
// only accepts cli,fpm,micro,embed for now
if (!['cli', 'fpm', 'micro', 'embed'].includes(ms)) {
core.setFailed('Invalid sapi');
}

core.setOutput('sapi', ms);
core.debug(`Sapi: ${ms}`);
Expand Down

0 comments on commit 9da9e98

Please sign in to comment.