Skip to content

Possibility to use relative path instead of absolute one to register module ? #3

@mpellerin42

Description

@mpellerin42

Hi,

I'm testing this preprocessor and it seems to work well. But there is a little thing annoying for me : modules are registered under their absolute full path name (ie. c:/absolute/path/to/my/app/then/my/file.js). So in my test, I have to do something like this to load my module :

describe("test with require", function(){
    var serviceUnderTest;
    var serviceUnderTestModule = require('c:/absolute/path/to/my/app/then/my/file.js');
    beforeEach(function(){
        serviceUnderTest= new serviceUnderTestModule ();
    });
    it('should have a method defined', function() {
        expect(serviceUnderTest.anyFunction).toBeDefined();
    });
});

With commonjsPreprocessor options, I saw that I can do a path replacement like that to use relative path instead of absolute one :

commonjsPreprocessor: {
  options: {
    pathReplace: function(path){
      return path.replace(/^c\:\/absolute\/path\/to\/my\/app/, '\.');
    }
  }
}

so my test become :

describe("test with require", function(){
    var serviceUnderTest;
    var serviceUnderTestModule = require('./then/my/file.js');
    beforeEach(function(){
        serviceUnderTest= new serviceUnderTestModule ();
    });
    it('should have a method defined', function() {
        expect(serviceUnderTest.anyFunction).toBeDefined();
    });
});

which is better.
But this solution is not good enough : absolute path is still hard coded in my karma conf, but it will be different for each colleagues and for continuous integration.

So, is there a configuration way that I didn't see to avoid these absolute paths ?
Or maybe do you know a way to get current absolute path directly from karma conf file ?

Any help will be very appreciate :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions