-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.wp-env.json schema: Make the schema more strict and add unit tests
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import Ajv from 'ajv-draft-04'; | ||
import glob from 'fast-glob'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import wpEnvSchema from '../../schemas/json/wp-env.json'; | ||
|
||
describe( '.wp-env.json schema', () => { | ||
const jsonFiles = glob.sync( [ '.wp-env.json' ], { onlyFiles: true } ); | ||
const ajv = new Ajv( { | ||
allowMatchingProperties: true, | ||
} ); | ||
|
||
test( 'strictly adheres to the draft-04 meta schema', () => { | ||
// Use ajv.compile instead of ajv.validateSchema to validate the schema | ||
// because validateSchema only checks syntax, whereas, compile checks | ||
// if the schema is semantically correct with strict mode. | ||
// See https://github.com/ajv-validator/ajv/issues/1434#issuecomment-822982571 | ||
const result = ajv.compile( wpEnvSchema ); | ||
|
||
expect( result.errors ).toBe( null ); | ||
} ); | ||
|
||
test( 'found .wp-env.json files', () => { | ||
expect( jsonFiles.length ).toBeGreaterThan( 0 ); | ||
} ); | ||
|
||
test.each( jsonFiles )( 'validates schema for `%s`', ( filepath ) => { | ||
// We want to validate the .wp-env.json file using the local schema. | ||
const { $schema, ...metadata } = require( filepath ); | ||
|
||
// we expect the $schema property to be present in the .wp-env.json file | ||
expect( $schema ).toBeTruthy(); | ||
|
||
const result = ajv.validate( wpEnvSchema, metadata ) || ajv.errors; | ||
|
||
expect( result ).toBe( true ); | ||
} ); | ||
} ); |