grunt-template-jasmine-requirejs
grunt-template-jasmine-requirejs copied to clipboard
Error when using with requirejs-text plugin
I am getting cross-origin errors when requirejs tries to load files using the text! requirejs plugin. Should I be able to use requirejs plugins and are there any tricks I need to know to get them to work?
The cross-origin error only happens when I am debugging and loading _SpecRunner.html into the browser. For Firefox, it only happens if my baseURL is outside of the spec directory, e.g. "../public" For Chrome, it happens whatever the baseURL.
However, even when I don't get cross-origin errors, I get syntax errors for the text files (like they are being loaded as js rather than text).
fwiw, here's a simple example. It succeeds when run through Grunt. It gives a syntax error with Mozilla (although the test does succeed). And it fails with Chrome due to cross-origin error. Will understand if you want to close this issue as running in a browser is not the purpose, but it does make it much harder to debug if you can't.
Gruntfile
module.exports = function (grunt) {
grunt.initConfig({
jasmine: {
hello: {
options: {
specs: 'hello-spec.js',
template: require('grunt-template-jasmine-requirejs'),
vendor: [ '.grunt/grunt-contrib-jasmine/es5-shim.js'],
templateOptions: {
requireConfig: {
baseUrl: '.',
paths: {
"text": "require-text-2.1.12"
}}}
}}}
});
grunt.loadNpmTasks('grunt-contrib-jasmine');
grunt.registerTask('run_tests', [ 'jasmine:hello' ] );
};
hello-spec.js
define(['text!hello.txt'], function(txt){
describe("Bind: ", function(){
it("should find hello", function(){
expect(txt).toContain("HELLO");
});
});
});
hello.txt
HELLO!!!!!
It looks like this was fixed for me. I thought I had the same problem as you until seeing that it was a JSON.parse in my code under test that failed.
I'm using:
- jasmine 2.1.3
- Squire 0.2.1
- requirejs 2.1.8
- requirejs text plugin 2.0.7
If I didn't mock the text module, it would come as an empty string.
I used Squire to mock the module like this:
beforeEach(function(done) {
var builder = injector.mock("text!/api/v1/practice/builder/question/list/1", "{\"list\": []}");
builder.require(["viewmodels/report_editor/user_widget_instance_options"], function(_UserWidgetInstanceOptions) {
UserWidgetInstanceOptions = _UserWidgetInstanceOptions;
done();
});
});