nikku/karma-browserify

Cannot find module, but why?

Inf3rno opened this issue · 4 comments

I have an error message:

Chrome 51.0.2704 (Windows 7 0.0.0) ERROR
  Uncaught Error: Cannot find module './features/step_definitions/Class'
  at C:/Users/inf3rno/AppData/Local/Temp/c6dcdd93ce11f47d85665dd900a3f80f.browserify:1 <- node_modules/browserify/node_modules/browser-pack/_prelude.js:1:0

But according to the debug log, the file was served and bundled:

31 07 2016 18:34:50.252:DEBUG [framework.browserify]: updating features\step_definitions\Class.js in bundle
31 07 2016 18:34:56.123:DEBUG [middleware:source-files]: Requesting /base/features/step_definitions/Class.js?3a6a67a21f16e4a0fa350826b0e72e93e269bcc6 /
31 07 2016 18:34:56.124:DEBUG [middleware:source-files]: Fetching D:/creation/software developer/projects/o3/features/step_definitions/Class.js
31 07 2016 18:34:56.125:DEBUG [web-server]: serving (cached): D:/creation/software developer/projects/o3/features/step_definitions/Class.js

My karma.conf.js is in the D:/creation/software developer/projects/o3 directory, and it contains the following code:

module.exports = function (config) {
    config.set({
        plugins: [
            "karma-browserify",
            "karma-chrome-launcher",
            "karma-mocha"
        ],
        frameworks: ["browserify", "mocha"],
        files: [
            "yadda.js",
            "lib/**/*.js",
            {pattern: "lib/**/!(*.js)", included: false},
            "features/**/*.js",
            {pattern: "features/**/!(*.js)", included: false}
        ],
        preprocessors: {
            "yadda.js": ["browserify"],
            "lib/**/*.js": ["browserify"],
            "features/**/*.js": ["browserify"]
        },
        client: {
            mocha: {
                reporter: "html",
                ui: "bdd"
            }
        },
        browserify: {
            debug: true
        },
        browsers: ["Chrome"],
        reporters: ["progress"],
        port: 9876,
        colors: true,
        logLevel: config.LOG_DEBUG,
        autoWatch: false,
        captureTimeout: 6000,
        singleRun: true
    });
};

The deps are installed and the op system is windows 7 x64.

Any idea why I cannot load this module?

The yadda.js contains this code:

var Yadda = require("yadda");
Yadda.plugins.mocha.StepLevelPlugin.init();

var English = Yadda.localisation.English;

var path = Yadda.shims.path;

new Yadda.FeatureFileSearch("./features").each(function (file) {

    featureFile(file, function (feature) {

        var stepsFile = String("./" + path.dirname(file) + "/step_definitions/" + path.basename(file, ".feature")).split("\\").join("/");

        var library = English.library();

        var defineSteps = require(stepsFile);
        defineSteps.call(library);

        var yadda = Yadda.createInstance(library);

        scenarios(feature.scenarios, function (scenario) {
            steps(scenario.steps, function (step, done) {
                yadda.run(step, done);
            });
        });
    });
});

So the module path is generated in a callback. I think this is the problem because if I add the following to the beginning of the file, then the code works properly.

require("./features/step_definitions/noop");
require("./features/step_definitions/Class");
require("./features/step_definitions/UserError");
require("./features/step_definitions/CompositeError");

I am not sure why this is a problem or how to fix it.

According to this SO answer http://stackoverflow.com/a/27672458/607033 it is possible to add a glob like pattern if you want dynamic require in your code. So browserify will know which files to add to the bundle.

nikku commented

Let's hope someone can help you. I do not understand your setup and cannot 😢.

Others had a similar issue: #178

Yadda is a BDD testing lib which has feature files and step definition files. The yadda.js is looking for feature files and require-ing the step definition files, which have a similar path than the feature files. At least by the lib I am developing now. So it is not that complicated and only the require(stepsFile); what you need to understand that the stepsFile path is generated by the code, so it is not static. I am trying now to add the paths I need with require-globify and a glob pattern, but somehow it does not work. :S