Skip to content

Commit

Permalink
chore: cleanup, update readme, use jest and npm scripts (#49)
Browse files Browse the repository at this point in the history
## Summary

Just cleaning up a little.

- use jest instead of nodejs test runner
- remove tasks.mjs file and just use npm scripts
- slightly move test files
- update readme
  • Loading branch information
xddq authored May 16, 2024
1 parent 1fc43b9 commit ab48758
Show file tree
Hide file tree
Showing 18 changed files with 3,489 additions and 675 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ TODO
.yarn
generated-typebox.ts
examples
test/integration/schemas/*.ts
test/fixture/*.ts
2 changes: 1 addition & 1 deletion .github/workflows/buildAndTest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
- name: Install Dependencies
run: yarn --immutable
- name: Build
run: yarn build
run: yarn compile
- name: Test
run: npm test
2 changes: 1 addition & 1 deletion .github/workflows/format.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
- name: Install Dependencies
run: yarn --immutable
- name: Format
run: yarn format-check
run: yarn format:check
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
- name: Install Dependencies
run: yarn --immutable
- name: Lint
run: yarn lint-check
run: yarn lint:check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,5 @@ node_modules
TODO*
/schema.json
generated-typebox.ts
.envrc
!examples/**
91 changes: 28 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,61 +169,24 @@ the target TypeBox officially supports. _These types are fully compatible with
the JSON Schema Draft 7 specification._ (from typebox repo 20.08.2023).

However, since the amount of breaking changes is quite small between most JSON
schema specs, support for other specs should "just work". Feel free to open a
discussion or issue when you find problems. Happy about contributions if you
want to help out.

- [x] draft-04
- [x] draft-06
- [x] draft-07 (main goal of this package, see Feature List for the state)
- [x] draft-2019-09
- should be working with the _current feature set_
- [ ] draft-2020-12
- use with caution. Not expected to fully work. See
[here](https://github.com/sinclairzx81/typebox/issues/490)

### Feature List

Tracking the progress/features of `JSON schema -> TypeBox` transformation to see
whats already implemented and what is missing.

- [x] Type.String() via "string" instance type
- [x] Type.Boolean() via "boolean" instance type
- [x] Type.Number() via "number" instance type
- [x] Type.Null() via "null" instance type
- [x] Type.Array() via "array" instance type
- [x] Type.Object() via "object" instance type
- [x] Type.Literal() via "const" property
- [x] Type.Union() via "anyOf" or "enum" property
- schema2typebox generates union types instead of enums. If you have a problem
with this behaviour and valid arguments for using enums please create an
issue and it may be considered again.
- [x] Type.Union() via a list of types given by the 'type' instance type (e.g.
type ["string","null"]
- [x] Type.Intersect() via "allOf" property
- [x] OneOf() via "oneOf" property
- This adds oneOf to the typebox type registry as (Kind: 'ExtendedOneOf') in
order to be able to align to oneOf json schema semantics and still be able
to use the typebox compiler. [More
info](https://github.com/xddq/schema2typebox/issues/16).
- [x] Type.Not() via "not" property
- [x] Type.Unknown() for objects without properties
- [x] Full support for schema options (e.g. minLength: 1, description: "test
entity").
- [x] $refs anywhere using [@apidevtools/json-schema-ref-parser](https://github.com/APIDevTools/json-schema-ref-parser)
- [ ] (low prio) Type.Tuple() via "array" instance type with minimalItems,
maximalItems and additionalItems false
schema specs, support for other specs (draft-04, draft-06, draft-2019-09) should
"just work". Feel free to open a discussion or issue when you find problems.
Happy about contributions if you want to help out. Draft-2020 info can be found
[here](https://github.com/sinclairzx81/typebox/issues/490) not expected to fully
work.

## DEV/CONTRIBUTOR NOTES

- If you have an idea or want to help implement something, feel free to do so.
Please always start by creating a discussion post to avoid any unnecessary
work.
- Please always create tests for new features that are implemented. This will
decrease mental overhead for reviewing and developing in the long run.
- See specification for JSON schema draft-07
[here](https://json-schema.org/specification-links.html#draft-7). The meta
schema is also stored inside this repo under ./meta-schema-draft-07.json.
If you have an idea or want to help implement something, feel free to do so.
Please always start by creating an issue to avoid any unnecessary work on
either side.

Please always create tests for new features that are implemented. This will
decrease mental overhead for reviewing and developing in the long run.

To understand the JSON schema draft-07 you can check json-schema.org
[here](https://json-schema.org/specification-links.html#draft-7). The meta
schema can be found [here](https://json-schema.org/draft-07/schema).

## cli usage

Expand Down Expand Up @@ -261,19 +224,21 @@ The following text is the output that will be displayed when you issue

### Code coverage

This project aims for high code coverage. When you add new features or fix a
bug, please add a test for it. This is what I could get out of the [experimental
code coverage](https://nodejs.org/api/test.html#collecting-code-coverage) from
node test runner in v20.03.1. Was run with code from 28.06.2023.
This project aims for a high code coverage. When you add new features or fix a
bug, please add an according test for it. The current output (17.05.2024) looks
like this:

| File | Line % | Branch % | Funcs % |
| ------------------------------ | ------ | -------- | ------- |
| dist/src/programmatic-usage.js | 95.83 | 53.33 | 80.00 |
| dist/src/schema-to-typebox.js | 92.06 | 86.54 | 94.74 |
| File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
| --------------------- | ------- | -------- | ------- | ------- | ---------------------------- |
| All files | 85.86 | 75 | 96.87 | 85.86 |
| index.ts | 100 | 100 | 100 | 100 |
| programmatic-usage.ts | 100 | 66.66 | 100 | 100 | 30 |
| schema-matchers.ts | 97.05 | 100 | 90.9 | 97.05 | 63-64 |
| schema-to-typebox.ts | 81.4 | 72.64 | 100 | 81.4 | ...0,239,259,309-324,345-346 |

While I enjoy using the test runner from nodejs itself,
this feature is still lacking.
You can inspect the code coverage in depth by running `npx http-server
./coverage/lcov-report` and then browsing http://localhost:8080.

### Template Repo

Template for the repo setup was taken from [here](https://github.com/xddq/nodejs-typescript-modern-starter).
Template for this repo was taken from [here](https://github.com/xddq/nodejs-typescript-modern-starter).
12 changes: 12 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
presets: [
["@babel/preset-env", { targets: { node: "current" } }],
"@babel/preset-typescript",
],
// required for easily mocking module exports.
// see: https://stackoverflow.com/questions/67872622/jest-spyon-not-working-on-index-file-cannot-redefine-property
assumptions: {
constantReexports: true,
},
plugins: [],
};
36 changes: 36 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//@ts-check
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/

/** @type {import('jest').Config} */
const config = {
// Automatically clear mock calls, instances, contexts and results before every test
clearMocks: true,
// Indicates whether the coverage information should be collected while executing the test
collectCoverage: true,
coverageReporters: ["lcov", "text"],
collectCoverageFrom: [
"src/*.ts",
"!**/node_modules/**",
"!**/dist/**",
"!**/*.(spec|test|d).ts",
],
// The directory where Jest should output its coverage files
coverageDirectory: "coverage",
// Indicates which provider should be used to instrument code for coverage
coverageProvider: "v8",
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
// Indicates whether each individual test should be reported during the run
verbose: true,
testMatch: [
"**/test/**/*.[jt]s?(x)",
"!**/test/util/**/*.[jt]s?(x)",
"!**/test/fixture/**/*.[jt]s?(x)",
"**/?(*.)+(spec|test).[tj]s?(x)",
],
};

module.exports = config;
33 changes: 16 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,16 @@
"author": "Pierre Dahmani <hi@pierre-dev.com>",
"license": "MIT",
"scripts": {
"build": "node tasks.mjs build",
"bundle": "node tasks.mjs bundle",
"clean": "node tasks.mjs clean",
"dev": "concurrently \"npm:watch-*\"",
"format": "node tasks.mjs format --write",
"format-check": "node tasks.mjs format --check",
"gen-dummy": "node dist/src/generate-dummy-schema.js",
"lint": "node tasks.mjs lint --fix",
"lint-check": "node tasks.mjs lint",
"start": "node tasks.mjs start",
"test": "node tasks.mjs test",
"watch-node": "node tasks.mjs watch-node",
"watch-ts": "node tasks.mjs watch-ts",
"release": "yarn build && npm publish"
"compile": "tsc",
"compile:watch": "tsc -w",
"dev": "nodemon --watch src --watch test --ext ts,json --exec 'yarn compile && yarn start'",
"format": "prettier . --write",
"format:check": "prettier . --check",
"lint": "eslint . --fix",
"lint:check": "eslint .",
"start": "node --enable-source-maps ./dist/src/index.js",
"test": "jest --runInBand .",
"release": "rm -r dist && yarn compile && npm publish"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -57,20 +53,23 @@
"typescript": "5.0.4"
},
"devDependencies": {
"@babel/core": "7.24.5",
"@babel/preset-env": "7.24.5",
"@babel/preset-typescript": "7.24.1",
"@jest/globals": "29.7.0",
"@tsconfig/node18": "2.0.0",
"@types/chai": "4.3.5",
"@types/jest": "29.5.12",
"@types/minimist": "1.2.2",
"@types/node": "18.16.8",
"@types/nodemon": "1.19.2",
"@types/prettier": "2.7.2",
"@types/shelljs": "0.8.12",
"@typescript-eslint/eslint-plugin": "5.59.6",
"@typescript-eslint/parser": "5.59.6",
"chai": "4.3.7",
"concurrently": "7.6.0",
"esbuild": "0.17.18",
"eslint": "8.40.0",
"eslint-config-prettier": "8.8.0",
"jest": "29.7.0",
"nodemon": "2.0.22",
"shelljs": "0.8.5"
},
Expand Down
Loading

0 comments on commit ab48758

Please sign in to comment.