orlade
orlade

Reputation: 2090

Error importing Node.js modules into Intern tests

I'm trying out Intern for testing our Node.js modules. I've got it set up to run an empty test and even require node modules, but when I try to require one of the modules in our package I get the error:

/path/to/app/node_modules/path/path.js:327
  var path = (i >= 0) ? arguments[i] : process.cwd();
                                               ^
TypeError: undefined is not a function
  at Object.exports.resolve (/path/to/app/node_modules/path/path.js:327:52)
  at Object.exports.relative (/path/to/app/node_modules/path/path.js:405:20)
  at getSource (/path/to/app/node_modules/intern/lib/util.js:368:21)
  at formatLine (/path/to/app/node_modules/intern/lib/util.js:405:40)
  at processChromeTrace (/path/to/app/node_modules/intern/lib/util.js:418:16)
  at normalizeStackTrace (/path/to/app/node_modules/intern/lib/util.js:470:38)
  at Object.getErrorMessage (/path/to/app/node_modules/intern/lib/util.js:597:14)
  at PreExecutor._handleError (/path/to/app/node_modules/intern/lib/executors/PreExecutor.js:256:24)
  at /path/to/app/node_modules/intern/node_modules/dojo/lang.js:78:32
  at process.<anonymous> (/path/to/app/node_modules/intern/lib/executors/PreExecutor.js:302:6)

Folder structure is:

Here's my intern.js:

define({
  capabilities: {
    'browserstack.selenium_version': '2.45.0'
  },

  environments: [
    {browserName: 'internet explorer', version: '11', platform: 'WIN8'},
    {browserName: 'internet explorer', version: '10', platform: 'WIN8'},
    {browserName: 'internet explorer', version: '9', platform: 'WINDOWS'},
    {browserName: 'firefox', version: '37', platform: ['WINDOWS', 'MAC']},
    {browserName: 'chrome', version: '39', platform: ['WINDOWS', 'MAC']},
    {browserName: 'safari', version: '8', platform: 'MAC'}
  ],

  maxConcurrency: 2,
  tunnel: 'BrowserStackTunnel',
  loaderOptions: {
    packages: [{name: 'app', location: 'js/src'}]
  },
  reporters: ['Console', 'Lcov'],
  suites: ['js/tests/unit/all'],
  excludeInstrumentation: /^(?:\.npm|js\/tests|src|tests|node_modules)\//
});

My test is just:

define (require) ->
  registerSuite = require('intern!object')
  assert = require('intern/chai!assert')
  SchemaBuilder = require('intern/dojo/node!app/path/to/Module')

  registerSuite
    name: 'Testing'

    'test': ->

From the project root I'm running './node_modules/.bin/intern-client config=tests/intern'

Is this a problem with my code, configuration, environment, or Intern?

I've tried with Node.js v0.12.0 and v0.12.7.

Upvotes: 2

Views: 477

Answers (1)

orlade
orlade

Reputation: 2090

Interestingly, removing the path module from my app dependencies (package.json) resolved (well, avoided) the undefined is not a function error, seemingly falling back on Intern's own path resolution. I don't really know why, but this seems like a bug.

This presented a new, more tractable error: Error: Cannot find module 'app/path/to/Module', suggesting the path in the require() was simply wrong.

I think the problem is that the intern/dojo/node loader doesn't support the loaderOptions: {packages: [{name: 'app', location: 'js/src'}]} configuration, and hence it is searching for an app directory relative to the test module. If I replace app/ with ../../../../js/src/ then it resolves and works as expected.

It's a bit ugly, but it's the standard way of requiring local files in node, so it's also not too surprising, I had just hoped Intern's loader could make it cleaner.

Upvotes: 2

Related Questions