node.js – RequireJS + Mocha + JSDom + Node -> Shim config not supported in Node

I’m trying to setup a Mocha testing framework using JSDom with RequireJS. Because I’m running the test on node instead of using a browser (since I’m using JSDom), all the non AMD modules doesn’t seem to be imported and is throwing Shim config not supported in Node. Does anyone know how I can export those modules to AMD or what the right approach is? (aka what I’m doing wrong)

Example of my set-up

Component.js

define(["jquery", "non_AMD_Module", ... ], function($, NonAMDModule, ...) {
  let component = {
    ...
    foo = () => {
      NonAMDModule.bar();
    };
  };
  return component;
});

Component.test.js

const requirejs = require('requirejs');
const { JSDOM }  = require('jsdom');

requirejs.config({
  baseUrl: "dist/app",
  paths: {
    jquery: "lib/jquery",
    component: "path_to_component",
    non_AMD_Module: "path_to_module"
  },
  shim: {
    non_AMD_Module: { exports: "non_AMD_Module" } // This doesn't work
  }
});

const { window } = new JSDOM("<html></html>");
global.window = window;
global.document = window.document;
global.$ = requirejs('jquery');

const Component = requireJS('component');
describe('test', () => {
  it('is a simple test', () => {
     const testComponent = new Component();
     testComponent.foo();
  }
});

When I run the test suite, I get:

Mocha Exploded!
  TypeError: Cannot read property 'bar' of undefined

running r.js -convert "path_to_module" did not work for this module

Source link