From 4345d76e010450213322adaf5cf433f4285f492b Mon Sep 17 00:00:00 2001 From: Albin Ramovic Date: Tue, 19 Nov 2024 18:04:39 +0100 Subject: [PATCH] initial commit - tests cleanup --- __tests__/noOrdInCdsrc.test.js | 10 +- __tests__/ordCdsrc.test.js | 25 +-- __tests__/ordPackageJson.test.js | 17 +- __tests__/protectedServices.test.js | 18 +- .../unittest/extendOrdWithCustom.test.js | 189 +++++++++++------- 5 files changed, 143 insertions(+), 116 deletions(-) diff --git a/__tests__/noOrdInCdsrc.test.js b/__tests__/noOrdInCdsrc.test.js index a327140..d86b5d2 100644 --- a/__tests__/noOrdInCdsrc.test.js +++ b/__tests__/noOrdInCdsrc.test.js @@ -1,20 +1,20 @@ const cds = require("@sap/cds"); -const ord = require("../lib/ord"); const path = require("path"); const csn = require("./__mocks__/publicResourcesCsn.json"); -jest.mock("../lib/date", () => ({ - getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00") -})); - describe("Tests for ORD document when .cdsrc.json has no `ord` property", () => { + let ord; + beforeAll(() => { + jest.spyOn(require("../lib/date"), "getRFC3339Date").mockReturnValue("2024-11-04T14:33:25+01:00"); + ord = require("../lib/ord"); cds.root = path.join(__dirname, "bookshop"); cds.env = {}; }); afterAll(() => { jest.clearAllMocks(); + jest.resetAllMocks(); }); test("Successfully create ORD Documents with no `ord` in .cdsrc.json", () => { diff --git a/__tests__/ordCdsrc.test.js b/__tests__/ordCdsrc.test.js index 3722e25..85208f0 100644 --- a/__tests__/ordCdsrc.test.js +++ b/__tests__/ordCdsrc.test.js @@ -1,26 +1,19 @@ const cds = require("@sap/cds"); -const ord = require("../lib/ord"); const path = require("path"); -// Mock the @sap/cds module -jest.mock("@sap/cds", () => { - const path = require("path"); - let cds = jest.requireActual("@sap/cds"); - cds.root = path.join(__dirname, "bookshop"); - - return cds; -}); - -jest.mock("../lib/date", () => ({ - getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00") -})); - - describe("Tests for default ORD document when .cdsrc.json is present", () => { - let csn; + let csn, ord; beforeAll(async () => { + cds.root = path.join(__dirname, "bookshop"); csn = await cds.load(path.join(cds.root, "srv")); + jest.spyOn(require("../lib/date"), "getRFC3339Date").mockReturnValue("2024-11-04T14:33:25+01:00"); + ord = require("../lib/ord"); + }); + + afterAll(() => { + jest.clearAllMocks(); + jest.resetAllMocks(); }); test("Successfully create ORD Documents with defaults", () => { diff --git a/__tests__/ordPackageJson.test.js b/__tests__/ordPackageJson.test.js index 867cfee..641ce6f 100644 --- a/__tests__/ordPackageJson.test.js +++ b/__tests__/ordPackageJson.test.js @@ -1,20 +1,19 @@ const cds = require("@sap/cds"); -const ord = require("../lib/ord"); const path = require("path"); -jest.mock("../lib/date", () => ({ - getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00") -})); - describe("Tests for default ORD document when .cdsrc.json is not present", () => { - let csn; + let csn, ord; beforeAll(async () => { - csn = await cds.load(path.join(__dirname, "bookshop", "srv")); + jest.spyOn(require("../lib/date"), "getRFC3339Date").mockReturnValue("2024-11-04T14:33:25+01:00"); + ord = require("../lib/ord"); + cds.root = path.join(__dirname, "bookshop"); + csn = await cds.load(path.join(cds.root, "srv")); }); - beforeEach(() => { - cds.root = path.join(__dirname, "bookshop"); + afterAll(() => { + jest.clearAllMocks(); + jest.resetAllMocks(); }); test("Successfully create ORD Documents with defaults", () => { diff --git a/__tests__/protectedServices.test.js b/__tests__/protectedServices.test.js index 67424a1..b3ef58c 100644 --- a/__tests__/protectedServices.test.js +++ b/__tests__/protectedServices.test.js @@ -1,9 +1,9 @@ const cds = require("@sap/cds"); -const internal_csn = require("./__mocks__/internalResourcesCsn.json"); -const ord = require("../lib/ord"); +const csnInternal = require("./__mocks__/internalResourcesCsn.json"); +const csnPrivate = require("./__mocks__/privateResourcesCsn.json"); const path = require("path"); -const private_csn = require("./__mocks__/privateResourcesCsn.json"); +let ord; function checkOrdDocument(csn) { const document = ord(csn); @@ -13,11 +13,6 @@ function checkOrdDocument(csn) { expect(document.eventResources).toHaveLength(0); } - -jest.mock("../lib/date", () => ({ - getRFC3339Date: jest.fn(() => "2024-11-04T14:33:25+01:00") -})); - describe("Tests for ORD document when there is no public service", () => { beforeAll(() => { cds.root = path.join(__dirname, "bookshop"); @@ -27,17 +22,20 @@ describe("Tests for ORD document when there is no public service", () => { description: "this is my custom description", policyLevel: "sap:core:v1" }; + jest.spyOn(require("../lib/date"), "getRFC3339Date").mockReturnValue("2024-11-04T14:33:25+01:00"); + ord = require("../lib/ord"); }); afterAll(() => { jest.clearAllMocks(); + jest.resetAllMocks(); }); test("All services are private: Successfully create ORD Documents without packages, empty apiResources and eventResources lists", () => { - checkOrdDocument(private_csn); + checkOrdDocument(csnPrivate); }); test("All services are internal: Successfully create ORD Documents without packages, empty apiResources and eventResources lists", () => { - checkOrdDocument(internal_csn); + checkOrdDocument(csnInternal); }); }); diff --git a/__tests__/unittest/extendOrdWithCustom.test.js b/__tests__/unittest/extendOrdWithCustom.test.js index 4052a98..4c63f48 100644 --- a/__tests__/unittest/extendOrdWithCustom.test.js +++ b/__tests__/unittest/extendOrdWithCustom.test.js @@ -1,116 +1,151 @@ const cds = require("@sap/cds"); -const path = require('path'); -const { extendCustomORDContentIfExists } = require('../../lib/extendOrdWithCustom'); +const path = require("path"); +const { extendCustomORDContentIfExists } = require("../../lib/extendOrdWithCustom"); -jest.mock("@sap/cds", () => { - const actualCds = jest.requireActual('@sap/cds'); - return { - ...actualCds, - env: {}, - log: jest.fn(() => ({ - levels: { DEBUG: 0, WARN: 1 }, - warn: jest.fn(() => console.warn('Mocked warning')), - error: jest.fn(() => console.error('Mocked error')), - })), - }; -}); - -describe('extendOrdWithCustom', () => { +describe("extendOrdWithCustom", () => { let appConfig = {}; + let warningSpy; + + beforeAll(() => { + cds.env = {}; + warningSpy = jest.spyOn(console, "warn"); + }); beforeEach(() => { appConfig = { env: { - customOrdContentFile: 'customOrdContentFile.json', + customOrdContentFile: "customOrdContentFile.json", }, }; }); - describe('extendCustomORDContentIfExists', () => { - it('should skip if there is no customOrdContentFile property in the .cdsrc.json', () => { + afterAll(() => { + jest.resetAllMocks(); + jest.clearAllMocks(); + }); + + describe("extendCustomORDContentIfExists", () => { + it("should skip if there is no customOrdContentFile property in the .cdsrc.json", () => { const ordContent = {}; appConfig.env.customOrdContentFile = undefined; const result = extendCustomORDContentIfExists(appConfig, ordContent); expect(result).toEqual(ordContent); }); - it('should skip if customOrdContentFile property in the .cdsrc.json points to NON-EXISTING custom ord file', () => { + it("should skip if customOrdContentFile property in the .cdsrc.json points to NON-EXISTING custom ord file", () => { const ordContent = {}; appConfig.env.customOrdContentFile = "./ord/NotExistingCustom.ord.json"; - const result = extendCustomORDContentIfExists(appConfig, ordContent); + const result = extendCustomORDContentIfExists( + appConfig, + ordContent + ); expect(result).toEqual(ordContent); }); - it('should ignore and log warn if found ord top-level primitive property in customOrdFile', () => { + it("should ignore and log warn if found ord top-level primitive property in customOrdFile", () => { const ordContent = {}; - const warningSpy = jest.spyOn(console, 'warn'); - prepareTestEnvironment({ namespace: "sap.sample" }, appConfig, 'testCustomORDContentFileThrowErrors.json'); - const result = extendCustomORDContentIfExists(appConfig, ordContent); + prepareTestEnvironment({ namespace: "sap.sample" }, appConfig, "testCustomORDContentFileThrowErrors.json"); + const result = extendCustomORDContentIfExists(appConfig,ordContent); expect(warningSpy).toHaveBeenCalledTimes(3); - expect(warningSpy).toHaveBeenCalledWith('Mocked warning'); - + expect(warningSpy).toHaveBeenCalledWith( + "[ord-plugin] -", + expect.stringContaining("Found ord top level primitive ord property in customOrdFile:"), + expect.anything(), + expect.stringContaining("Please define it in .cdsrc.json.")); expect(result).toMatchSnapshot(); }); - it('should add new ord resources that are not supported by cap framework', () => { + it("should add new ord resources that are not supported by cap framework", () => { const ordContent = {}; - prepareTestEnvironment({}, appConfig, 'testCustomORDContentFileWithNewResources.json'); - const result = extendCustomORDContentIfExists(appConfig, ordContent); + prepareTestEnvironment( + {}, + appConfig, + "testCustomORDContentFileWithNewResources.json" + ); + const result = extendCustomORDContentIfExists( + appConfig, + ordContent + ); expect(result).toMatchSnapshot(); }); - it('should enhance the list of generated ord resources', () => { - const ordContent = { packages: [{ ordId: "sap.sm:package:smDataProducts:v1", localId: "smDataProductsV1" }] }; - prepareTestEnvironment({}, appConfig, 'testCustomORDContentFileWithEnhanced.json'); - const result = extendCustomORDContentIfExists(appConfig, ordContent); + it("should enhance the list of generated ord resources", () => { + const ordContent = { + packages: [ + { + ordId: "sap.sm:package:smDataProducts:v1", + localId: "smDataProductsV1", + }, + ], + }; + prepareTestEnvironment( + {}, + appConfig, + "testCustomORDContentFileWithEnhanced.json" + ); + const result = extendCustomORDContentIfExists( + appConfig, + ordContent + ); expect(result).toMatchSnapshot(); }); - it('should should patch the existing generated ord resources', () => { + it("should should patch the existing generated ord resources", () => { const ordContent = { - packages: [{ - ordId: "sap.sm:package:smDataProducts:v1", - localId: "smDataProductsV1" - }], - apiResources: [{ - ordId: "sap.sm:apiResource:SupplierService:v1", - title: "should be removed", - partOfGroups: [ - "sap.cds:service:sap.test.cdsrc.sample:originalService" - ], - partOfPackage: "sap.sm:package:smDataProducts:v2", - extensible: { - "supported": "no" + packages: [ + { + ordId: "sap.sm:package:smDataProducts:v1", + localId: "smDataProductsV1", + }, + ], + apiResources: [ + { + ordId: "sap.sm:apiResource:SupplierService:v1", + title: "should be removed", + partOfGroups: [ + "sap.cds:service:sap.test.cdsrc.sample:originalService", + ], + partOfPackage: "sap.sm:package:smDataProducts:v2", + extensible: { + supported: "no", + }, + entityTypeMappings: [ + { + entityTypeTargets: [ + { + ordId: "sap.odm:entityType:BusinessPartner:v2", + }, + { + ordId: "sap.odm:entityType:BusinessPartner:v3", + }, + ], + }, + ], }, - entityTypeMappings: [ - { - entityTypeTargets: [ - { - ordId: "sap.odm:entityType:BusinessPartner:v2" - }, - { - ordId: "sap.odm:entityType:BusinessPartner:v3" - } - ] - } - ] - }, - { - ordId: "sap.sm:apiResource:orginalService:v2", - partOfGroups: [ - "sap.cds:service:sap.test.cdsrc.sample:originalService" - ], - partOfPackage: "sap.sm:package:smDataProducts:v2", - entityTypeMappings: [ - { - entityTypeTargets: [] - } - ] - }] + { + ordId: "sap.sm:apiResource:orginalService:v2", + partOfGroups: [ + "sap.cds:service:sap.test.cdsrc.sample:originalService", + ], + partOfPackage: "sap.sm:package:smDataProducts:v2", + entityTypeMappings: [ + { + entityTypeTargets: [], + }, + ], + }, + ], }; - prepareTestEnvironment({}, appConfig, 'testCustomORDContentFileWithPatch.json'); - const result = extendCustomORDContentIfExists(appConfig, ordContent); + prepareTestEnvironment( + {}, + appConfig, + "testCustomORDContentFileWithPatch.json" + ); + const result = extendCustomORDContentIfExists( + appConfig, + ordContent + ); expect(result).toMatchSnapshot(); }); }); @@ -119,5 +154,7 @@ describe('extendOrdWithCustom', () => { function prepareTestEnvironment(ordEnvVariables, appConfig, testFileName) { cds.env["ord"] = ordEnvVariables; appConfig.env.customOrdContentFile = testFileName; - jest.spyOn(path, 'join').mockReturnValueOnce(`${__dirname}/utils/${testFileName}`); + jest.spyOn(path, "join").mockReturnValueOnce( + `${__dirname}/utils/${testFileName}` + ); }