From 1ac057ea470432714ea97d64f3474ca25e07d3ac Mon Sep 17 00:00:00 2001 From: Iain Collins Date: Sun, 8 Mar 2020 22:07:09 +0000 Subject: [PATCH] Fix example usage in README * Addresses issue #15 * Added examples of usage --- README.md | 48 ++++++++++++++++++++-------------- examples/test-multiple-urls.js | 41 +++++++++++++++++++++++++++++ examples/test-url.js | 39 +++++++++++++++++++++++++++ index.js | 6 +++-- package-lock.json | 2 +- 5 files changed, 114 insertions(+), 22 deletions(-) create mode 100644 examples/test-multiple-urls.js create mode 100644 examples/test-url.js diff --git a/README.md b/README.md index 178d070..944c726 100644 --- a/README.md +++ b/README.md @@ -195,32 +195,42 @@ You can integrate Structured Data Testing Tool with a CD/CI pipeline by using th ```javascript const { structuredDataTest } = require('structured-data-testing-tool') -const { ReportageNewsArticle, Twitter, Facebook } = require('structured-data-testing-tool/presets') +const { Google, Twitter, Facebook } = require('structured-data-testing-tool/presets') const url = 'https://www.bbc.co.uk/news/world-us-canada-49060410' - -structuredDataTest(url, { presets: [ ReportageNewsArticle, Twitter, Facebook ] }) + +let result + +structuredDataTest(url, { + // Check for compliance with Google, Twitter and Facebook recommendations + presets: [ Google, Twitter, Facebook ], + // Check the page includes a specific Schema (see https://schema.org/docs/full.html for a list) + schemas: [ 'ReportageNewsArticle' ] +}) .then(res => { - // If you end up here, then there were no errors - console.log("All tests passed.") - console.log('Passed:',res.passed.length) - console.log('Failed:',res.failed.length) - console.log('Warnings:',res.warnings.length) + console.log('✅ All tests passed!') + result = res }) .catch(err => { - // If any test fails, the promise is rejected if (err.type === 'VALIDATION_FAILED') { - console.log("Some tests failed.") - console.log('Passed:',err.res.passed.length) - console.log('Failed:',err.res.failed.length) - console.log('Warnings:',err.res.warnings.length) - // Loop over validation errors - err.res.failed.forEach(test => { - console.error(test) - }) + console.log('❌ Some tests failed.') + result = err.res } else { - // Handle other errors here (e.g. an error fetching a URL) - console.log(err) + console.log(err) // Handle other errors here (e.g. an error fetching a URL) + } +}) +.finally(() => { + if (result) { + console.log( + `Passed: ${result.passed.length},`, + `Failed: ${result.failed.length},`, + `Warnings: ${result.warnings.length}`, + ) + console.log(`Schemas found: ${result.schemas.join(',')}`) + + // Loop over validation errors + if (result.failed.length > 0) + console.log("⚠️ Errors:\n", result.failed.map(test => test)) } }) ``` diff --git a/examples/test-multiple-urls.js b/examples/test-multiple-urls.js new file mode 100644 index 0000000..cfd2347 --- /dev/null +++ b/examples/test-multiple-urls.js @@ -0,0 +1,41 @@ +const { structuredDataTest } = require('structured-data-testing-tool') +const { Google, Twitter, Facebook } = require('structured-data-testing-tool/presets') + +const urls = [ + 'https://www.bbc.co.uk/news/world-us-canada-49060410', + 'https://www.nytimes.com/2020/03/08/world/coronavirus-news.html', + 'https://www.ft.com/content/75583754-5fab-11ea-b0ab-339c2307bcd4', + 'https://news.sky.com/story/coronavirus-italy-sees-largest-daily-rise-in-deaths-and-confirmed-cases-11953192' +] + +urls.forEach(url => { + let result + structuredDataTest(url, { + presets: [ Google, Twitter, Facebook ], + }) + .then(res => { + console.log(`✅ ${url}`) + result = res + }) + .catch(err => { + if (err.type === 'VALIDATION_FAILED') { + console.log(`❌ ${url}`) + result = err.res + } else { + console.log(err) // Handle other errors here (e.g. an error fetching a URL) + } + }) + .finally(() => { + if (result) { + console.log( + ` Passed: ${result.passed.length},`, + ` Failed: ${result.failed.length},`, + ` Warnings: ${result.warnings.length}`, + ) + console.log(` Schemas found: ${result.schemas.join(',')}`) + + // Loop over validation errors + //if (result.failed.length > 0) console.log("⚠️ Errors:\n", result.failed.map(test => test)) + } + }) +}) \ No newline at end of file diff --git a/examples/test-url.js b/examples/test-url.js new file mode 100644 index 0000000..90469a8 --- /dev/null +++ b/examples/test-url.js @@ -0,0 +1,39 @@ +const { structuredDataTest } = require('structured-data-testing-tool') +const { Google, Twitter, Facebook } = require('structured-data-testing-tool/presets') + +const url = 'https://www.bbc.co.uk/news/world-us-canada-49060410' + +let result + +structuredDataTest(url, { + // Check for compliance with Google, Twitter and Facebook recommendations + presets: [ Google, Twitter, Facebook ], + // Check the page includes a specific Schema (see https://schema.org/docs/full.html for a list) + schemas: [ 'ReportageNewsArticle' ] +}) +.then(res => { + console.log('✅ All tests passed!') + result = res +}) +.catch(err => { + if (err.type === 'VALIDATION_FAILED') { + console.log('❌ Some tests failed.') + result = err.res + } else { + console.log(err) // Handle other errors here (e.g. an error fetching a URL) + } +}) +.finally(() => { + if (result) { + console.log( + `Passed: ${result.passed.length},`, + `Failed: ${result.failed.length},`, + `Warnings: ${result.warnings.length}`, + ) + console.log(`Schemas found: ${result.schemas.join(',')}`) + + // Loop over validation errors + if (result.failed.length > 0) + console.log("⚠️ Errors:\n", result.failed.map(test => test)) + } +}) \ No newline at end of file diff --git a/index.js b/index.js index 99f5b0c..1e7bc7e 100644 --- a/index.js +++ b/index.js @@ -140,9 +140,11 @@ const _structuredDataTest = (structuredData, options) => { // This is a recursive function scoped to this function const _addTestsFromPresets = (presets, structuredData, tests, testsSkipped, testGroups) => { presets.forEach(preset => { - if (!preset.name) { + if (!preset) + throw new Error(`Invalid preset specified`) + + if (!preset.name) throw new Error(`Preset specified does not have a 'name' (required)`) - } const groups = (Array.isArray(testGroups)) ? testGroups.concat(preset.name) : [preset.name] diff --git a/package-lock.json b/package-lock.json index 40f7506..c0388b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "structured-data-testing-tool", - "version": "4.4.0", + "version": "4.4.1", "lockfileVersion": 1, "requires": true, "dependencies": {