Skip to content

Commit

Permalink
Add trivial entrypoint + update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
fsoikin committed Oct 7, 2024
1 parent 1822082 commit a631ddf
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 29 deletions.
18 changes: 11 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## v8.4.0

@srghma Use `spec-node` in tests
## Added

- Use `spec-node` in tests by @srghma
- New trivial entrypoint function `discoverAndRunSpecs` for ergonomics by @fsoikin

## v8.0.1

- @CarstenKoenig Fix discovery on Windows by prepending `file://`
## Changed

- Fix discovery on Windows by prepending `file://` by @CarstenKoenig

## v8.0.0
Features:
- Update to v0.15.0 and support es modules

Breaking changes:
- `discover` now needs `MonadAff` constraint instead of `MonadEffect` due to dynamic imports returning promises
## Changed
- Update to v0.15.0 and support es modules
- **breaking** `discover` now needs `MonadAff` constraint instead of `MonadEffect` due to dynamic imports returning promises
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,35 @@ purescript-spec-discovery is an extension to
[purescript-spec](https://github.com/purescript-spec/purescript-spec) that finds
specs automatically, given a regular expression pattern.

It only works for NodeJS environments, currently.
It only works for NodeJS environments, currently, since it's using NodeJS
facilities to list and load modules.

## Usage

Install via Spago:

```bash
spago install spec-discovery
```

Use as main entry point:

```purescript
module Test.Main where
import Prelude
import Effect (Effect)
import Test.Spec.Discovery (discoverAndRunSpec)
import Test.Spec.Reporter.Console (consoleReporter)
main :: Effect Unit
main = discoverAndRunSpecs [consoleReporter] """My\.Package\..*Spec"""
```

Or, if you need more sophistication, like an alternative config or whatnot, use
the `discover` function to just return a list of specs and then run them in
whatever way you need:

```purescript
module Test.Main where
Expand All @@ -20,21 +41,23 @@ import Effect (Effect)
import Effect.Aff (launchAff_)
import Test.Spec.Discovery (discover)
import Test.Spec.Reporter.Console (consoleReporter)
import Test.Spec.Runner (runSpec)
import Test.Spec.Runner.Node (runSpecAndExitProcess)
import Test.Spec.Runner.Node.Config (defaultConfig)
main :: Effect Unit
main = launchAff_ do
specs <- discover """My\.Package\..*Spec"""
runSpec [consoleReporter] specs
liftEffect $ runSpecAndExitProcess'
{ defaultConfig: defaultConfig { timeout = Nothing }
, parseCLIOptions: true
}
[consoleReporter]
specs
```

All modules that match the regular expression, **and have a definition
`spec :: Spec Unit`**, will be included and run.

## Documentation

Documentation is publised on [Pursuit](https://pursuit.purescript.org/packages/purescript-spec-discovery).

## Contribute

If you have any issues or possible improvements please file them as
Expand Down
2 changes: 1 addition & 1 deletion spago.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package:
name: spec-discovery
publish:
license: MPL-2.0
version: 8.3.0
version: 8.4.0
location:
githubOwner: purescript-spec
githubRepo: purescript-spec-discovery
Expand Down
25 changes: 16 additions & 9 deletions src/Test/Spec/Discovery.purs
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
module Test.Spec.Discovery (discover) where
module Test.Spec.Discovery
( discover
, discoverAndRunSpecs
)
where

import Prelude

import Control.Promise (Promise)
import Control.Promise as Promise
import Data.Traversable (sequence_)
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Aff.Class (class MonadAff, liftAff)
import Effect.Class (liftEffect)
import Test.Spec (Spec)
import Test.Spec.Runner (Reporter)
import Test.Spec.Runner.Node (runSpecAndExitProcess)

foreign import getSpecs
:: String
-> Effect (Promise (Array (Spec Unit)))
foreign import getSpecs :: String -> Effect (Promise (Array (Spec Unit)))

discover
:: forall m
. MonadAff m
=> String
-> m (Spec Unit)
discover :: m. MonadAff m => String -> m (Spec Unit)
discover pattern = getSpecs pattern # Promise.toAffE >>= (pure <<< sequence_) # liftAff

discoverAndRunSpecs :: Array Reporter -> String -> Effect Unit
discoverAndRunSpecs reporters pattern = launchAff_ do
specs <- discover pattern
liftEffect $ runSpecAndExitProcess reporters specs
7 changes: 2 additions & 5 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ module Test.Main where
import Prelude

import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Class (liftEffect)
import Test.Spec.Discovery (discover)
import Test.Spec.Discovery (discoverAndRunSpecs)
import Test.Spec.Reporter.Console (consoleReporter)
import Test.Spec.Runner.Node (runSpecAndExitProcess)

main :: Effect Unit
main = launchAff_ $ discover "Test\\.Spec\\.Discovery.*Spec" >>= (liftEffect <<< runSpecAndExitProcess [consoleReporter])
main = discoverAndRunSpecs [consoleReporter] "Test\\.Spec\\.Discovery.*Spec"

0 comments on commit a631ddf

Please sign in to comment.