Skip to content

Commit

Permalink
.wp-env.json schema: Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
t-hamano committed Jul 24, 2024
1 parent e05fbc2 commit 46e393b
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/integration/wp-env-schema.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* External dependencies
*/
import Ajv from 'ajv';
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-07 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 ) => {

Check failure on line 32 in test/integration/wp-env-schema.test.js

View workflow job for this annotation

GitHub Actions / JavaScript (Node.js 20) 2/4

Error: expect(jest.fn()).not.toHaveWarned(expected) Expected mock function not to be called but it was called with: ["strict mode: missing type \"object\" for keyword \"properties\" at \"#/allOf/1\" (strictTypes)"],["strict mode: missing type \"object\" for keyword \"propertyNames\" at \"#/allOf/2\" (strictTypes)"] console.warn() should not be used unless explicitly expected See https://www.npmjs.com/package/@wordpress/jest-console for details. at Object.assertExpectedCalls (/home/runner/work/gutenberg/gutenberg/packages/jest-console/src/index.js:32:40) at Promise.then.completed (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/utils.js:300:28) at new Promise (<anonymous>) at callAsyncCircusFn (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/utils.js:233:10) at _callCircusHook (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:280:40) at processTicksAndRejections (node:internal/process/task_queues:95:5) at _runTest (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:253:5) at _runTestsForDescribeBlock (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:125:9) at _runTestsForDescribeBlock (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:120:9) at run (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:70:3) at runAndTransformResultsToJestFormat (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21) at jestAdapter (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19) at runTestInternal (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/runTest.js:367:16) at runTest (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/runTest.js:444:34) at Object.worker (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/testWorker.js:106:12)

Check failure on line 32 in test/integration/wp-env-schema.test.js

View workflow job for this annotation

GitHub Actions / JavaScript (Node.js 22) 2/4

Error: expect(jest.fn()).not.toHaveWarned(expected) Expected mock function not to be called but it was called with: ["strict mode: missing type \"object\" for keyword \"properties\" at \"#/allOf/1\" (strictTypes)"],["strict mode: missing type \"object\" for keyword \"propertyNames\" at \"#/allOf/2\" (strictTypes)"] console.warn() should not be used unless explicitly expected See https://www.npmjs.com/package/@wordpress/jest-console for details. at Object.assertExpectedCalls (/home/runner/work/gutenberg/gutenberg/packages/jest-console/src/index.js:32:40) at Promise.then.completed (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/utils.js:300:28) at new Promise (<anonymous>) at callAsyncCircusFn (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/utils.js:233:10) at _callCircusHook (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:280:40) at processTicksAndRejections (node:internal/process/task_queues:95:5) at _runTest (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:253:5) at _runTestsForDescribeBlock (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:125:9) at _runTestsForDescribeBlock (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:120:9) at run (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/run.js:70:3) at runAndTransformResultsToJestFormat (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapterInit.js:122:21) at jestAdapter (/home/runner/work/gutenberg/gutenberg/node_modules/jest-circus/build/legacy-code-todo-rewrite/jestAdapter.js:79:19) at runTestInternal (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/runTest.js:367:16) at runTest (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/runTest.js:444:34) at Object.worker (/home/runner/work/gutenberg/gutenberg/node_modules/jest-runner/build/testWorker.js:106:12)
// 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 );
} );
} );

0 comments on commit 46e393b

Please sign in to comment.