From dcb7246e5fd282c2a08dd00027943d132ed5b673 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 2 Mar 2023 14:15:43 +0100 Subject: [PATCH] basic test for context --- __tests__/context.test.ts | 45 +++++++++++++++++++++++++++++++++++++++ jest.config.ts | 29 +++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 __tests__/context.test.ts create mode 100644 jest.config.ts diff --git a/__tests__/context.test.ts b/__tests__/context.test.ts new file mode 100644 index 0000000..bca0983 --- /dev/null +++ b/__tests__/context.test.ts @@ -0,0 +1,45 @@ +import {beforeEach, describe, expect, test} from '@jest/globals'; + +import * as context from '../src/context'; + +describe('getInputs', () => { + beforeEach(() => { + process.env = Object.keys(process.env).reduce((object, key) => { + if (!key.startsWith('INPUT_')) { + object[key] = process.env[key]; + } + return object; + }, {}); + }); + + // prettier-ignore + test.each([ + [ + 0, + new Map([ + ['version', '23.0.1'], + ]), + { + version: '23.0.1', + } as context.Inputs + ] + ])( + '[%d] given %p as inputs, returns %p', + async (num: number, inputs: Map, expected: context.Inputs) => { + inputs.forEach((value: string, name: string) => { + setInput(name, value); + }); + const res = await context.getInputs(); + expect(res).toEqual(expected); + } + ); +}); + +// See: https://github.com/actions/toolkit/blob/master/packages/core/src/core.ts#L67 +function getInputName(name: string): string { + return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; +} + +function setInput(name: string, value: string): void { + process.env[getInputName(name)] = value; +} diff --git a/jest.config.ts b/jest.config.ts new file mode 100644 index 0000000..937f4b4 --- /dev/null +++ b/jest.config.ts @@ -0,0 +1,29 @@ +import fs from 'fs'; +import os from 'os'; +import path from 'path'; + +const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-docker-action-')); + +process.env = Object.assign({}, process.env, { + TEMP: tmpDir, + GITHUB_REPOSITORY: 'docker/setup-docker-action', + RUNNER_TEMP: path.join(tmpDir, 'runner-temp'), + RUNNER_TOOL_CACHE: path.join(tmpDir, 'runner-tool-cache') +}) as { + [key: string]: string; +}; + +module.exports = { + clearMocks: true, + moduleFileExtensions: ['js', 'ts'], + testMatch: ['**/*.test.ts'], + transform: { + '^.+\\.ts$': 'ts-jest' + }, + moduleNameMapper: { + '^csv-parse/sync': '/node_modules/csv-parse/dist/cjs/sync.cjs' + }, + collectCoverageFrom: ['src/**/{!(main.ts),}.ts'], + coveragePathIgnorePatterns: ['lib/', 'node_modules/', '__mocks__/', '__tests__/'], + verbose: true +};