Skip to content

Commit

Permalink
feat: split package exports into matcher and core functionality
Browse files Browse the repository at this point in the history
package.json: Created the entry routes and a separate test script specifically for testing
coordinates. Each category will get its own script for ease of development.
readme: Updated the
configuration and core importing sections.
typedefinitions.js: newly created file solely for JSDoc
type defs.
matchers.js: Exports the matcher functions grouped into objects by category.
core.js:
Exports the core functionality grouped into objects by category.
setup: Moving all setup scripts
into this folder. Creates a separate script for each group as well as an overall script to load
everything.

Resolves: #5
  • Loading branch information
M-Scott-Lassiter committed May 24, 2022
1 parent e5ae40f commit a7340d9
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 29 deletions.
9 changes: 7 additions & 2 deletions .cz-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// When adding new matchers, place them in alphabetical order inside the appropriate category

const coordinateMatchers = [{ name: 'isValid2DCoordinate' }, { name: 'isValid3DCoordinate' }]
const coordinateMatchers = [
{ name: 'isValid2DCoordinate' },
{ name: 'isValid3DCoordinate' },
{ name: 'isValidCoordinate' }
]

const boundingBoxMatchers = []

Expand Down Expand Up @@ -82,6 +86,7 @@ module.exports = {
{ name: 'package' }
],
docs: documentationScopes,
feat: allMatchers,
fix: allMatchers,
perf: allMatchers,
refactor: allMatchers,
Expand All @@ -108,7 +113,7 @@ module.exports = {

// limit subject length
subjectLimit: 100,
// breaklineChar: '|', // It is supported for fields body and footer.
breaklineChar: '|', // It is supported for fields body and footer.
footerPrefix: 'Resolves:'
// askForBreakingChangeFirst : true, // default is false
}
40 changes: 36 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,52 @@ Other projects have done that (and better), such as the venerable [Turf.js](http
npm install --save-dev jest jest-geojson
```

<!-- Future expansion: Add instructions for yarn -->

## Configure Jest

< Under Construction >
Jest [allows you to run scripts](https://jestjs.io/docs/configuration#setupfilesafterenv-array) after its environment loads. You can take advantage of that to load all `jest-geojson` matchers automatically so your test script can make use of them without any further steps.

To do so, either create a `jest.config.js` file:

```javascript
module.exports = {
setupFilesAfterEnv: ['jest-geojson/setup/all']
}
```

or add a key to your `package.json`:

```json
{
"name": "myPackageName",
...
"jest": {
"setupFilesAfterEnv": ["jest-geojson/setup/all"]
}
}
```

<!-- Eventually, add a tutorial for doing custom matcher loading -->

---

## Import the Core Engine

You can directly import the functions driving the test matchers. The core object groups the functions by category.

```javascript
const jest-geojson = require(jest-geojson).core
const jest-geojson = require(jest-geojson/core)
```

You can also import the matcher functions without automatically loading them. These matchers are also grouped by category.

```javascript
const jest-geojson = require(jest-geojson)
```

These are

# Matchers

`jest-geojson` organizes matchers by categories that correspond to the input type passed to `expect()`.
Expand Down Expand Up @@ -246,5 +280,3 @@ Maintained by M. Scott Lassiter.
[![Twitter Badge Profile](https://img.shields.io/badge/Twitter-1DA1F2?style=plastic&logo=twitter&logoColor=white)](https://twitter.com/MScottLassiter)
[![LinkedIn Badge Profile](https://img.shields.io/badge/LinkedIn-0077B5?style=plastic&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/mscottlassiter)
[![Stackoverflow Badge Profile](https://img.shields.io/badge/stackoverflow-orange.svg?longCache=true&style=plastic&logo=stackoverflow&logoColor=white)](https://stackoverflow.com/users/6186333/sandpiper)
README.md
Displaying README.md.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
setupFilesAfterEnv: ['./src/JestSetup.js']
setupFilesAfterEnv: ['./src/setup/all.js']
}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"jest-tests",
"jest-geojson",
"geojson",
"gis",
"matchers",
"jest-matchers",
"test",
Expand All @@ -23,7 +24,11 @@
"package.json",
"package-lock.json"
],
"main": "./src/index.js",
"exports": {
".": "./src/matchers.js",
"./core": "./src/core.js",
"./setup/all": "./src/matchers.js"
},
"repository": {
"type": "git",
"url": "https://github.com/M-Scott-Lassiter/jest-geojson.git"
Expand All @@ -36,6 +41,7 @@
"tableofcontents": "markdown-toc -i ./README.md && markdown-toc -i ./CONTRIBUTING.md",
"test": "jest --coverage --verbose",
"test:dev": "jest --watch --coverage --verbose",
"test-coordinates": "jest tests/coordinates --coverage --verbose",
"lint": "eslint . --ext .js --fix",
"prepare": "husky install"
},
Expand Down
3 changes: 0 additions & 3 deletions src/JestSetup.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable global-require */

exports.coordinates = {
valid2DCoordinate: require('./core/coordinates/valid2DCoordinate'),
valid3DCoordinate: require('./core/coordinates/valid3DCoordinate'),
validCoordinate: require('./core/coordinates/validCoordinate')
}
21 changes: 21 additions & 0 deletions src/matchers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file exports all matchers grouped within an object named after their categories.
// Any new matchers must get added to their individual files.

/* eslint-disable global-require */

// Bounding Boxes

// Coordinates
exports.coordinates = {
isValid2DCoordinate: require('./matchers/coordinates/isValid2DCoordinate').isValid2DCoordinate,
isValid3DCoordinate: require('./matchers/coordinates/isValid3DCoordinate').isValid3DCoordinate,
isValidCoordinate: require('./matchers/coordinates/isValidCoordinate').isValidCoordinate
}

// Features

// Geometries

// Properties

// Winding
18 changes: 18 additions & 0 deletions src/setup/all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const matchers = require('../matchers')

exports.throwJestRuntimeError = () => {
throw new Error(
"Unable to find Jest's global expect.\n\n" +
'Please check you have added jest-geojson correctly to your jest configuration.\n\n' +
'For help, see https://github.com/M-Scott-Lassiter/jest-geojson/tree/beta#configure-jest\n\n'
)
}

const jestExpect = global.expect

if (jestExpect !== undefined) {
expect.extend(matchers.coordinates)
// expect.extend(matchers.boundingBoxes)
} else {
exports.throwJestRuntimeError()
}
10 changes: 10 additions & 0 deletions src/setup/coordinates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const matchers = require('../matchers')
const { throwJestRuntimeError } = require('./all')

const jestExpect = global.expect

if (jestExpect !== undefined) {
expect.extend(matchers.coordinates)
} else {
throwJestRuntimeError()
}
19 changes: 1 addition & 18 deletions src/index.js → src/typedefinitions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable global-require */
/* This is the entry point the jest-geojson package. Example usage in other projects:
import * as matchers from 'jest-extended'
Expand Down Expand Up @@ -46,21 +47,3 @@
* @example
* { pass: true, message: () => 'This test passed' }
*/

// //////////////////// Module Exports Begin Here //////////////////// //

// Bounding Boxes

// Coordinates
exports.isValid2DCoordinate =
require('./matchers/coordinates/isValid2DCoordinate').isValid2DCoordinate
exports.isValid3DCoordinate =
require('./matchers/coordinates/isValid3DCoordinate').isValid3DCoordinate
exports.isValidCoordinate = require('./matchers/coordinates/isValidCoordinate').isValidCoordinate
// Features

// Geometries

// Properties

// Winding
17 changes: 17 additions & 0 deletions tests/core.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// These tests do nothing but verify that the exported objects have all expected functions.

const core = require('../src/core')

describe('Coordinate Functions Exported', () => {
test('valid2DCoordinate', () => {
expect('valid2DCoordinate' in core.coordinates).toBeTruthy()
})

test('valid3DCoordinate', () => {
expect('valid3DCoordinate' in core.coordinates).toBeTruthy()
})

test('validCoordinate', () => {
expect('validCoordinate' in core.coordinates).toBeTruthy()
})
})
17 changes: 17 additions & 0 deletions tests/matchers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// These tests do nothing but verify that the exported objects have all expected functions.

const matchers = require('../src/matchers')

describe('Coordinate Functions Exported', () => {
test('isValid2DCoordinate', () => {
expect('isValid2DCoordinate' in matchers.coordinates).toBeTruthy()
})

test('isValid3DCoordinate', () => {
expect('isValid3DCoordinate' in matchers.coordinates).toBeTruthy()
})

test('isValidCoordinate', () => {
expect('isValidCoordinate' in matchers.coordinates).toBeTruthy()
})
})

0 comments on commit a7340d9

Please sign in to comment.