Get Contents of External Text File In Karma-Jasmine Test

1.2k Views Asked by At

I've been at this for the whole day and have made virtually no headway. I have an Javascript interpreter which takes text code and I am trying to test this using Karma-Jasmine tests. Because I don't want to embed code in every single spec, I am trying to load these items in from external text files.

So far I've made the text file a fixture in karma.config.json, but I can't seem to figure out how to actually read this into a Javascript string.

Here's a sample spec:

jasmine.getFixtures().fixturesPath = 'base/codeFiles';
var testCode = loadFixtures("base/codeFiles/correct_1.txt");
console.log(f);

describe("#interpreter_load_test", function() {
  it("loads the code correctly", function() {
    var INTERPRETER = getInterpreter();

    INTERPRETER.initialize();
    INTERPRETER.loadCode(testCode);
    var codeObj = INTERPRETER.code;
    var numLines = codeObj.codeLength;
    var loadedCode = codeObj.codeString;
    expect(numLines).toBe(16);
    expect(loadedCode).toBe(testCode);
  });
});

Here's my karma.config.json

// Karma configuration
// Generated on Sat Jun 20 2015 21:46:22 GMT+0000 (UTC)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine-jquery', 'jasmine'],


    // list of files / patterns to load in the browser
    files: [
      'js/build/production.min.js',
      'js/tests/*.js',
      {
        pattern: 'codeFiles/*.txt',
        watched: true,
        served: true,
        included: false,
      }
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['PhantomJS', 'Chrome', 'Firefox', 'IE', 'Safari'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    proxies: {
      '/codeFiles': 'http://localhost:9876/base/codeFiles',
    },
  });
};

I'm banging my head against the wall at this point, so any help would be greatly appreciated.

1

There are 1 best solutions below

0
On

In the end I decided to switch to Mocha and use the fs package, which I probably should have been doing in the first place.