How can I use the require-js text-plugin with karma

1.7k Views Asked by At

I am trying to get the require-js text plugin to work with the karma-testrunner I downloaded both with npm. I got karma working with requirejs, it is only the text-plugin that is making me some trouble.

When I add the text.js file to the files that karma serves I get a mismatch Error for it:

Uncaught Error: Mismatched anonymous define() module: function (module) {
   //code from text plugin

If I don't serve the file with the text-plugin or exclude in karma.conf it I get a script Error from requirejs (and a karma Warning: 404/base/text.js)

 Uncaught Error: Script error for: text

I added the following to my require config file:

require.config({
    paths: {
    text: './text.js'  //also tried text.js

  }
 })

but that doesn't seem to change anything

I have the dependency on the text plugin and template declared like this:

   define(['text!views/viewtemplate.html'], function (template) { ...
1

There are 1 best solutions below

0
Steve Hynding On

I was having this issue, too, as I was trying to bring mock JSON files into my test files and parse them with JSON.parse(requirejsInjectedJsonText).

The issue I had was forgetting to update my karma config file with the require-js-text and mock files:

files: [
  {pattern: 'mocks/**/*.json', included: false},
  {pattern:'lib/requirejs-text/text.js', included: false},
  'config/require.config.js'
]

After adding these in place (requirejs config reference must always be in the last index of 'files' array in the karma config file), I was able to successfully pull in my mock data.

For the record, as I was fiddling around, I went as far as @ddd in adding the shim to the require config, i.e.:

shim: {
  'text': {'exports': 'text'}
}

But, it turned out to be unnecessary after I removed it AND had the karma config setup as above.

Cheers!