Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Commit

Permalink
Build: Improve coverage by adding more tests (#3)
Browse files Browse the repository at this point in the history
* Add tests for isTrue and isFalse

* Add sinon bindings

* Add more tests

* Make assertions async

* Add more tests and remove regex assert
  • Loading branch information
Alxandr committed Jun 1, 2017
1 parent a6da72c commit 946481b
Show file tree
Hide file tree
Showing 14 changed files with 790 additions and 93 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ coverage/
[Dd]ebug/
[Rr]elease/
x64/
[Bb]in/
[Bb]in/*
[Oo]bj/

# Snapshots
![Bb]in/**/__snapshots__/*

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
Expand Down
19 changes: 19 additions & 0 deletions bin/js/test/Fable.Ava.Test/__snapshots__/spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`snapshot 1`] = `
Object {
"kind": "snapshot",
"num": 1,
"test": true,
"with": "ava",
}
`;

exports[`snapshot 2`] = `
Object {
"kind": "snapshot",
"num": 1,
"test": true,
"with": "ava",
}
`;
7 changes: 7 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const babel = require('babel-core');
const client = require('fable-utils/client');
const babelPlugins = require('fable-utils/babel-plugins');
const istanbul = require('babel-plugin-istanbul').default;
const sourcemap = require('convert-source-map');
const pkg = require('./package.json');

const instrument = process.env.INSTRUMENT_CODE;
Expand Down Expand Up @@ -89,6 +90,12 @@ const main = async (argv) => {

const outFile = path.join(outDir, replaceExt(relPath, '.js'));
const outFileDir = path.dirname(outFile);
if (transformed.map) {
const relSrcPath = path.relative(outFileDir, fsFile);
const map = sourcemap.fromObject(transformed.map).setProperty('sources', [relSrcPath]);
transformed.code += '\n\n' + map.toComment() + '\n';
}

await fs.mkdirp(outFileDir);
await fs.writeFile(outFile, transformed.code, { encoding: 'utf-8' });
} catch (e) {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
"babel-plugin-istanbul": "^4.1.3",
"babel-preset-env": "^1.5.1",
"codecov": "^2.2.0",
"convert-source-map": "^1.5.0",
"fable-utils": "^1.0.0",
"fs-extra": "^3.0.1",
"glob": "^7.1.2",
"mkdirp": "^0.5.1",
"nyc": "^10.3.2"
"nyc": "^10.3.2",
"sinon": "^2.3.1"
},
"scripts": {
"test": "nyc ava bin/js/test/**/*.js",
Expand Down
99 changes: 51 additions & 48 deletions src/Fable.Ava/assert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,64 @@ namespace Fable.Ava
open Fable.Core
open Fable.Import.Ava

type Asserter = {
pass: string option -> unit
fail: string option -> unit
isTruthy: obj -> string option -> unit
isFalsy: obj -> string option -> unit
isTrue: bool -> string option -> unit
isFalse: bool -> string option -> unit
isSame: obj -> obj -> string option -> unit
isNotSame: obj -> obj -> string option -> unit
isDeepEqual: obj -> obj -> string option -> unit
isNotDeepEqual: obj -> obj -> string option -> unit
throws: (unit -> unit) -> string option -> unit
doesNotThrow: (unit -> unit) -> string option -> unit
asyncThrows: (unit -> Async<unit>) -> string option -> Async<unit>
doesNotAsyncThrow: (unit -> Async<unit>) -> string option -> Async<unit>
//matches: string -> System.Text.RegularExpressions.Regex -> string option -> unit
ifError: obj -> string option -> unit
snapshot: obj -> string option -> unit
}

module Assert =
type Assert<'t> =
type Assert =
| Pass of message: string option
| Fail of message: string option
| Truthy of value: 't * message: string option
| Falsy of value: 't * message: string option
| Truthy of value: obj * message: string option
| Falsy of value: obj * message: string option
| True of value: bool * message: string option
| False of value: bool * message: string option
| Is of value: 't * expected: 't * message: string option
| Not of value: 't * expected: 't * message: string option
| DeepEqual of value: 't * expected: 't * message: string option
| NotDeepEqual of value: 't * expected: 't * message: string option
| Is of value: obj * expected: obj * message: string option
| Not of value: obj * expected: obj * message: string option
| DeepEqual of value: obj * expected: obj * message: string option
| NotDeepEqual of value: obj * expected: obj * message: string option
| Throws of fn: (unit -> unit) * message: string option
| NotThrows of fn: (unit -> unit) * message: string option
| AsyncThrows of fn: (unit -> Async<unit>) * message: string option
| NotAsyncThrows of fn: (unit -> Async<unit>) * message: string option
| Regex of value: string * regex: Fable.Import.JS.RegExp * message: string option
//| Regex of value: string * regex: System.Text.RegularExpressions.Regex * message: string option
| IfError of error: obj * message: string option
| Snapshot of value: obj * message: string option

let internal run (t: IAsserter) = function
| Pass None -> t.Pass ()
| Pass (Some s) -> t.Pass s
| Fail None -> t.Fail ()
| Fail (Some s) -> t.Fail s
| Truthy (v, None) -> t.Truthy v
| Truthy (v, Some s) -> t.Truthy (v, s)
| Falsy (v, None) -> t.Falsy v
| Falsy (v, Some s) -> t.Falsy (v, s)
| True (v, None) -> t.True v
| True (v, Some s) -> t.True (v, s)
| False (v, None) -> t.False v
| False (v, Some s) -> t.False (v, s)
| Is (v, e, None) -> t.Is (v, e)
| Is (v, e, Some s) -> t.Is (v, e, s)
| Not (v, e, None) -> t.Not (v, e)
| Not (v, e, Some s) -> t.Not (v, e, s)
| DeepEqual (v, e, None) -> t.DeepEqual (v, e)
| DeepEqual (v, e, Some s) -> t.DeepEqual (v, e, s)
| NotDeepEqual (v, e, None) -> t.NotDeepEqual (v, e)
| NotDeepEqual (v, e, Some s) -> t.NotDeepEqual (v, e, s)
| Throws (f, None) -> t.Throws f
| Throws (f, Some s) -> t.Throws (f, s)
| NotThrows (f, None) -> t.NotThrows f
| NotThrows (f, Some s) -> t.NotThrows (f, s)
| AsyncThrows (f, None) -> t.Throws (f >> Async.StartAsPromise)
| AsyncThrows (f, Some s) -> t.Throws (f >> Async.StartAsPromise, s)
| NotAsyncThrows (f, None) -> t.NotThrows (f >> Async.StartAsPromise)
| NotAsyncThrows (f, Some s) -> t.NotThrows (f >> Async.StartAsPromise, s)
| Regex (v, r, None) -> t.Regex (v, r)
| Regex (v, r, Some s) -> t.Regex (v, r, s)
| IfError (e, None) -> t.IfError e
| IfError (e, Some s) -> t.IfError (e, s)
| Snapshot (v, None) -> t.Snapshot v
| Snapshot (v, Some s) -> t.Snapshot (v, s)
let run (a: Asserter) = function
| Pass (s) -> a.pass s; async.Return ()
| Fail (s) -> a.fail s; async.Return ()
| Truthy (v, s) -> a.isTruthy v s; async.Return ()
| Falsy (v, s) -> a.isFalsy v s; async.Return ()
| True (v, s) -> a.isTrue v s; async.Return ()
| False (v, s) -> a.isFalse v s; async.Return ()
| Is (v, e, s) -> a.isSame v e s; async.Return ()
| Not (v, e, s) -> a.isNotSame v e s; async.Return ()
| DeepEqual (v, e, s) -> a.isDeepEqual v e s; async.Return ()
| NotDeepEqual (v, e, s) -> a.isNotDeepEqual v e s; async.Return ()
| Throws (f, s) -> a.throws f s; async.Return ()
| NotThrows (f, s) -> a.doesNotThrow f s; async.Return ()
| AsyncThrows (f, s) -> a.asyncThrows f s
| NotAsyncThrows (f, s) -> a.doesNotAsyncThrow f s
//| Regex (v, r, s) -> a.matches v r s; async.Return ()
| IfError (e, s) -> a.ifError e s; async.Return ()
| Snapshot (v, s) -> a.snapshot v s; async.Return ()

let internal setMessage msg = function
| Pass (Some s)
Expand All @@ -74,7 +77,7 @@ module Assert =
| NotThrows (_, Some s)
| AsyncThrows (_, Some s)
| NotAsyncThrows (_, Some s)
| Regex (_, _, Some s)
//| Regex (_, _, Some s)
| IfError (_, Some s)
| Snapshot (_, Some s) -> failwithf "Message already set: %s" s
| Pass (None) -> Pass (Some msg)
Expand All @@ -91,7 +94,7 @@ module Assert =
| NotThrows (f, None) -> NotThrows (f, Some msg)
| AsyncThrows (f, None) -> AsyncThrows (f, Some msg)
| NotAsyncThrows (f, None) -> NotAsyncThrows (f, Some msg)
| Regex (v, r, None) -> Regex (v, r, Some msg)
//| Regex (v, r, None) -> Regex (v, r, Some msg)
| IfError (e, None) -> IfError (e, Some msg)
| Snapshot (v, None) -> Snapshot (v, Some msg)

Expand All @@ -106,10 +109,10 @@ module Assert =
let isDeepEqual v e = DeepEqual (v, e, None)
let isNotDeepEqual v e = NotDeepEqual (v, e, None)
let throws f = Throws (f, None)
let notThrows f = NotThrows (f, None)
let doesNotThrow f = NotThrows (f, None)
let asyncThrows f = AsyncThrows (f, None)
let notAsyncThrows f = NotAsyncThrows (f, None)
let matches v r = Regex (v, r, None)
let doesNotAsyncThrow f = NotAsyncThrows (f, None)
//let matches v r = Regex (v, r, None)
let ifError e = IfError (e, None)
let snapshot v = Snapshot (v, None)

Expand Down
51 changes: 26 additions & 25 deletions src/Fable.Ava/bindings.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,47 @@ module Fable.Import.Ava
open Fable.Core
open Fable.Import.JS

type IAsserter =
[<Sealed; Erase>]
type TestContext internal () =
[<Emit("$0.plan($1)")>]
member x.Plan (n: int): unit = jsNative
[<Emit("$0.pass({{$1}})")>]
abstract Pass: ?msg: string -> unit
member x.Pass (?msg: string): unit = jsNative
[<Emit("$0.fail({{$1}})")>]
abstract Fail: ?msg: string -> unit
member x.Fail (?msg: string): unit = jsNative
[<Emit("$0.truthy($1,{{$2}})")>]
abstract Truthy: value: 'a * ?msg: string -> unit
member x.Truthy (value: obj, ?msg: string): unit = jsNative
[<Emit("$0.falsy($1,{{$2}})")>]
abstract Falsy: value: 'a * ?msg: string -> unit
member x.Falsy (value: obj, ?msg: string): unit = jsNative
[<Emit("$0.true($1,{{$2}})")>]
abstract True: value: bool * ?msg: string -> unit
member x.True (value: bool, ?msg: string): unit = jsNative
[<Emit("$0.false($1,{{$2}})")>]
abstract False: value: bool * ?msg: string -> unit
member x.False (value: bool, ?msg: string): unit = jsNative
[<Emit("$0.is($1,$2,{{$3}})")>]
abstract Is: value: 'a * expected: 'a * ?msg: string -> unit
member x.Is (value: 'a, expected: 'a, ?msg: string): unit = jsNative
[<Emit("$0.not($1,$2,{{$3}})")>]
abstract Not: value: 'a * expected: 'a * ?msg: string -> unit
member x.Not (value: 'a, expected: 'a, ?msg: string): unit = jsNative
[<Emit("$0.deepEqual($1,$2,{{$3}})")>]
abstract DeepEqual: value: 'a * expected: 'a * ?msg: string -> unit
member x.DeepEqual (value: 'a, expected: 'a, ?msg: string): unit = jsNative
[<Emit("$0.notDeepEqual($1,$2,{{$3}})")>]
abstract NotDeepEqual: value: 'a * expected: 'a * ?msg: string -> unit
member x.NotDeepEqual (value: 'a, expected: 'a, ?msg: string): unit = jsNative
[<Emit("$0.throws($1,null,{{$2}})")>]
abstract Throws: fn: (unit -> unit) * ?msg: string -> unit
member x.Throws (fn: (unit -> unit), ?msg: string): unit = jsNative
[<Emit("$0.throws($1,null,{{$2}})")>]
abstract Throws: fn: (unit -> Promise<unit>) * ?msg: string -> unit
[<Emit("$0.notthrows($1,{{$2}})")>]
abstract NotThrows: fn: (unit -> unit) * ?msg: string -> unit
[<Emit("$0.notthrows($1,{{$2}})")>]
abstract NotThrows: fn: (unit -> Promise<unit>) * ?msg: string -> unit
[<Emit("$0.regex($1,$2,{{$3}})")>]
abstract Regex: value: string * test: RegExp * ?msg: string -> unit
member x.Throws (promise: Promise<unit>, ?msg: string): Promise<unit> = jsNative
[<Emit("$0.notThrows($1,{{$2}})")>]
member x.NotThrows (fn: (unit -> unit), ?msg: string): unit = jsNative
[<Emit("$0.notThrows($1,{{$2}})")>]
member x.NotThrows (promise: Promise<unit>, ?msg: string): Promise<unit> = jsNative
// TODO: Bug weird bug with regex
//[<Emit("$0.regex($1,$2,{{$3}})")>]
//member x.Regex (value: string, test: System.Text.RegularExpressions.Regex, ?msg: string): unit = jsNative
[<Emit("$0.ifError($1,{{$2}})")>]
abstract IfError: error: obj * ?msg: string -> unit
member x.IfError (error: obj, ?msg: string): unit = jsNative
[<Emit("$0.snapshot($1,{{$2}})")>]
abstract Snapshot: error: obj * ?msg: string -> unit
member x.Snapshot (error: obj, ?msg: string): unit = jsNative

type ITestContext =
inherit IAsserter

type TestImpl = ITestContext -> Promise<unit>
type TestImpl = TestContext -> Promise<unit>

//[<Import("default", from="ava")>]
module Test =
Expand Down
Loading

0 comments on commit 946481b

Please sign in to comment.