Adapt to new Aff v4.

This commit is contained in:
Oskar Wickström 2017-10-04 08:00:29 +02:00
parent a766d7bf8c
commit 740064f182
4 changed files with 55 additions and 35 deletions

View File

@ -1,6 +1,5 @@
{
"name": "purescript-spec-mocha",
"version": "0.2.0",
"license": "MIT",
"repository": {
"type": "git",
@ -16,7 +15,9 @@
"output"
],
"dependencies": {
"purescript-console": "^3.0.0",
"purescript-foldable-traversable": "^3.0.0",
"purescript-spec": "^1.0.0"
"purescript-exceptions": "^3.0.0",
"purescript-spec": "^2.0.0"
}
}

View File

@ -1,4 +1,4 @@
/* global exports, it */
/* global exports, it, describe */
// module Test.Spec.Mocha
@ -9,15 +9,17 @@ if (typeof describe !== 'function' || typeof it !== 'function') {
exports.itAsync = function (only) {
"use strict";
return function (name) {
return function (aff) {
return function (run) {
return function () {
var f = only ? it.only : it;
f(name, function (done) {
aff(function () {
return run(function () {
done();
}, function (err) {
return function () {};
})(function (err) {
done(err);
});
return function () {};
})();
});
};
};
@ -36,7 +38,7 @@ exports.describe = function (only) {
return function (name) {
return function (nested) {
return function () {
var f = only ? describe : describe.only;
var f = only ? describe.only : describe;
f(name, function () {
nested();
});

View File

@ -4,38 +4,50 @@ module Test.Spec.Mocha (
) where
import Prelude
import Control.Monad.Aff (Aff())
import Control.Monad.Eff (kind Effect, Eff())
import Data.Foldable (traverse_)
import Test.Spec (Spec, Group(..), collect)
import Control.Monad.Aff (Aff, Error, runAff_)
import Control.Monad.Eff (Eff, kind Effect)
import Data.Either (either)
import Data.Foldable (traverse_)
import Test.Spec (Spec, Group(..), collect)
foreign import data MOCHA :: Effect
foreign import itAsync :: forall e.
Boolean
-> String
-> Aff e Unit
-> Eff (mocha :: MOCHA | e) Unit
foreign import itAsync
:: forall e
. Boolean
-> String
-> (Eff e Unit
-> (Error -> Eff e Unit)
-> Eff e Unit)
-> Eff (mocha :: MOCHA | e) Unit
foreign import itPending :: forall e. String
-> Eff (mocha :: MOCHA | e) Unit
foreign import itPending
:: forall e
. String
-> Eff (mocha :: MOCHA | e) Unit
foreign import describe :: forall e.
Boolean
-> String
-> Eff (mocha :: MOCHA | e) Unit
-> Eff (mocha :: MOCHA | e) Unit
foreign import describe
:: forall e
. Boolean
-> String
-> Eff (mocha :: MOCHA | e) Unit
-> Eff (mocha :: MOCHA | e) Unit
registerGroup :: forall e. (Group (Aff e Unit))
-> Eff (mocha :: MOCHA | e) Unit
registerGroup (It only name test) = itAsync only name test
registerGroup
:: forall e
. (Group (Aff e Unit))
-> Eff (mocha :: MOCHA | e) Unit
registerGroup (It only name test) =
itAsync only name cb
where
cb onSuccess onError =
runAff_ (either onError (const onSuccess)) test
registerGroup (Pending name) = itPending name
registerGroup (Describe only name groups) =
describe only name (traverse_ registerGroup groups)
runMocha :: forall e. Spec e Unit
-> Eff (mocha :: MOCHA | e) Unit
runMocha
:: forall e
. Spec e Unit
-> Eff (mocha :: MOCHA | e) Unit
runMocha spec = traverse_ registerGroup (collect spec)

View File

@ -1,8 +1,9 @@
module Test.Main where
import Prelude
import Control.Monad.Aff (later')
import Control.Monad.Aff (delay)
import Control.Monad.Eff (Eff)
import Data.Time.Duration (Milliseconds(..))
import Test.Spec (SpecEffects, describe, it, pending)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.Mocha (MOCHA, runMocha)
@ -21,8 +22,12 @@ main = runMocha do
describe "async" do
it "works" $ do
expected <- later' 1000 (pure 2)
expected <- do
delay (Milliseconds 1.0)
pure 2
(1 + 1) `shouldEqual` expected
it "and can fail" do
expected <- later' 1000 (pure 3)
expected <- do
delay (Milliseconds 1.0)
pure 3
(1 + 1) `shouldEqual` expected