iaincollins / structured-data-testing-tool

A library and command line tool to help inspect and test for Structured Data.
https://www.npmjs.com/package/structured-data-testing-tool
ISC License
64 stars 14 forks source link

Error: Cannot find module './presets' #15

Closed Zrce closed 4 years ago

Zrce commented 4 years ago

Im failing with

Error: Cannot find module './presets'

if I try this code in the documentation.

const { structuredDataTest } = require('structured-data-testing-tool')
const { ReportageNewsArticle, Twitter, Facebook } = require('./presets')

const url = 'https://www.bbc.co.uk/news/world-us-canada-49060410'

structuredDataTest(url, { presets: [ ReportageNewsArticle, Twitter, Facebook ] })
.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)
})
.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)
    })
  } else {
    // Handle other errors here (e.g. an error fetching a URL)
    console.log(err)
  }
})
iaincollins commented 4 years ago

Sorry, that looks like a bug in the documentation! Thank you for reporting it.

The line:

const { ReportageNewsArticle, Twitter, Facebook } = require('./presets')

Should read:

const {
  ReportageNewsArticle,
  Twitter,
  Facebook
} = require('structured-data-testing-tool/presets')
iaincollins commented 4 years ago

I've updated README, the update to it will go out in the next release.

Zrce commented 4 years ago

Thanks. Actually I still get

TypeError: Cannot read property 'name' of undefined

iaincollins commented 4 years ago

Oh no! Sorry, I looked closer and the example is completely wrong (it is outdated).

Please try this, and let me know if you have any questions:

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 => {
  result = res
  console.log('✅ All tests passed!')
})
.catch(err => {
  if (err.type === 'VALIDATION_FAILED') {
    result = err.res
    console.log('❌ Some tests failed.')
    if (result.failed.length > 0) console.log("⚠️  Errors:\n", result.failed.map(test => test))
  }
  } 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(',')}`)
})

I'll fix this in the documentation and improve the error handling so the error messages are better if anyone else runs into this issue.

iaincollins commented 4 years ago

Thanks to your feedback, a version with updated documentation has been released!

I've also added this and one other example to the examples directory.

Feel free to re-open this issue (or open a new one) if you run into any more problems.