generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
671de06
commit 90a0647
Showing
12 changed files
with
15,114 additions
and
963 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,125 @@ | ||
/** | ||
* Unit tests for the action's entrypoint, src/index.ts | ||
*/ | ||
// index.test.js | ||
const core = require('@actions/core') | ||
const fetch = require('isomorphic-fetch') | ||
const fs = require('fs') | ||
const { parseStringPromise } = require('xml2js') | ||
const { fetchRssFeed } = require('../src/index') | ||
|
||
const { run } = require('../src/main') | ||
|
||
// Mock the action's entrypoint | ||
jest.mock('../src/main', () => ({ | ||
run: jest.fn() | ||
jest.mock('isomorphic-fetch') | ||
jest.mock('fs', () => ({ | ||
accessSync: jest.fn(), | ||
writeFileSync: jest.fn(), | ||
constants: { W_OK: 'some_value' }, | ||
promises: { | ||
access: jest.fn() | ||
} | ||
})) | ||
|
||
describe('index', () => { | ||
it('calls run when imported', async () => { | ||
require('../src/index') | ||
jest.mock('xml2js') | ||
jest.mock('@actions/core') | ||
|
||
describe('fetchRssFeed', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('should set failure if no feed URL is provided', async () => { | ||
process.env.INPUT_FEED_URL = '' | ||
await fetchRssFeed() | ||
expect(core.setFailed).toHaveBeenCalledWith('Feed URL is not provided') | ||
}) | ||
|
||
it('should set failure if fetch fails', async () => { | ||
expect.assertions(1) | ||
process.env.INPUT_FEED_URL = 'http://example.com/feed' | ||
fetch.mockRejectedValue(new Error('Fetch failed')) | ||
|
||
await fetchRssFeed() | ||
|
||
expect(core.setFailed).toHaveBeenCalledWith('Fetch failed') | ||
}) | ||
|
||
it('should save the feed to a file if file path is provided', async () => { | ||
process.env.INPUT_FEED_URL = 'http://example.com/feed' | ||
process.env.INPUT_FILE_PATH = './feed.json' | ||
fetch.mockResolvedValue({ | ||
ok: true, | ||
text: jest.fn().mockResolvedValue('<rss></rss>') | ||
}) | ||
parseStringPromise.mockResolvedValue({ rss: {} }) | ||
fs.accessSync.mockReturnValue(true) | ||
|
||
await fetchRssFeed() | ||
|
||
expect(fs.writeFileSync).toHaveBeenCalledWith( | ||
'./feed.json', | ||
JSON.stringify({ rss: {} }, null, 2) | ||
) | ||
expect(core.setOutput).toHaveBeenCalledWith( | ||
'feed', | ||
JSON.stringify({ rss: {} }, null, 2) | ||
) | ||
}) | ||
|
||
it('should not save the feed to a file if no file path is provided', async () => { | ||
process.env.INPUT_FEED_URL = 'http://example.com/feed' | ||
process.env.INPUT_FILE_PATH = '' | ||
fetch.mockResolvedValue({ | ||
ok: true, | ||
text: jest.fn().mockResolvedValue('<rss></rss>') | ||
}) | ||
parseStringPromise.mockResolvedValue({ rss: {} }) | ||
|
||
await fetchRssFeed() | ||
|
||
expect(fs.writeFileSync).not.toHaveBeenCalled() | ||
expect(core.setOutput).toHaveBeenCalledWith( | ||
'feed', | ||
JSON.stringify({ rss: {} }, null, 2) | ||
) | ||
}) | ||
}) | ||
|
||
// Verify that mocks work correctly | ||
describe('fetch mock', () => { | ||
it('should reject when mockRejectedValue is used', async () => { | ||
// Set up the mock to reject | ||
fetch.mockRejectedValue(new Error('Fetch failed')) | ||
|
||
// Expect that calling fetch will reject with the specified error | ||
await expect(fetch('some_url')).rejects.toThrow('Fetch failed') | ||
}) | ||
}) | ||
|
||
describe('fs mock', () => { | ||
it('should call accessSync without errors', () => { | ||
fs.accessSync.mockReturnValue(true) | ||
expect(() => fs.accessSync('some_path', fs.constants.W_OK)).not.toThrow() | ||
}) | ||
|
||
it('should call writeFileSync without errors', () => { | ||
fs.writeFileSync.mockReturnValue(true) | ||
expect(() => fs.writeFileSync('some_path', 'some_data')).not.toThrow() | ||
}) | ||
}) | ||
|
||
describe('xml2js mock', () => { | ||
it('should resolve parseStringPromise', async () => { | ||
parseStringPromise.mockResolvedValue({ rss: {} }) | ||
await expect(parseStringPromise('<rss></rss>')).resolves.toEqual({ | ||
rss: {} | ||
}) | ||
}) | ||
}) | ||
|
||
describe('@actions/core mock', () => { | ||
it('should call setOutput without errors', () => { | ||
core.setOutput.mockReturnValue(true) | ||
expect(() => core.setOutput('key', 'value')).not.toThrow() | ||
}) | ||
|
||
expect(run).toHaveBeenCalled() | ||
it('should call setFailed without errors', () => { | ||
core.setFailed.mockReturnValue(true) | ||
expect(() => core.setFailed('Some error')).not.toThrow() | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
name: 'The name of your action here' | ||
description: 'Provide a description here' | ||
author: 'Your name or organization here' | ||
name: 'RSS Feed Fetch Action' | ||
description: 'Fetches an RSS feed and optionally saves it to a file' | ||
author: 'Christopher C. Smith <[email protected]>' | ||
|
||
# Define your inputs here. | ||
inputs: | ||
milliseconds: | ||
description: 'Your input description here' | ||
feed_url: | ||
description: 'The URL of the RSS feed to fetch' | ||
required: true | ||
default: '1000' | ||
file_path: | ||
description: 'The relative file path to save the fetched RSS feed' | ||
required: false | ||
|
||
# Define your outputs here. | ||
outputs: | ||
time: | ||
description: 'Your output description here' | ||
feed: | ||
description: 'The fetched RSS feed in JSON format' | ||
|
||
runs: | ||
using: node20 | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.