Skip to content

Commit

Permalink
Merge pull request #161 from celonis/TA-2683-testing
Browse files Browse the repository at this point in the history
[TA-2683] Setup tests and add tests for listing assignments
  • Loading branch information
promeris authored Dec 15, 2023
2 parents bf14484 + ca5cf5e commit c35bf3a
Showing 11 changed files with 2,063 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-and-publish.yml
Original file line number Diff line number Diff line change
@@ -27,6 +27,9 @@ jobs:
- name: Yarn Build
run: yarn build

- name: Yarn Test
run: yarn test

- name: Move files to build
run: |
cp README.md dist/README.md
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -16,3 +16,5 @@ jobs:
run: yarn install
- name: Yarn Build
run: yarn build
- name: Yarn Test
run: yarn test
16 changes: 16 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Config } from "@jest/types"
const config: Config.InitialOptions = {
verbose: true,
transform: {
"^.+\\.tsx?$": "ts-jest",
},
testMatch: ["<rootDir>/tests/**/*.spec.ts"],
moduleNameMapper: {
"^\\./../package.json$": "<rootDir>/tests/mocks/package.json",
},
setupFilesAfterEnv: [
"<rootDir>/tests/jest.setup.ts",
],
}

export default config
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@celonis/content-cli",
"version": "0.6.0",
"version": "0.6.1",
"description": "CLI Tool to help manage content in Celonis EMS",
"main": "content-cli.js",
"bin": {
@@ -11,7 +11,8 @@
"homepage": "https://github.com/celonis/content-cli",
"scripts": {
"build": "./node_modules/.bin/tsc && cp package.json dist/package.json",
"lint": "tslint -p ."
"lint": "tslint -p .",
"test": "jest"
},
"dependencies": {
"@datadog/datadog-ci": "^0.17.12",
@@ -26,9 +27,13 @@
},
"devDependencies": {
"@types/adm-zip": "^0.4.34",
"@types/jest": "29.5.11",
"@types/node": "20.10.4",
"jest": "29.7.0",
"lint-staged": "^9.5.0",
"prettier": "^1.19.1",
"ts-jest": "29.1.1",
"ts-node": "10.9.2",
"tslint": "^5.20.1",
"tslint-config-prettier": "^1.18.0",
"tslint-consistent-codestyle": "^1.16.0",
25 changes: 25 additions & 0 deletions tests/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as fs from 'fs';
import {setDefaultProfile} from "./utls/context-mock";
import {TestTransport} from "./utls/test-transport";
import {logger} from "../src/util/logger";

// Mock the modules using Jest
jest.mock('axios');
jest.mock('fs');

(fs.writeFileSync as jest.Mock).mockImplementation(() => {
// Mock implementation here if needed
});

beforeAll(() => {
setDefaultProfile();
})

let testTransport;

beforeEach(() => {
testTransport = new TestTransport({})
logger.add(testTransport);
})

export {testTransport};
66 changes: 66 additions & 0 deletions tests/list/assignments.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {VariableCommand} from "../../src/commands/variable.command";
import axios from "axios";
import {FileService} from "../../src/services/file-service";
import * as fs from "fs";
import * as path from "path";
import {testTransport} from "../jest.setup";

describe("List assignments", () => {

it("Should list assignments for supported type and non-json response", async () => {
const mockAssignmentValues = [
{id: "id-1"},
{id: "id-2"}
];
const resp = {data: mockAssignmentValues};
(axios.get as jest.Mock).mockResolvedValue(resp);

await new VariableCommand().listAssignments("DATA_MODEL", false, "");

expect(testTransport.logMessages.length).toBe(2);
expect(testTransport.logMessages[0].message).toContain('{"id":"id-1"}');
expect(testTransport.logMessages[1].message).toContain('{"id":"id-2"}');

expect(axios.get).toHaveBeenCalledWith("https://myTeam.celonis.cloud/package-manager/api/compute-pools/pools-with-data-models", expect.anything())
})

it("Should export assignments for supported type and json response", async () => {
const mockAssignmentValues = [
{id: "id-1"},
{id: "id-2"}
];
const resp = {data: mockAssignmentValues};
(axios.get as jest.Mock).mockResolvedValue(resp);

await new VariableCommand().listAssignments("DATA_MODEL", true, "");

expect(testTransport.logMessages.length).toBe(1);
expect(testTransport.logMessages[0].message).toContain(FileService.fileDownloadedMessage);

const expectedFileName = testTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];

expect(fs.writeFileSync).toHaveBeenCalledWith(path.resolve(process.cwd(), expectedFileName), JSON.stringify(mockAssignmentValues), {encoding: "utf-8"});
})

it("Should contain url params in the url", async () => {
const mockAssignmentValues = [{id: "id-1"}];
const resp = {data: mockAssignmentValues};
(axios.get as jest.Mock).mockResolvedValue(resp);

await new VariableCommand().listAssignments("CONNECTION", false, "param1=value1,param2=value2");

expect(axios.get).toHaveBeenCalledWith("https://myTeam.celonis.cloud/process-automation-v2/api/connections?param1=value1&param2=value2", expect.anything())
})

it("Should throw error for unsupported variable types", async () => {
const type: string = "DUMMY_UNSUPPORTED_TYPE";

try {
await new VariableCommand().listAssignments(type, false, "");
} catch (e) {
if (!(e.message === `Variable type ${type} not supported.`)) {
fail();
}
}
})
})
3 changes: 3 additions & 0 deletions tests/mocks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": "mocked-version"
}
12 changes: 12 additions & 0 deletions tests/utls/context-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {contextService} from "../../src/services/context.service";

export function setDefaultProfile(): void {
contextService.setContext({
profile: {
name: "test",
team: "https://myTeam.celonis.cloud/",
apiToken: "YnQ3N2M0M2ItYzQ3OS00YzgyLTg0ODgtOWNkNzhiNzYwOTU2OlFkNnBpVCs0M0JBYm1ZWGlCZ2hPd245aldwWTNubFQyYVFOTFBUeHEwdUxM",
authenticationType: "Bearer"
}
});
}
15 changes: 15 additions & 0 deletions tests/utls/test-transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as Transport from "winston-transport";
import {LogEntry} from "winston";

export class TestTransport extends Transport {
public logMessages: LogEntry[] = [];

constructor(options: any) {
super(options);
}

public log(info: LogEntry, callback: () => void): void {
this.logMessages.push(info);
callback();
}
}
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@
"module": "umd",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
"experimentalDecorators": true,
"types": ["node", "jest"],
},
"exclude": ["./tests", "./jest.config.ts"]
}
Loading

0 comments on commit c35bf3a

Please sign in to comment.