|
1 | | -/** |
2 | | - * Unit tests for the action's main functionality, src/main.ts |
3 | | - * |
4 | | - * To mock dependencies in ESM, you can create fixtures that export mock |
5 | | - * functions and objects. For example, the core module is mocked in this test, |
6 | | - * so that the actual '@actions/core' module is not imported. |
7 | | - */ |
8 | | -import { jest } from '@jest/globals' |
9 | | -import * as core from '../__fixtures__/core.js' |
10 | | -import { wait } from '../__fixtures__/wait.js' |
11 | | - |
12 | | -// Mocks should be declared before the module being tested is imported. |
13 | | -jest.unstable_mockModule('@actions/core', () => core) |
14 | | -jest.unstable_mockModule('../src/wait.js', () => ({ wait })) |
15 | | - |
16 | | -// The module being tested should be imported dynamically. This ensures that the |
17 | | -// mocks are used in place of any actual dependencies. |
18 | | -const { run } = await import('../src/main.js') |
19 | | - |
20 | | -describe('main.ts', () => { |
21 | | - beforeEach(() => { |
22 | | - // Set the action's inputs as return values from core.getInput(). |
23 | | - core.getInput.mockImplementation(() => '500') |
24 | | - |
25 | | - // Mock the wait function so that it does not actually wait. |
26 | | - wait.mockImplementation(() => Promise.resolve('done!')) |
27 | | - }) |
28 | | - |
29 | | - afterEach(() => { |
30 | | - jest.resetAllMocks() |
31 | | - }) |
32 | | - |
33 | | - it('Sets the time output', async () => { |
34 | | - await run() |
35 | | - |
36 | | - // Verify the time output was set. |
37 | | - expect(core.setOutput).toHaveBeenNthCalledWith( |
38 | | - 1, |
39 | | - 'time', |
40 | | - // Simple regex to match a time string in the format HH:MM:SS. |
41 | | - expect.stringMatching(/^\d{2}:\d{2}:\d{2}/) |
42 | | - ) |
43 | | - }) |
44 | | - |
45 | | - it('Sets a failed status', async () => { |
46 | | - // Clear the getInput mock and return an invalid value. |
47 | | - core.getInput.mockClear().mockReturnValueOnce('this is not a number') |
48 | | - |
49 | | - // Clear the wait mock and return a rejected promise. |
50 | | - wait |
51 | | - .mockClear() |
52 | | - .mockRejectedValueOnce(new Error('milliseconds is not a number')) |
53 | | - |
54 | | - await run() |
| 1 | +import * as process from 'process' |
| 2 | +import * as cp from 'child_process' |
| 3 | +import * as path from 'path' |
| 4 | +import {expect, test} from '@jest/globals' |
| 5 | + |
| 6 | +// shows how the runner will run a javascript action with env / stdout protocol |
| 7 | +test('test runs', () => { |
| 8 | + process.env['INPUT_REPORT-PATH'] = path.join(__dirname, 'resource', '*.xml') |
| 9 | + process.env['INPUT_FOLLOW-SYMBOLIC-LINKS'] = 'true' |
| 10 | + process.env['INPUT_IGNORE-WARNINGS'] = 'false' |
| 11 | + const np = process.execPath |
| 12 | + const ip = path.join(__dirname, '..', 'lib', 'main.js') |
| 13 | + const options: cp.ExecFileSyncOptions = { |
| 14 | + env: process.env |
| 15 | + } |
| 16 | + |
| 17 | + try { |
| 18 | + const stdout = cp.execFileSync(np, [ip], options) |
| 19 | + console.log(stdout.toString()) |
| 20 | + expect.assertions(1) |
| 21 | + } catch (error: any) { |
| 22 | + console.log(error.stdout.toString()) |
| 23 | + expect(error.status).toEqual(1) |
| 24 | + } |
| 25 | +}) |
55 | 26 |
|
56 | | - // Verify that the action was marked as failed. |
57 | | - expect(core.setFailed).toHaveBeenNthCalledWith( |
58 | | - 1, |
59 | | - 'milliseconds is not a number' |
60 | | - ) |
61 | | - }) |
| 27 | +test('test runs without error', () => { |
| 28 | + process.env['INPUT_REPORT-PATH'] = path.join( |
| 29 | + __dirname, |
| 30 | + 'resource', |
| 31 | + 'empty-results.xml' |
| 32 | + ) |
| 33 | + process.env['INPUT_FOLLOW-SYMBOLIC-LINKS'] = 'true' |
| 34 | + process.env['INPUT_IGNORE-WARNINGS'] = 'false' |
| 35 | + const np = process.execPath |
| 36 | + const ip = path.join(__dirname, '..', 'lib', 'main.js') |
| 37 | + const options: cp.ExecFileSyncOptions = { |
| 38 | + env: process.env |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + const stdout = cp.execFileSync(np, [ip], options) |
| 43 | + console.log(stdout.toString()) |
| 44 | + } catch (error: any) { |
| 45 | + console.log(error.status) |
| 46 | + console.log(error.stdout.toString()) |
| 47 | + expect.assertions(1) |
| 48 | + } |
62 | 49 | }) |
0 commit comments