Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to ESM #434

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

4 changes: 4 additions & 0 deletions .github/workflows/check-dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ jobs:
runs-on: ubuntu-latest

steps:
# Checkout the repository.
- name: Checkout
id: checkout
uses: actions/checkout@v4

# Setup Node.js using the version specified in `.node-version`.
- name: Setup Node.js
id: setup-node
uses: actions/setup-node@v4
with:
node-version-file: .node-version
cache: npm

# Install dependencies using `npm ci`.
- name: Install Dependencies
id: install
run: npm ci

# Build the `dist/` directory.
- name: Build dist/ Directory
id: build
run: npm run bundle
Expand Down
14 changes: 13 additions & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# This workflow will lint the entire codebase using the
# `super-linter/super-linter` action.
#
# For more information, see the super-linter repository:
# https://github.com/super-linter/super-linter
name: Lint Codebase

on:
Expand All @@ -19,30 +24,37 @@ jobs:
runs-on: ubuntu-latest

steps:
# Checkout the repository.
- name: Checkout
id: checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

# Setup Node.js using the version specified in `.node-version`.
- name: Setup Node.js
id: setup-node
uses: actions/setup-node@v4
with:
node-version-file: .node-version
cache: npm

# Install dependencies using `npm ci`.
- name: Install Dependencies
id: install
run: npm ci

# Lint the codebase using the `super-linter/super-linter` action.
- name: Lint Codebase
id: super-linter
uses: super-linter/super-linter/slim@v7
env:
DEFAULT_BRANCH: main
FILTER_REGEX_EXCLUDE: dist/**/*
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VALIDATE_JAVASCRIPT_STANDARD: false
LINTER_RULES_PATH: ${{ github.workspace }}
VALIDATE_ALL_CODEBASE: true
VALIDATE_JAVASCRIPT_ES: false
VALIDATE_JAVASCRIPT_STANDARD: false
VALIDATE_JSCPD: false
VALIDATE_JSON: false
24 changes: 24 additions & 0 deletions .markdown-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# See: https://github.com/DavidAnson/markdownlint

# Unordered list style
MD004:
style: dash

# Disable line length for tables
MD013:
tables: false

# Ordered list item prefix
MD029:
style: one

# Spaces after list markers
MD030:
ul_single: 1
ol_single: 1
ul_multi: 1
ol_multi: 1

# Code block style
MD046:
style: fenced
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.6.0
22.9.0
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
dist/
node_modules/
coverage/
coverage/
16 changes: 0 additions & 16 deletions .prettierrc.json

This file was deleted.

16 changes: 16 additions & 0 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# See: https://prettier.io/docs/en/configuration

printWidth: 80
tabWidth: 2
useTabs: false
semi: false
singleQuote: true
quoteProps: as-needed
jsxSingleQuote: false
trailingComma: none
bracketSpacing: true
bracketSameLine: true
arrowParens: always
proseWrap: always
htmlWhitespaceSensitivity: css
endOfLine: lf
12 changes: 12 additions & 0 deletions .yaml-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# See: https://yamllint.readthedocs.io/en/stable/

rules:
document-end: disable
document-start:
level: warning
present: false
line-length:
level: warning
max: 80
allow-non-breakable-words: true
allow-non-breakable-inline-mappings: true
6 changes: 5 additions & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Repository CODEOWNERS
############################################################################
# Repository CODEOWNERS #
# Order is important! The last matching pattern takes the most precedence. #
############################################################################

# Default owners, unless a later match takes precedence.
* @actions/actions-oss-maintainers
12 changes: 12 additions & 0 deletions __fixtures__/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* This file is used to mock the `@actions/core` module in tests.
*/
import { jest } from '@jest/globals'

export const debug = jest.fn()
export const error = jest.fn()
export const info = jest.fn()
export const getInput = jest.fn()
export const setOutput = jest.fn()
export const setFailed = jest.fn()
export const warning = jest.fn()
3 changes: 3 additions & 0 deletions __fixtures__/wait.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { jest } from '@jest/globals'

export const wait = jest.fn()
18 changes: 0 additions & 18 deletions __tests__/index.test.js

This file was deleted.

112 changes: 39 additions & 73 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,62 @@
/**
* Unit tests for the action's main functionality, src/main.js
*
* To mock dependencies in ESM, you can create fixtures that export mock
* functions and objects. For example, the core module is mocked in this test,
* so that the actual '@actions/core' module is not imported.
*/
const core = require('@actions/core')
const main = require('../src/main')
import { jest } from '@jest/globals'
import * as core from '../__fixtures__/core.js'
import { wait } from '../__fixtures__/wait.js'

// Mock the GitHub Actions core library
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()
// Mocks should be declared before the module being tested is imported.
jest.unstable_mockModule('@actions/core', () => core)
jest.unstable_mockModule('../src/wait.js', () => ({ wait }))

// Mock the action's main function
const runMock = jest.spyOn(main, 'run')
// The module being tested should be imported dynamically. This ensures that the
// mocks are used in place of any actual dependencies.
const { run } = await import('../src/main.js')

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

describe('action', () => {
describe('main.js', () => {
beforeEach(() => {
jest.clearAllMocks()
// Set the action's inputs as return values from core.getInput().
core.getInput.mockImplementation(() => '500')

// Mock the wait function so that it does not actually wait.
wait.mockImplementation(() => Promise.resolve('done!'))
})

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'
default:
return ''
}
})
afterEach(() => {
jest.resetAllMocks()
})

await main.run()
expect(runMock).toHaveReturned()
it('Sets the time output', async () => {
await run()

// 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(
// Verify the time output was set.
expect(core.setOutput).toHaveBeenNthCalledWith(
1,
'time',
expect.stringMatching(timeRegex)
// Simple regex to match a time string in the format HH:MM:SS.
expect.stringMatching(/^\d{2}:\d{2}:\d{2}/)
)
})

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'
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'
)
})
it('Sets a failed status', async () => {
// Clear the getInput mock and return an invalid value.
core.getInput.mockClear().mockReturnValueOnce('this is not a number')

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':
throw new Error('Input required and not supplied: milliseconds')
default:
return ''
}
})
// Clear the wait mock and return a rejected promise.
wait
.mockClear()
.mockRejectedValueOnce(new Error('milliseconds is not a number'))

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

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(
// Verify that the action was marked as failed.
expect(core.setFailed).toHaveBeenNthCalledWith(
1,
'Input required and not supplied: milliseconds'
'milliseconds is not a number'
)
})
})
10 changes: 5 additions & 5 deletions __tests__/wait.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* Unit tests for src/wait.js
*/
const { wait } = require('../src/wait')
const { expect } = require('@jest/globals')
import { wait } from '../src/wait.js'

describe('wait.js', () => {
it('throws an invalid number', async () => {
it('Throws an invalid number', async () => {
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 is not a number')
})

it('waits with a valid number', async () => {
it('Waits with a valid number', async () => {
const start = new Date()
await wait(500)
const end = new Date()
Expand Down
15 changes: 10 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
name: 'The name of your action here'
description: 'Provide a description here'
author: 'Your name or organization here'
name: The name of your action here
description: Provide a description here
author: Your name or organization here

# Add your action's branding here. This will appear on the GitHub Marketplace.
branding:
icon: heart
color: red

# Define your inputs here.
inputs:
milliseconds:
description: 'Your input description here'
description: Your input description here
required: true
default: '1000'

# Define your outputs here.
outputs:
time:
description: 'Your output description here'
description: Your output description here

runs:
using: node20
Expand Down
Loading