Skip to content

Commit

Permalink
Second
Browse files Browse the repository at this point in the history
  • Loading branch information
crazywhalecc committed Aug 23, 2024
1 parent e60dfc4 commit a119da9
Show file tree
Hide file tree
Showing 12 changed files with 1,532 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .github/linters/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ rules:
'@typescript-eslint/promise-function-async': 'error',
'@typescript-eslint/require-array-sort-compare': 'error',
'@typescript-eslint/restrict-plus-operands': 'error',
'@typescript-eslint/semi': ['error', 'never'],
'@typescript-eslint/semi': 'off',
'@typescript-eslint/space-before-function-paren': 'off',
'@typescript-eslint/type-annotation-spacing': 'error',
'@typescript-eslint/unbound-method': 'error'
Expand Down
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"semi": true,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
Expand Down
12 changes: 6 additions & 6 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
* Unit tests for the action's entrypoint, src/index.ts
*/

import * as main from '../src/main'
import * as main from '../src/main';

// Mock the action's entrypoint
const runMock = jest.spyOn(main, 'run').mockImplementation()
const runMock = jest.spyOn(main, 'run').mockImplementation();

describe('index', () => {
it('calls run when imported', async () => {
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('../src/index')
require('../src/index');

expect(runMock).toHaveBeenCalled()
})
})
expect(runMock).toHaveBeenCalled();
});
});
75 changes: 39 additions & 36 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,84 +6,87 @@
* variables following the pattern `INPUT_<INPUT_NAME>`.
*/

import * as core from '@actions/core'
import * as main from '../src/main'
import * as core from '@actions/core';
import * as main from '../src/main';

// Mock the action's main function
const runMock = jest.spyOn(main, 'run')
const runMock = jest.spyOn(main, 'run');

// Other utilities
const timeRegex = /^\d{2}:\d{2}:\d{2}/
const timeRegex = /^\d{2}:\d{2}:\d{2}/;

// Mock the GitHub Actions core library
let debugMock: jest.SpiedFunction<typeof core.debug>
let errorMock: jest.SpiedFunction<typeof core.error>
let getInputMock: jest.SpiedFunction<typeof core.getInput>
let setFailedMock: jest.SpiedFunction<typeof core.setFailed>
let setOutputMock: jest.SpiedFunction<typeof core.setOutput>
let debugMock: jest.SpiedFunction<typeof core.debug>;
let errorMock: jest.SpiedFunction<typeof core.error>;
let getInputMock: jest.SpiedFunction<typeof core.getInput>;
let setFailedMock: jest.SpiedFunction<typeof core.setFailed>;
let setOutputMock: jest.SpiedFunction<typeof core.setOutput>;

describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()
jest.clearAllMocks();

debugMock = jest.spyOn(core, 'debug').mockImplementation()
errorMock = jest.spyOn(core, 'error').mockImplementation()
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
})
debugMock = jest.spyOn(core, 'debug').mockImplementation();
errorMock = jest.spyOn(core, 'error').mockImplementation();
getInputMock = jest.spyOn(core, 'getInput').mockImplementation();
setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation();
setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation();
});

it('sets the time output', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock.mockImplementation(name => {
switch (name) {
case 'milliseconds':
return '500'
return '500';
default:
return ''
return '';
}
})
});

await main.run()
expect(runMock).toHaveReturned()
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(
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(errorMock).not.toHaveBeenCalled()
})
);
expect(errorMock).not.toHaveBeenCalled();
});

it('sets a failed status', 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'
return 'this is not a number';
default:
return ''
return '';
}
})
});

await main.run()
expect(runMock).toHaveReturned()
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(errorMock).not.toHaveBeenCalled()
})
})
);
expect(errorMock).not.toHaveBeenCalled();
});
});
26 changes: 13 additions & 13 deletions __tests__/wait.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
* Unit tests for src/wait.ts
*/

import { wait } from '../src/wait'
import { expect } from '@jest/globals'
import { wait } from '../src/wait';
import { expect } from '@jest/globals';

describe('wait.ts', () => {
it('throws an invalid number', async () => {
const input = parseInt('foo', 10)
expect(isNaN(input)).toBe(true)
const input = parseInt('foo', 10);
expect(isNaN(input)).toBe(true);

await expect(wait(input)).rejects.toThrow('milliseconds not a number')
})
await expect(wait(input)).rejects.toThrow('milliseconds not a number');
});

it('waits with a valid number', async () => {
const start = new Date()
await wait(500)
const end = new Date()
const start = new Date();
await wait(500);
const end = new Date();

const delta = Math.abs(end.getTime() - start.getTime())
const delta = Math.abs(end.getTime() - start.getTime());

expect(delta).toBeGreaterThan(450)
})
})
expect(delta).toBeGreaterThan(450);
});
});
4 changes: 2 additions & 2 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit a119da9

Please sign in to comment.