From 74b43f666e5fdd368c5fcaf497c6f541b1f93b60 Mon Sep 17 00:00:00 2001 From: AliakseiLiasnitski Date: Wed, 28 Feb 2024 23:26:50 +0300 Subject: [PATCH] EPMRPP-89644 || Add setup file to add ReportingApi to the global variable --- package.json | 11 +++++++++++ src/models/index.ts | 2 ++ src/models/reporting.ts | 4 ++++ src/reportingApi.ts | 4 ++++ src/setup.ts | 12 ++++++++++++ src/types/global.d.ts | 20 ++++++++++++++++++++ src/types/interfaces.d.ts | 4 ++++ 7 files changed, 57 insertions(+) create mode 100644 src/setup.ts create mode 100644 src/types/global.d.ts diff --git a/package.json b/package.json index e1ac6c4..3c8ef17 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,17 @@ "description": "Agent to integrate Vitest with ReportPortal.", "main": "build/index.js", "types": "build/index.d.ts", + "type": "module", + "exports": { + ".": { + "import": "./build/index.js", + "require": "./build/index.js" + }, + "./setup": { + "import": "./build/setup.js", + "require": "./build/setup.js" + } + }, "scripts": { "build": "npm run clean && tsc", "clean": "rimraf ./build", diff --git a/src/models/index.ts b/src/models/index.ts index f5d4968..516d5aa 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -23,6 +23,7 @@ import { Attachment, RPTaskMeta, ReportingApi, + GlobalReportingApi, } from './reporting'; import { ReportPortalConfig } from './configs'; import { Attribute } from './common'; @@ -36,5 +37,6 @@ export { Attribute, RPTaskMeta, ReportingApi, + GlobalReportingApi, LogRQ, }; diff --git a/src/models/reporting.ts b/src/models/reporting.ts index 01b8f04..161eb4d 100644 --- a/src/models/reporting.ts +++ b/src/models/reporting.ts @@ -71,3 +71,7 @@ export interface RPTaskMeta extends TaskMeta { export interface ReportingApi { attachment: (context: Task, data: Attachment) => void; } + +export interface GlobalReportingApi { + attachment: (data: Attachment) => void; +} diff --git a/src/reportingApi.ts b/src/reportingApi.ts index e23a13f..aa896e9 100644 --- a/src/reportingApi.ts +++ b/src/reportingApi.ts @@ -23,3 +23,7 @@ const attachment = (task: Task, data: Models.Attachment) => { export const ReportingApi: Models.ReportingApi = { attachment } + +export const bindReportingApi = (task: Task): Models.GlobalReportingApi => ({ + attachment: (data: Models.Attachment) => attachment(task, data) +}) diff --git a/src/setup.ts b/src/setup.ts new file mode 100644 index 0000000..d80afb0 --- /dev/null +++ b/src/setup.ts @@ -0,0 +1,12 @@ +import { afterEach, beforeEach } from "vitest"; +import { bindReportingApi } from "./reportingApi"; + +beforeEach(async (ctx) => { + // @ts-ignore + global.ReportingApi = bindReportingApi(ctx.task); +}); + +afterEach(() => { + // @ts-ignore + global.ReportingApi = undefined; +}); diff --git a/src/types/global.d.ts b/src/types/global.d.ts new file mode 100644 index 0000000..fd485bb --- /dev/null +++ b/src/types/global.d.ts @@ -0,0 +1,20 @@ +/* + * Copyright 2024 EPAM Systems + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +declare global { + export const ReportingApi: GlobalReportingApi; +} diff --git a/src/types/interfaces.d.ts b/src/types/interfaces.d.ts index b23e36a..1f33700 100644 --- a/src/types/interfaces.d.ts +++ b/src/types/interfaces.d.ts @@ -43,4 +43,8 @@ declare namespace Interfaces { interface ReportingApi { attachment: (task: Task, data: Attachment) => void; } + + interface GlobalReportingApi { + attachment: (data: Attachment) => void; + } }