|
1 | 1 | /** |
2 | 2 | * Unit tests for the action's main functionality, src/main.js |
3 | 3 | */ |
4 | | -const core = require('@actions/core') |
5 | | -const main = require('../src/main') |
| 4 | +const core = require('@actions/core'); |
| 5 | +const main = require('../src/main'); |
6 | 6 |
|
7 | 7 | // Mock the GitHub Actions core library |
8 | | -const debugMock = jest.spyOn(core, 'debug').mockImplementation() |
9 | | -const getInputMock = jest.spyOn(core, 'getInput').mockImplementation() |
10 | | -const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation() |
11 | | -const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation() |
| 8 | +const debugMock = jest.spyOn(core, 'debug').mockImplementation(); |
| 9 | +const getInputMock = jest.spyOn(core, 'getInput').mockImplementation(); |
| 10 | +const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation(); |
| 11 | +const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation(); |
12 | 12 |
|
13 | 13 | // Mock the action's main function |
14 | | -const runMock = jest.spyOn(main, 'run') |
| 14 | +const runMock = jest.spyOn(main, 'run'); |
15 | 15 |
|
16 | 16 | // Other utilities |
17 | | -const timeRegex = /^\d{2}:\d{2}:\d{2}/ |
| 17 | +const timeRegex = /^\d{2}:\d{2}:\d{2}/; |
18 | 18 |
|
19 | 19 | describe('action', () => { |
20 | | - beforeEach(() => { |
21 | | - jest.clearAllMocks() |
22 | | - }) |
| 20 | + beforeEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + }); |
23 | 23 |
|
24 | | - it('sets the time output', async () => { |
25 | | - // Set the action's inputs as return values from core.getInput() |
26 | | - getInputMock.mockImplementation(name => { |
27 | | - switch (name) { |
28 | | - case 'milliseconds': |
29 | | - return '500' |
30 | | - default: |
31 | | - return '' |
32 | | - } |
33 | | - }) |
| 24 | + it('sets the time output', async () => { |
| 25 | + // Set the action's inputs as return values from core.getInput() |
| 26 | + getInputMock.mockImplementation(name => { |
| 27 | + switch (name) { |
| 28 | + case 'milliseconds': |
| 29 | + return '500'; |
| 30 | + default: |
| 31 | + return ''; |
| 32 | + } |
| 33 | + }); |
34 | 34 |
|
35 | | - await main.run() |
36 | | - expect(runMock).toHaveReturned() |
| 35 | + await main.run(); |
| 36 | + expect(runMock).toHaveReturned(); |
37 | 37 |
|
38 | | - // Verify that all of the core library functions were called correctly |
39 | | - expect(debugMock).toHaveBeenNthCalledWith(1, 'Waiting 500 milliseconds ...') |
40 | | - expect(debugMock).toHaveBeenNthCalledWith( |
41 | | - 2, |
42 | | - expect.stringMatching(timeRegex) |
43 | | - ) |
44 | | - expect(debugMock).toHaveBeenNthCalledWith( |
45 | | - 3, |
46 | | - expect.stringMatching(timeRegex) |
47 | | - ) |
48 | | - expect(setOutputMock).toHaveBeenNthCalledWith( |
49 | | - 1, |
50 | | - 'time', |
51 | | - expect.stringMatching(timeRegex) |
52 | | - ) |
53 | | - }) |
| 38 | + // Verify that all of the core library functions were called correctly |
| 39 | + expect(debugMock).toHaveBeenNthCalledWith( |
| 40 | + 1, |
| 41 | + 'Waiting 500 milliseconds ...' |
| 42 | + ); |
| 43 | + expect(debugMock).toHaveBeenNthCalledWith( |
| 44 | + 2, |
| 45 | + expect.stringMatching(timeRegex) |
| 46 | + ); |
| 47 | + expect(debugMock).toHaveBeenNthCalledWith( |
| 48 | + 3, |
| 49 | + expect.stringMatching(timeRegex) |
| 50 | + ); |
| 51 | + expect(setOutputMock).toHaveBeenNthCalledWith( |
| 52 | + 1, |
| 53 | + 'time', |
| 54 | + expect.stringMatching(timeRegex) |
| 55 | + ); |
| 56 | + }); |
54 | 57 |
|
55 | | - it('sets a failed status', async () => { |
56 | | - // Set the action's inputs as return values from core.getInput() |
57 | | - getInputMock.mockImplementation(name => { |
58 | | - switch (name) { |
59 | | - case 'milliseconds': |
60 | | - return 'this is not a number' |
61 | | - default: |
62 | | - return '' |
63 | | - } |
64 | | - }) |
| 58 | + it('sets a failed status', async () => { |
| 59 | + // Set the action's inputs as return values from core.getInput() |
| 60 | + getInputMock.mockImplementation(name => { |
| 61 | + switch (name) { |
| 62 | + case 'milliseconds': |
| 63 | + return 'this is not a number'; |
| 64 | + default: |
| 65 | + return ''; |
| 66 | + } |
| 67 | + }); |
65 | 68 |
|
66 | | - await main.run() |
67 | | - expect(runMock).toHaveReturned() |
| 69 | + await main.run(); |
| 70 | + expect(runMock).toHaveReturned(); |
68 | 71 |
|
69 | | - // Verify that all of the core library functions were called correctly |
70 | | - expect(setFailedMock).toHaveBeenNthCalledWith( |
71 | | - 1, |
72 | | - 'milliseconds not a number' |
73 | | - ) |
74 | | - }) |
| 72 | + // Verify that all of the core library functions were called correctly |
| 73 | + expect(setFailedMock).toHaveBeenNthCalledWith( |
| 74 | + 1, |
| 75 | + 'milliseconds not a number' |
| 76 | + ); |
| 77 | + }); |
75 | 78 |
|
76 | | - it('fails if no input is provided', async () => { |
77 | | - // Set the action's inputs as return values from core.getInput() |
78 | | - getInputMock.mockImplementation(name => { |
79 | | - switch (name) { |
80 | | - case 'milliseconds': |
81 | | - throw new Error('Input required and not supplied: milliseconds') |
82 | | - default: |
83 | | - return '' |
84 | | - } |
85 | | - }) |
| 79 | + it('fails if no input is provided', async () => { |
| 80 | + // Set the action's inputs as return values from core.getInput() |
| 81 | + getInputMock.mockImplementation(name => { |
| 82 | + switch (name) { |
| 83 | + case 'milliseconds': |
| 84 | + throw new Error( |
| 85 | + 'Input required and not supplied: milliseconds' |
| 86 | + ); |
| 87 | + default: |
| 88 | + return ''; |
| 89 | + } |
| 90 | + }); |
86 | 91 |
|
87 | | - await main.run() |
88 | | - expect(runMock).toHaveReturned() |
| 92 | + await main.run(); |
| 93 | + expect(runMock).toHaveReturned(); |
89 | 94 |
|
90 | | - // Verify that all of the core library functions were called correctly |
91 | | - expect(setFailedMock).toHaveBeenNthCalledWith( |
92 | | - 1, |
93 | | - 'Input required and not supplied: milliseconds' |
94 | | - ) |
95 | | - }) |
96 | | -}) |
| 95 | + // Verify that all of the core library functions were called correctly |
| 96 | + expect(setFailedMock).toHaveBeenNthCalledWith( |
| 97 | + 1, |
| 98 | + 'Input required and not supplied: milliseconds' |
| 99 | + ); |
| 100 | + }); |
| 101 | +}); |
0 commit comments