3

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) { ...
ddd
  • 219
  • 4
  • 15
  • We would need more code to help you better. Did you add `'text.js'` in the array of `files` served by Karma? (in your karma config) – glepretre Oct 20 '14 at 14:59
  • @glepretre Thanks for your response, I got it working in the meantime by following the hints given in this discussion https://github.com/karma-runner/karma/issues/740 – ddd Oct 20 '14 at 16:23

1 Answers1

2

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!

Steve Hynding
  • 1,799
  • 1
  • 12
  • 22