Skip to content

Commit

Permalink
Merge pull request #2 from cspotcode/fork-tasks
Browse files Browse the repository at this point in the history
Misc tasks to fork
  • Loading branch information
LironHazan authored Sep 12, 2021
2 parents f26dffd + 9717aa0 commit 25b8f1d
Show file tree
Hide file tree
Showing 10 changed files with 6,867 additions and 53 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# ts-mockito [![build badge](https://travis-ci.org/NagRock/ts-mockito.svg?branch=master)](https://travis-ci.org/NagRock/ts-mockito) [![codecov](https://codecov.io/gh/NagRock/ts-mockito/branch/master/graph/badge.svg)](https://codecov.io/gh/NagRock/ts-mockito)
# @cspotcode/ts-mockito [![build badge](https:/cspotcode/ts-mockito/actions/workflows/test.yml/badge.svg)](https:/cspotcode/ts-mockito/actions?query=branch%3Amaster) [![codecov](https://codecov.io/gh/cspotcode/ts-mockito/branch/master/graph/badge.svg)](https://codecov.io/gh/cspotcode/ts-mockito)

> This is a fork of https:/NagRock/ts-mockito. We hope to eventually re-merge and publish as ts-mockito.
Mocking library for TypeScript inspired by http://mockito.org/

## 1.x to 2.x migration guide
[1.x to 2.x migration guide](https:/NagRock/ts-mockito/wiki/ts-mockito-1.x-to-2.x-migration-guide)
[1.x to 2.x migration guide](https:/cspotcode/ts-mockito/wiki/ts-mockito-1.x-to-2.x-migration-guide)

## Main features

Expand All @@ -29,7 +31,7 @@ Mocking library for TypeScript inspired by http://mockito.org/

## Installation

`npm install ts-mockito --save-dev`
`npm install @cspotcode/ts-mockito --save-dev`

## Usage

Expand Down
4 changes: 3 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
process.env.CHROME_BIN = require('puppeteer').executablePath();

module.exports = function (config) {
config.set({

Expand Down Expand Up @@ -26,7 +28,7 @@ module.exports = function (config) {

reporters: ["progress", "mocha"],

browsers: ["CustomChromeHeadless", "PhantomJS"],
browsers: ["CustomChromeHeadless"],

mochaReporter: {
output: 'minimal'
Expand Down
6,813 changes: 6,813 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

43 changes: 22 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ts-mockito",
"name": "@cspotcode/ts-mockito",
"version": "2.6.0",
"description": "Mocking library for TypeScript",
"main": "lib/ts-mockito.js",
Expand All @@ -17,10 +17,13 @@
"lint": "tslint -c tslint.json 'src/**/*.ts' 'test/**/*.ts'"
},
"author": "[email protected]",
"contributors": [
"Andrew Bradley <[email protected]>"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https:/NagRock/ts-mockito"
"url": "https:/cspotcode/ts-mockito"
},
"keywords": [
"mock",
Expand All @@ -32,27 +35,25 @@
"javascript"
],
"devDependencies": {
"@types/jasmine": "^2.6.0",
"@types/lodash": "^4.14.104",
"@types/node": "^8.0.46",
"@types/jasmine": "^3.7.7",
"@types/lodash": "^4.14.170",
"@types/node": "^15.14.0",
"babel-polyfill": "^6.26.0",
"jasmine-core": "^2.8.0",
"jest": "^23.5.0",
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.1.0",
"jasmine-core": "^3.7.1",
"jest": "^27.0.6",
"karma": "^6.3.4",
"karma-chrome-launcher": "^3.1.0",
"karma-cli": "^2.0.0",
"karma-jasmine": "^4.0.1",
"karma-mocha-reporter": "^2.2.5",
"karma-phantomjs-launcher": "^1.0.4",
"karma-typescript": "^3.0.8",
"karma-typescript-preprocessor": "^0.3.1",
"phantomjs-prebuilt": "^2.1.16",
"puppeteer": "^0.12.0",
"rimraf": "^3.0.0",
"karma-typescript": "^5.5.1",
"karma-typescript-preprocessor": "^0.4.0",
"puppeteer": "^10.1.0",
"rimraf": "^3.0.2",
"ts-helpers": "^1.1.2",
"ts-jest": "^23.0.1",
"tslint": "^5.7.0",
"typescript": "^2.7.2"
"ts-jest": "^27.0.3",
"tslint": "^6.1.3",
"typescript": "^4.3.5"
},
"dependencies": {
"lodash": "^4.17.5"
Expand All @@ -63,7 +64,7 @@
"jest": {
"testEnvironment": "node",
"transform": {
"^.+\\.ts$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.ts$": "ts-jest"
},
"testRegex": "(/__tests__/.*|\\.(spec))\\.(ts|js)$",
"moduleFileExtensions": [
Expand Down
3 changes: 2 additions & 1 deletion src/Mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {strictEqual} from "./ts-mockito";
import {MockableFunctionsFinder} from "./utils/MockableFunctionsFinder";
import {ObjectInspector} from "./utils/ObjectInspector";
import {ObjectPropertyCodeRetriever} from "./utils/ObjectPropertyCodeRetriever";
import { TODO } from "./utils/types";

export class Mocker {
public mock: any = {};
Expand Down Expand Up @@ -208,7 +209,7 @@ export class Mocker {
}

private createMethodToStub(key: string): () => any {
return (...args) => {
return (...args: Array<any>) => {
if (args.length === 1 && args[0] === "__tsMockitoGetInfo") {
return {
key,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/MockableFunctionsFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export class MockableFunctionsFinder {
public find(code: string): string[] {
return (code.match(this.functionNameRegex) || [])
.map((match: string) => match.match(this.cleanFunctionNameRegex)[1])
.filter((functionName: string) => this.isMockable(functionName));
.filter((functionName: string) => this.isMockable(functionName))
.map((functionName: string) => functionName.indexOf('=') > 0 ? functionName.substr(0, functionName.indexOf('=')) : functionName);
}

private isMockable(name: string): boolean {
Expand Down
1 change: 1 addition & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type TODO = any;
9 changes: 4 additions & 5 deletions test/mocking.getter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,15 @@ class FooWithGetterAndSetter extends SampleAbstractClass {
constructor(private dependency: Bar) {
super();
}

public sampleMethod(): number {
return 4;
}

public get twoPlusTwo(): number {
return this.dependency.sumTwoNumbers(2, 2);
}

public set twoPlusTwo(value: number) {
this.dependency.sumTwoNumbers(value, 0);
}

public sampleMethod(): number {
return 4;
}
}
28 changes: 12 additions & 16 deletions test/mocking.types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe("mocking", () => {
// given

// when
mockedFoo = mock(SampleGeneric);
mockedFoo = mock<SampleGeneric<SampleInterface>>(SampleGeneric);
foo = instance(mockedFoo);

// then
Expand All @@ -97,15 +97,15 @@ describe("mocking", () => {
// given

// when
mockedFoo = mock(SampleGeneric);
mockedFoo = mock<SampleGeneric<SampleInterface>>(SampleGeneric);

// then
expect((mockedFoo.twoPlusTwo as any) instanceof MethodToStub).toBe(true);
});

it("allows to mock method with generic return type value (with IDE completion)", () => {
// given
mockedFoo = mock(SampleGeneric);
mockedFoo = mock<SampleGeneric<SampleInterface>>(SampleGeneric);
foo = instance(mockedFoo);
const expectedResult = new SampleInterfaceImplementation();
when(mockedFoo.getGenericTypedValue()).thenReturn(expectedResult);
Expand All @@ -119,7 +119,7 @@ describe("mocking", () => {

it("does create own property descriptors on instance", () => {
// given
mockedFoo = mock(SampleGeneric);
mockedFoo = mock<SampleGeneric<SampleInterface>>(SampleGeneric);
foo = instance(mockedFoo);

// when
Expand All @@ -131,7 +131,7 @@ describe("mocking", () => {

it("does create inherited property descriptors on mock", () => {
// given
mockedFoo = mock(SampleGeneric);
mockedFoo = mock<SampleGeneric<SampleInterface>>(SampleGeneric);
foo = instance(mockedFoo);

// when
Expand All @@ -142,7 +142,7 @@ describe("mocking", () => {

it("does create inherited property descriptors on instance", () => {
// given
mockedFoo = mock(SampleGeneric);
mockedFoo = mock<SampleGeneric<SampleInterface>>(SampleGeneric);
foo = instance(mockedFoo);

// when
Expand Down Expand Up @@ -174,18 +174,16 @@ abstract class SampleAbstractClass {
public get sampleString(): string {
return "sampleString";
}

public sampleMethod(): number {
return 4;
}

public get twoPlusTwo(): number {
return this.dependency.sumTwoNumbers(2, 2);
}

public set twoPlusTwo(value: number) {
this.dependency.sumTwoNumbers(value, 0);
}
public sampleMethod(): number {
return 4;
}
}

class SampleClassWithHasOwnProperty {
Expand Down Expand Up @@ -214,18 +212,16 @@ class SampleGeneric<T> {
public get sampleString(): string {
return "sampleString";
}

public sampleMethod(): number {
return 4;
}

public get twoPlusTwo(): number {
return this.dependency.sumTwoNumbers(2, 2);
}

public set twoPlusTwo(value: number) {
this.dependency.sumTwoNumbers(value, 0);
}
public sampleMethod(): number {
return 4;
}

public getGenericTypedValue(): T {
return null;
Expand Down
8 changes: 3 additions & 5 deletions test/spy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import {capture, reset, spy, verify, when} from "../src/ts-mockito";
describe("spying on a real object", () => {
class Real {
public b = 11;

get baz() {
return 3;
}
public foo(a: number) {
return a;
}

public bar() {
return 2;
}

get baz() {
return 3;
}
}

function RealFn() {
Expand Down

0 comments on commit 25b8f1d

Please sign in to comment.