Skip to content
This repository has been archived by the owner on Apr 22, 2022. It is now read-only.

Commit

Permalink
Unit tests (Azure#2)
Browse files Browse the repository at this point in the history
* initial commit

* Create main.yml

* Update main.yml

* added action.yml

* added action.yml

* added action.yml

* Delete main.yml

* removed node_modules

* changes in .gitignore

* changes in PowerShellToolRunner

* Added changes for review comments

* added inputs in action.yml

* changes in package.json

* added review comments

* Update action.yml

* Update action.yml

* fix in ScriptRunner.ts

* changes in URL

* added review comments

* changes for review comments

* added args in PowerShellToolRunner

* changes in PowerShellToolRunner

* fixed action.yml

* removed debug logs

* changes in main

* added unit tests

* added ci.yml

* update tsconfig json

* update ci.yml

* added unit tests

* update tests
  • Loading branch information
aksm-ms authored Apr 13, 2020
1 parent d1a6510 commit b479e38
Show file tree
Hide file tree
Showing 8 changed files with 4,708 additions and 33 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
on:
pull_request:
branches:
- master
push:
branches:
- master

jobs:
build_test_job:
name: 'Build and test job'
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
steps:

- name: 'Checking out repo code'
uses: actions/checkout@v2

- name: 'Validate build'
run: |
npm install
npm run build
- name: 'Run L0 tests'
run: |
npm run test
22 changes: 22 additions & 0 deletions __tests__/InitializeAzure.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import InitializeAzure from '../src/InitializeAzure';

jest.mock('../src/Utilities/Utils');
jest.mock('../src/Utilities/PowerShellToolRunner');

afterEach(() => {
jest.restoreAllMocks();
});

describe('Testing importAzModule', () => {
let importAzModuleSpy;
beforeEach(() => {
importAzModuleSpy = jest.spyOn(InitializeAzure, 'importAzModule');
});

test('InitializeAzure importAzModule should pass', async () => {
const azVersion: string = '9.0.0';
await InitializeAzure.importAzModule(azVersion);
await InitializeAzure.importAzModule('latest');
expect(importAzModuleSpy).toHaveBeenCalledTimes(2);
});
});
26 changes: 26 additions & 0 deletions __tests__/ScriptRunner.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import ScriptRunner from '../src/ScriptRunner';

jest.mock('../src/Utilities/FileUtils');
jest.mock('../src/Utilities/PowerShellToolRunner');
jest.mock('../src/Utilities/ScriptBuilder');
let scriptRunner: ScriptRunner;

beforeAll(() => {
scriptRunner = new ScriptRunner("inlineScript", "Stop", true);
});

afterEach(() => {
jest.restoreAllMocks();
});

describe('Testing ScriptRunner', () => {
let executeFileSpy;
beforeEach(() => {
executeFileSpy = jest.spyOn(scriptRunner, 'executeFile');
});
test('executeFile should pass', async () => {
executeFileSpy.mockImplementationOnce(() => Promise.resolve());
await scriptRunner.executeFile();
expect(executeFileSpy).toHaveBeenCalled();
});
});
57 changes: 57 additions & 0 deletions __tests__/Utilities/Utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Utils from '../../src/Utilities/Utils';

const version: string = '9.0.0';
const moduleName: string = 'az';

afterEach(() => {
jest.restoreAllMocks();
});

describe('Testing isValidVersion', () => {
const validVersion: string = '1.2.4';
const invalidVersion: string = 'a.bcd';

test('isValidVersion should be true', () => {
expect(Utils.isValidVersion(validVersion)).toBeTruthy();
});
test('isValidVersion should be false', () => {
expect(Utils.isValidVersion(invalidVersion)).toBeFalsy();
});
});

describe('Testing setPSModulePath', () => {
test('PSModulePath with azPSVersion non-empty', () => {
Utils.setPSModulePath(version);
expect(process.env.PSModulePath).toContain(version);
});
test('PSModulePath with azPSVersion empty', () => {
const prevPSModulePath = process.env.PSModulePath;
Utils.setPSModulePath();
expect(process.env.PSModulePath).not.toEqual(prevPSModulePath);
});
});

describe('Testing getLatestModule', () => {
let getLatestModuleSpy;

beforeEach(() => {
getLatestModuleSpy = jest.spyOn(Utils, 'getLatestModule');
});
test('getLatestModule should pass', async () => {
getLatestModuleSpy.mockImplementationOnce((_moduleName: string) => Promise.resolve(version));
await Utils.getLatestModule(moduleName);
expect(getLatestModuleSpy).toHaveBeenCalled();
});
});

describe('Testing checkModuleVersion', () => {
let checkModuleVersionSpy;
beforeEach(() => {
checkModuleVersionSpy = jest.spyOn(Utils, 'checkModuleVersion');
});
test('checkModuleVersion should pass', async () => {
checkModuleVersionSpy.mockImplementationOnce((_moduleName: string, _version: string) => Promise.resolve());
await Utils.checkModuleVersion(moduleName, version);
expect(checkModuleVersionSpy).toHaveBeenCalled();
});
});
15 changes: 15 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html

module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
};

Loading

0 comments on commit b479e38

Please sign in to comment.