Skip to content

Commit

Permalink
Merge pull request #969 from actions/ncalteen/esm
Browse files Browse the repository at this point in the history
Convert to ESM
  • Loading branch information
ncalteen authored Jan 10, 2025
2 parents d11ebba + aa4c2de commit b58a5b3
Show file tree
Hide file tree
Showing 35 changed files with 30,729 additions and 28,624 deletions.
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

64 changes: 0 additions & 64 deletions .github/linters/.eslintrc.yml

This file was deleted.

9 changes: 0 additions & 9 deletions .github/linters/tsconfig.json

This file was deleted.

4 changes: 4 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ jobs:
DEFAULT_BRANCH: main
FILTER_REGEX_EXCLUDE: dist/**/*
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
LINTER_RULES_PATH: ${{ github.workspace }}
VALIDATE_ALL_CODEBASE: true
VALIDATE_JAVASCRIPT_ES: false
VALIDATE_JAVASCRIPT_STANDARD: false
VALIDATE_JSCPD: false
VALIDATE_TYPESCRIPT_ES: false
VALIDATE_JSON: false
VALIDATE_TYPESCRIPT_STANDARD: false
6 changes: 6 additions & 0 deletions .github/linters/.markdown-lint.yml → .markdown-lint.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# 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
Expand Down
2 changes: 1 addition & 1 deletion .node-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21.6.2
20.9.0
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
dist/
node_modules/
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
2 changes: 2 additions & 0 deletions .github/linters/.yaml-lint.yml → .yaml-lint.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# See: https://yamllint.readthedocs.io/en/stable/

rules:
document-end: disable
document-start:
Expand Down
6 changes: 5 additions & 1 deletion CODEOWNERS
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
10 changes: 10 additions & 0 deletions __fixtures__/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type * as core from '@actions/core'
import { jest } from '@jest/globals'

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

export const wait = jest.fn<typeof import('../src/wait.js').wait>()
17 changes: 0 additions & 17 deletions __tests__/index.test.ts

This file was deleted.

103 changes: 38 additions & 65 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,62 @@
/**
* Unit tests for the action's main functionality, src/main.ts
*
* These should be run as if the action was called from a workflow.
* Specifically, the inputs listed in `action.yml` should be set as environment
* variables following the pattern `INPUT_<INPUT_NAME>`.
* 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.
*/
import { jest } from '@jest/globals'
import * as core from '../__fixtures__/core.js'
import { wait } from '../__fixtures__/wait.js'

import * as core from '@actions/core'
import * as main from '../src/main'
// 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}/

// 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>

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

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

// 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,
'milliseconds not a number'
'milliseconds is not a number'
)
expect(errorMock).not.toHaveBeenCalled()
})
})
11 changes: 5 additions & 6 deletions __tests__/wait.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/**
* Unit tests for src/wait.ts
*/

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

describe('wait.ts', () => {
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
14 changes: 7 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,23 +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'
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
1 change: 1 addition & 0 deletions dist/index.d.ts

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

Loading

0 comments on commit b58a5b3

Please sign in to comment.