Skip to content

Commit

Permalink
Merge master in
Browse files Browse the repository at this point in the history
  • Loading branch information
Jameskmonger committed Sep 23, 2016
2 parents a5d4ff5 + 7dc0c17 commit 50bb0c3
Show file tree
Hide file tree
Showing 77 changed files with 3,358 additions and 1,375 deletions.
14 changes: 8 additions & 6 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
node_modules
npm-debug.log

#TypeScript
*.ts
*.js.map
!*.d.ts


#Testing
test
coverage

#CI
.travis.yml
.codeclimate.yml

#DevOps
scripts
102 changes: 101 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# alsatian
[![NPM Version](https://img.shields.io/npm/v/alsatian.svg)](https://www.npmjs.com/package/alsatian)
[![Build Status](https://travis-ci.org/alsatian-test/alsatian.svg?branch=master)](https://travis-ci.org/alsatian-test/alsatian)
[![Code Climate](https://codeclimate.com/github/alsatian-test/alsatian/badges/gpa.svg)](https://codeclimate.com/github/alsatian-test/alsatian)
[![Coverage Status](https://coveralls.io/repos/github/alsatian-test/alsatian/badge.svg?branch=master)](https://coveralls.io/github/alsatian-test/alsatian?branch=master)
[![Issue Count](https://codeclimate.com/github/alsatian-test/alsatian/badges/issue_count.svg)](https://codeclimate.com/github/alsatian-test/alsatian)
[![bitHound Code](https://www.bithound.io/github/alsatian-test/alsatian/badges/code.svg)](https://www.bithound.io/github/alsatian-test/alsatian)
[![bitHound Dependencies](https://www.bithound.io/github/alsatian-test/alsatian/badges/dependencies.svg)](https://www.bithound.io/github/alsatian-test/alsatian/master/dependencies/npm)
[![Known Vulnerabilities](https://snyk.io/test/github/alsatian-test/alsatian/badge.svg)](https://snyk.io/test/github/alsatian-test/alsatian)

TypeScript testing framework with test cases, compatible with istanbul and tap reporters.

Expand All @@ -29,6 +33,19 @@ alsatian [list of globs]
alsatian ./test/**/*.spec.js ./special-test.js
```

### CLI Options

You can change how Alsatian runs your tests using the available options

#### Timeout

The timeout option changes the length of time that a test may run for in milliseconds (the default is 500ms). The examples below will change this to 5000ms.

```
alsatian --timeout 5000
alsatian -t 5000
```

## Using alsatian

Create your first spec file
Expand Down Expand Up @@ -240,12 +257,95 @@ SpyOn(some, "function").andStub();
SpyOn(some, "function").andCall(() => console.log("I are called"));
```

... or make it return whatever you desire
... or make it return whatever you desire ...

```
SpyOn(some, "function").andReturn(42);
```

... and even return it to how it started

```
SpyOn(some, "function");
some.function.restore();
// OR
const spy = SpyOn(some, "function");
spy.restore();
```

#### Creating a spy from thin air

You may want to just create a fake spy property this is easy to do and has all the same functionality as a Spy created through ```SpyOn```

```
import { FunctionSpy } from "alsatian";
const spy = new FunctionSpy();
```

#### Spying on a property

Similarly to spying on functions you can also spy on properties as below ...

```
import { SpyOnProperty } from "alsatian";
class Test {
private _property: number = 42;
get property() {
return this._property;
}
set property(value: number) {
this._property = value;
}
}
const test = new Test();
SpyOnProperty(test, "property");
```

... then add a fake getter ...


```
SpyOnProperty(test, "property").andCallGetter(() => { return "something"; });
```

... or setter ...


```
SpyOnProperty(test, "property").andCallSetter((value: any) => { doSomethingWith(value); });
```

... return a set value ...


```
SpyOnProperty(test, "property").andReturnValue(42);
```

... and restore it to how it was before


```
const spy = SpyOnProperty(test, "property");
spy.restore();
```

### Async Tests

You can also have asynchronous tests using the ```AsyncTest``` annotation,
Expand Down
7 changes: 5 additions & 2 deletions cli/alsatian-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ let userArguments = new AlsatianCliOptions(process.argv.slice(2));
let testSet = createTestSet();
testSet.addTestsFromFiles(userArguments.fileGlobs);

// create alsatian test runner
let testRunner = new TestRunner();

// run the test set
let testRunner = new CliTestRunner();
testRunner.run(testSet, userArguments.timeout);
let cliTestRunner = new CliTestRunner(testRunner);
cliTestRunner.run(testSet, userArguments.timeout);
48 changes: 29 additions & 19 deletions cli/cli-test-runner.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import { TestRunner, TestSet, TestSetResults, TestOutcome } from "../core/alsatian-core";
import { createPromise } from "../promise/create-promise";

export class CliTestRunner extends TestRunner {
export class CliTestRunner {

public run(testSet: TestSet, timeout?: number) {
public constructor(private _testRunner: TestRunner) {
if (!_testRunner) {
throw new TypeError("_testRunner must not be null or undefined.");
}
}

try {
let testRunPromise = super.run(testSet, timeout);
public run(testSet: TestSet, timeout?: number) {

testRunPromise.then((results: TestSetResults) => {
if (results.outcome === TestOutcome.Error || results.outcome === TestOutcome.Fail) {
process.exit(1);
}
else {
process.exit(0);
}
});
}
catch (error) {
process.stderr.write(error.message);
process.exit(1);
}
}
try {
let testRunPromise = this._testRunner.run(testSet, timeout);

testRunPromise.then((results: TestSetResults) => {
if (results.outcome === TestOutcome.Error || results.outcome === TestOutcome.Fail) {
process.exit(1);
}
else {
process.exit(0);
}
})
.catch(this._handleTestSetRunError);
}
catch (error) {
this._handleTestSetRunError(error);
}
}

private _handleTestSetRunError(error: Error) {
process.stderr.write(error.message + "\n");
process.exit(1);
}
}
2 changes: 0 additions & 2 deletions core/_core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Expect } from "./expect";
import { TestRunner } from "./test-runner";
import { TestSet } from "./test-set";
import { TestLoader } from "./test-loader";
import { createTestSet } from "./create-test-set";
Expand All @@ -11,7 +10,6 @@ import { TestOutput } from "./test-output";
export {
createTestSet,
Expect,
TestRunner,
TestSet,
TestLoader,
FileRequirer,
Expand Down
4 changes: 3 additions & 1 deletion core/_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { GreaterThanMatchError } from "./errors/greater-than-match-error";
import { ErrorMatchError } from "./errors/error-match-error";
import { FunctionCallMatchError } from "./errors/function-call-match-error";
import { TestTimeoutError } from "./errors/test-timeout-error";
import { PropertySetMatchError } from "./errors/property-set-match-error";

export {
MatchError,
Expand All @@ -21,5 +22,6 @@ export {
GreaterThanMatchError,
ErrorMatchError,
FunctionCallMatchError,
TestTimeoutError
TestTimeoutError,
PropertySetMatchError
};
15 changes: 8 additions & 7 deletions core/_interfaces/test-fixture.i.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ITest } from "./test.i";

export interface ITestFixture {
fixture: { [id: string]: (...args: Array<any>) => any };
fixture: { [id: string]: (...args: Array<any>) => any };

ignored: boolean;
ignoreReason: string;
ignored: boolean;
ignoreReason: string;

focussed: boolean;
tests: Array<ITest>;
addTest(test: ITest): void;
getTests(): Array<ITest>;
focussed: boolean;
tests: Array<ITest>;

description: string;
addTest(test: ITest): void;
}
11 changes: 11 additions & 0 deletions core/_running.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TestRunner } from "./running/test-runner";
import { TestPlan } from "./running/test-plan";
import { TestItem } from "./running/test-item";
import { TestSetRunInfo } from "./running/test-set-run-info";

export {
TestRunner,
TestPlan,
TestItem,
TestSetRunInfo
};
16 changes: 10 additions & 6 deletions core/_spying.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { PropertySpy } from "./spying/property-spy";
import { SpyCall } from "./spying/spy-call";
import { SpyOn } from "./spying/spy-on";
import { Spy } from "./spying/spy";
import { RestorableSpy } from "./spying/restorable-spy";
import { SpyOnProperty } from "./spying/spy-on-property";
import { FunctionSpy } from "./spying/function-spy";
import { RestorableFunctionSpy } from "./spying/restorable-function-spy";

export {
SpyCall,
SpyOn,
Spy,
RestorableSpy
PropertySpy,
SpyCall,
SpyOn,
SpyOnProperty,
FunctionSpy,
RestorableFunctionSpy
};
55 changes: 31 additions & 24 deletions core/alsatian-core.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,51 @@
import "../typings/index.d.ts";

import {
createTestSet,
Expect,
TestRunner,
TestSet,
TestFixture,
TestOutput
createTestSet,
Expect,
TestSet,
TestFixture,
TestOutput
} from "./_core";

import {
AsyncTest,
FocusTest,
FocusTests,
IgnoreTest,
IgnoreTests,
Setup,
Teardown,
Test,
TestCase,
Timeout
AsyncTest,
FocusTest,
FocusTests,
IgnoreTest,
IgnoreTests,
Setup,
Teardown,
Test,
TestCase,
Timeout
} from "./_decorators";

import {
SpyOn
FunctionSpy,
SpyOn,
SpyOnProperty
} from "./_spying";

import {
TestSetResults,
TestOutcome,
TestFixtureResults,
TestResults,
TestCaseResult
TestSetResults,
TestOutcome,
TestFixtureResults,
TestResults,
TestCaseResult
} from "./_results";

import {
MatchError,
TestTimeoutError
MatchError,
TestTimeoutError
} from "./_errors";

import * as METADATA_KEYS from "./decorators/_metadata-keys";

import {
TestRunner
} from "./_running";

export {
AsyncTest,
createTestSet,
Expand All @@ -50,7 +55,9 @@ export {
IgnoreTest,
IgnoreTests,
Setup,
FunctionSpy,
SpyOn,
SpyOnProperty,
Teardown,
Test,
TestCase,
Expand Down
2 changes: 1 addition & 1 deletion core/decorators/async-test-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "reflect-metadata";
import { TESTS } from "./_metadata-keys";

export function AsyncTest(description?: string) {
return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => any/*Promise<void>*/>) => {
return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<(...args: any[]) => any>) => {

// check if this has been registered as a test already
let tests: Array<any> = Reflect.getMetadata(TESTS, target);
Expand Down
Loading

0 comments on commit 50bb0c3

Please sign in to comment.