wwilsman/js-yaml-loader

Cannot find module 'js-yaml-loader!../config/secrets.yml' from 'image_handler.js' for testing

ChunAllen opened this issue · 1 comments

I'm currently using js-yaml-loader to load my credentials from /config/secrets.yml but when I'm testing it using Jest.

image_handler.js

import placeholder from "../assets/images/placeholder.jpg";
import secrets from 'js-yaml-loader!../config/secrets.yml';
const config = secrets[process.env.NODE_ENV];

const parseImages = (images) => {
  var poiImages = [];
  if (images && images.length > 0 && images[0] != null) {
    for (let image of images) {
      var url = image.url || `${config.BASE_URL}/media/v1/download/uuid/${image.uuid}?apikey=${config.TIH_KEY}`;
      poiImages.push(url);
    }
  } else {
    poiImages.push(placeholder);
  }
  return poiImages;
}

export default {
  parseImages
};

I'm getting Cannot find module 'js-yaml-loader!../config/secrets.yml' from 'image_handler.js'

Hello! Thanks for opening an issue!

This is a Webpack plugin, and as such it requires Webpack to work. While Jest can work with Webpack projects, it does not use Webpack to bundle and transform files.

For static assets and CSS transforms, Jest recommends mocking those file extensions to serve a JS file that Jest is then able to handle.

However, you may be better off using a Jest transformer like this one or create your own custom transformer and use the JS-YAML library directly. You may also have to handle the loader! syntax in the filepath.