Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
timeout-minutes: 10
strategy:
matrix:
node: ['12', '14', '16']
node: ['14', '16', '17']
steps:
- name: Set running flag
run: echo "RUNNING=1" >> $GITHUB_ENV
Expand All @@ -62,6 +62,7 @@ jobs:
FILES: |
yarn.lock
jest.config.js
vite.config.ts
if: "! contains(env.COMMIT_MESSAGE, '[skip ci]') && ! contains(env.COMMIT_MESSAGE, '[ci skip]')"
- name: Set running flag
run: echo "RUNNING=" >> $GITHUB_ENV
Expand Down
13 changes: 0 additions & 13 deletions jest.config.js

This file was deleted.

3 changes: 0 additions & 3 deletions jest.setup.ts

This file was deleted.

14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
],
"scripts": {
"build": "tsc",
"cover": "jest --coverage",
"lint": "eslint 'src/**/*.ts' '__tests__/**/*.ts' --cache",
"lint:fix": "eslint --fix 'src/**/*.ts' '__tests__/**/*.ts'",
"cover": "vitest run --coverage",
"lint": "eslint 'src/**/*.ts' --cache",
"lint:fix": "eslint --fix 'src/**/*.ts'",
"test": "yarn lint && yarn cover",
"update": "npm_config_yes=true npx npm-check-updates -u --timeout 100000 && yarn install && yarn upgrade && yarn audit"
},
Expand All @@ -40,16 +40,14 @@
"js-yaml": "^4.1.0"
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/js-yaml": "^4.0.5",
"@types/node": "^17.0.24",
"@typescript-eslint/eslint-plugin": "^5.19.0",
"@typescript-eslint/parser": "^5.19.0",
"c8": "^7.11.0",
"eslint": "^8.13.0",
"jest": "^27.5.1",
"jest-circus": "^27.5.1",
"ts-jest": "^27.1.4",
"typescript": "^4.6.3"
"typescript": "^4.6.3",
"vitest": "^0.9.3"
},
"publishConfig": {
"access": "public"
Expand Down
9 changes: 5 additions & 4 deletions __tests__/command.test.ts → src/command.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-magic-numbers */
import {spyOnExportVariable, exportVariableCalledWith, spyOnAddPath, addPathCalledWith} from '../src';
import {addPath, exportVariable} from '@actions/core';
import { describe, it } from 'vitest';
import { spyOnExportVariable, exportVariableCalledWith, spyOnAddPath, addPathCalledWith } from '../src';
import { addPath, exportVariable } from '@actions/core';

describe('spyOnExportVariable, exportVariableCalledWith', () => {
it('should spy on exportVariable', () => {
Expand All @@ -10,8 +11,8 @@ describe('spyOnExportVariable, exportVariableCalledWith', () => {
exportVariable('test-name2', ['test-value2-1', 'test-value2-2']);

exportVariableCalledWith(spy, [
{name: 'test-name1', val: 'test-value1'},
{name: 'test-name2', val: ['test-value2-1', 'test-value2-2']},
{ name: 'test-name1', val: 'test-value1' },
{ name: 'test-name2', val: ['test-value2-1', 'test-value2-2'] },
]);
});
});
Expand Down
13 changes: 8 additions & 5 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import * as core from '@actions/core' ;
import SpyInstance = jest.SpyInstance;
import type { SpyInstance } from 'vitest';
import * as core from '@actions/core';
import { expect, vi } from 'vitest';

export const spyOnExportVariable = (): SpyInstance => jest.spyOn(core, 'exportVariable').mockReturnValue();
vi.mock('@actions/core');

export const spyOnExportVariable = (): SpyInstance => vi.spyOn(core, 'exportVariable').mockReturnValue();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const exportVariableCalledWith = (spyOnMock: SpyInstance, pairs: { name: string, val: any }[]): void => {
expect(spyOnMock).toBeCalledTimes(pairs.length);
pairs.forEach(({name, val}, index) => {
pairs.forEach(({ name, val }, index) => {
expect(spyOnMock.mock.calls[index][0]).toBe(name);
expect(spyOnMock.mock.calls[index][1]).toEqual(val);
});
};

export const spyOnAddPath = (): SpyInstance => jest.spyOn(core, 'addPath').mockReturnValue();
export const spyOnAddPath = (): SpyInstance => vi.spyOn(core, 'addPath').mockReturnValue();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const addPathCalledWith = (spyOnMock: SpyInstance, paths: string[]): void => {
expect(spyOnMock).toBeCalledTimes(paths.length);
Expand Down
7 changes: 4 additions & 3 deletions __tests__/context.test.ts → src/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-magic-numbers */
import {getContext, generateContext, createResponse} from '../src';
import { describe, expect, it } from 'vitest';
import { getContext, generateContext, createResponse } from '../src';

describe('getContext', () => {
it('should get context', () => {
Expand Down Expand Up @@ -101,12 +102,12 @@ describe('createResponse', () => {
});

it('should override', () => {
const response = createResponse<number>(123, {data: 234});
const response = createResponse<number>(123, { data: 234 });
expect(response).toHaveProperty('data');
expect(response.data).toBe(234);

const iterator = response[Symbol.iterator]();
expect(iterator).toHaveProperty('next');
expect(iterator.next()).toEqual({done: true, value: true});
expect(iterator.next()).toEqual({ done: true, value: true });
});
});
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Context} from '@actions/github/lib/context';
import {OctokitResponse} from '@octokit/types';
import type { Context } from '@actions/github/lib/context';
import type { OctokitResponse } from '@octokit/types';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const getContext = (override: { [key: string]: any }): Context => Object.assign({
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 13 additions & 10 deletions src/global.setup.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
/* eslint-disable no-magic-numbers */
import {EOL} from 'os';
import type childProcess from 'child_process';
import { EOL } from 'os';
import { vi } from 'vitest';
import global from './global';

export const setupGlobal = (): void => {
global.mockStdout = {
write: jest.fn(),
write: vi.fn(),
};
process.stdout.write = global.mockStdout.write;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type converterType = (value: any) => boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const converter = (prefix = ''): converterType => (value: any): boolean => process.stdout.write(prefix + JSON.stringify(value, null, '\t') + EOL);
console.log = jest.fn(converter());
console.info = jest.fn(converter('__info__'));
console.error = jest.fn(converter('__error__'));
console.warn = jest.fn(converter('__warning__'));
console.log = vi.fn(converter());
console.info = vi.fn(converter('__info__'));
console.error = vi.fn(converter('__error__'));
console.warn = vi.fn(converter('__warning__'));

global.mockChildProcess = {
stdout: 'stdout',
stderr: '',
error: null,
code: 0,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
exec: jest.fn((...args: any[]) => {
exec: vi.fn((...args: any[]) => {
const callback = args.length === 2 ? args[1] : args[2];
callback(
typeof global.mockChildProcess.error === 'function' ? global.mockChildProcess.error(args[0]) : global.mockChildProcess.error,
Expand All @@ -32,7 +34,7 @@ export const setupGlobal = (): void => {
);
}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
spawn: jest.fn((...args: any[]) => ({
spawn: vi.fn((...args: any[]) => ({
stdout: {
on: (event, callback): void => {
if (event === 'data') {
Expand All @@ -59,10 +61,11 @@ export const setupGlobal = (): void => {
},
})),
};
jest.mock('child_process', () => (Object.assign(jest.requireActual('child_process'), {
vi.mock('child_process', async() => ({
...await vi.importActual<typeof childProcess>('child_process'),
exec: global.mockChildProcess.exec,
spawn: global.mockChildProcess.spawn,
})));
}));

process.env.GITHUB_ACTOR = 'octocat';
process.env.GITHUB_ENV = '/home/runner/work/_temp/_runner_file_commands/set_env_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee';
Expand Down
9 changes: 5 additions & 4 deletions src/global.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {ExecException} from 'child_process';
import type { ExecException } from 'child_process';
import type { SpyInstanceFn } from 'vitest';

declare global {
interface Global {
mockStdout: {
write: jest.Mock;
write: SpyInstanceFn;
};
mockChildProcess: {
exec: jest.Mock;
spawn: jest.Mock;
exec: SpyInstanceFn;
spawn: SpyInstanceFn;
stdout: string | ((command: string) => string);
stderr: string | ((command: string) => string);
error: ExecException | null | ((command: string) => ExecException | null);
Expand Down
9 changes: 5 additions & 4 deletions __tests__/nock.test.ts → src/nock.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-magic-numbers */
import path from 'path';
import {getConfigFixture, getApiFixture, disableNetConnect, encodeContent} from '../src';
import { describe, expect, it, vi } from 'vitest';
import { getConfigFixture, getApiFixture, disableNetConnect, encodeContent } from '../src';

describe('getConfigFixture', () => {
it('should get config fixture', () => {
Expand Down Expand Up @@ -47,9 +48,9 @@ describe('getApiFixture', () => {
});

describe('disableNetConnect', () => {
const fn1 = jest.fn();
const fn2 = jest.fn();
const fn3 = jest.fn();
const fn1 = vi.fn();
const fn2 = vi.fn();
const fn3 = vi.fn();
disableNetConnect({
disableNetConnect: fn1,
cleanAll: fn2,
Expand Down
9 changes: 5 additions & 4 deletions src/nock.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'path';
import fs from 'fs';
import {load} from 'js-yaml';
import { load } from 'js-yaml';
import { afterEach, beforeEach } from 'vitest';

export const encodeContent = (content: string): string => Buffer.from(content).toString('base64');

Expand All @@ -10,7 +11,7 @@ export const getConfigFixture = (rootDir: string, fileName = 'config.yml'): { [k
encoding: 'base64',
size: 5362,
name: fileName,
path: `.github/${fileName}`,
path: `.github/${ fileName }`,
content: encodeContent(fs.readFileSync(path.resolve(rootDir, fileName)).toString()),
sha: '3d21ec53a331a6f037a91c368710b99387d012c1',
url:
Expand All @@ -33,15 +34,15 @@ export const getConfigFixture = (rootDir: string, fileName = 'config.yml'): { [k

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const getApiFixture = (rootDir: string, name: string, ext = '.json'): any => {
const content = fs.readFileSync(path.resolve(rootDir, `${name}${ext}`)).toString();
const content = fs.readFileSync(path.resolve(rootDir, `${ name }${ ext }`)).toString();
switch (ext.toLowerCase()) {
case '.json':
return JSON.parse(content);
case '.yml':
case '.yaml':
return load(content) || {};
default:
return {content};
return { content };
}
};

Expand Down
3 changes: 3 additions & 0 deletions src/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { setupGlobal } from '.';

setupGlobal();
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {GitHub} from '@actions/github/lib/utils';
import {RestEndpointMethods} from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types';
import type { GitHub } from '@actions/github/lib/utils';
import type { RestEndpointMethods } from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types';

export type Octokit = InstanceType<typeof GitHub> & {
rest: RestEndpointMethods;
Expand Down
Loading