computmaxer / karma-jspm

Other
74 stars 50 forks source link

Loading any test files results in a 404 #99

Closed jtompkins closed 8 years ago

jtompkins commented 8 years ago

I'm trying to get started with Karma and karma-jspm, so please forgive me if is is a dumb question. Basically, I can't get Karma to load any of my test files - it 404s. In case it's important, this issue is occurring on a Windows 8.1 PC.

In order to try and isolate what's happening, I created a new project with very little actual code. The structure looks like this:

image

My package.json file looks like this:

{
  "name": "duckpond",
  "version": "1.0.0",
  "description": "A small app to read Fusion notifications.",
  "author": "Joshua Tompkins",
  "license": "MIT",
  "devDependencies": {
    "jasmine": "^2.3.2",
    "jasmine-core": "^2.3.4",
    "jspm": "^0.16.2",
    "karma": "^0.13.9",
    "karma-chrome-launcher": "^0.2.0",
    "karma-jasmine": "^0.3.6",
    "karma-jspm": "^2.0.1"
  },
  "jspm": {
    "directories": {},
    "devDependencies": {
      "babel": "npm:babel-core@^5.8.22",
      "babel-runtime": "npm:babel-runtime@^5.8.20",
      "core-js": "npm:core-js@^1.1.0"
    }
  }
}

My config.js file looks like this:

System.config({
  baseURL: "/",
  defaultJSExtensions: true,
  transpiler: "babel",
  babelOptions: {
    "optional": [
      "runtime",
      "optimisation.modules.system"
    ],
    "blacklist": [],
    "stage": 0
  },

  paths: {
    "github:*": "jspm_packages/github/*",
    "npm:*": "jspm_packages/npm/*"
  },

  map: {
    "babel": "npm:babel-core@5.8.23",
    "babel-runtime": "npm:babel-runtime@5.8.20",
    "core-js": "npm:core-js@1.1.3",
    "github:jspm/nodelibs-process@0.1.1": {
      "process": "npm:process@0.10.1"
    },
    "npm:babel-runtime@5.8.20": {
      "process": "github:jspm/nodelibs-process@0.1.1"
    },
    "npm:core-js@1.1.3": {
      "fs": "github:jspm/nodelibs-fs@0.1.2",
      "process": "github:jspm/nodelibs-process@0.1.1",
      "systemjs-json": "github:systemjs/plugin-json@0.1.0"
    }
  }
});

Note that the baseUrl is set to /, which is the JSPM default (as far as I can tell).

My karma.conf.js file looks like this:

// Karma configuration
// Generated on Mon Aug 31 2015 08:50:56 GMT-0400 (Eastern Daylight Time)

module.exports = function(config) {
  config.set({
    singleRun: false,

    // 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: ['jspm', 'jasmine'],

    jspm: {
        // Edit this to your needs
        loadFiles: ['test/**/*.js'],
        serveFiles: ['app/**/*.js']
    },

    // list of files / patterns to load in the browser
    files: [],

    // 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,

    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome']
  })
}

As far as I can tell, this is basically what you get when you run karma init, with the karma-jspm changes added.

There's only one source file (simpleThing.js), and it looks like this:

const testFunction = () => {
  return 'test successful';
};

export default testFunction;

The corresponding spec file looks like this:

import testFunction from '../app/simpleThing';

describe('Basic Karma configuration', () => {
  it('should run a test against an imported function', () => {
    expect(testFunction()).toEqual('test successful');
  });
});

Running my tests results in this error:

image

The problem appears to be that the file is being served with a /base/ prefix, and karma-jspm isn't looking there.

I do not want to modify my config.js file to make Karma work. I can make some forward progress by proxying my /app and /test folders to /base/app (etc), but when I do that, I still get an error because karma-jspm can't find the node_modules or jspm_modules folders.

Not really sure where to go from here. I saw issue #91, but my baseUrl is the default value, so I wasn't sure if that error applied here. Does anyone have any ideas what I'm doing wrong here?

maxwellpeterson-wf commented 8 years ago

This is essentially #91. Since you are not changing your baseURL to something custom, you can completely omit the baseURL entry from config.js and everything should work as expected. Part of the issue being discussed in #91 is that setting the baseURL more than once doesn't seem to work. We have to set a different baseURL because Karma insists on serving the project files with a /base/ prefix on all requested URLs.

If you remove this line from your config.js: baseURL: "/", it should allow karma-jspm to set it to /base during test runs. Since "/" is the default, it shouldn't negatively affect your app outside of test runs, either.

jtompkins commented 8 years ago

@maxwellpeterson-wf Yep, that works beautifully. Thank you for the very quick response! Unless you have any objections, I'm closing this issue, since it's a dup of #91.