Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't require() module in 6.0.0-alpha.8 #173

Closed
Thorn1089 opened this issue Apr 19, 2018 · 3 comments
Closed

Can't require() module in 6.0.0-alpha.8 #173

Thorn1089 opened this issue Apr 19, 2018 · 3 comments

Comments

@Thorn1089
Copy link
Contributor

Software versions

  • OS: Windows 7
  • Consumer Pact library: Pact JS v6.0.0-alpha.8
  • Node Version: v8.9.4

Expected behaviour

I can run tests that import Pact like so:

const { Matchers, MessageConsumer, Message, synchronousBodyHandler } = require("@pact-foundation/pact");

Actual behaviour

I get an error when invoking the test via Mocha:

Error: Cannot find module '../../package.json'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (<redacted>\node_modules\@pact-foundation\pact\common\logger.js:15:11)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (<redacted>\node_modules\@pact-foundation\pact\pact.js:28:16)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (<redacted>\test\mq-consumer.test.js:1:134)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at <redacted>\node_modules\mocha\lib\mocha.js:253:27
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (<redacted>\node_modules\mocha\lib\mocha.js:250:14)
    at Mocha.run (<redacted>node_modules\mocha\lib\mocha.js:577:10)
    at Object.<anonymous> (<redacted>\node_modules\mocha\bin\_mocha:591:18)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3

It looks like @pact-foundation/pact/common/logger.js is requiring package.json from the wrong relative location:

L15: var pkg = require("../../package.json");

Manually editing this in my node_modules to ../package.json allows the test to pass. Were directories reorganized that might have led to the changed path?

Steps to reproduce

Run mocha spec test with...
test/mq-consumer.test.js:

const { Matchers, MessageConsumer, Message, synchronousBodyHandler } = require("@pact-foundation/pact");
const path = require("path");

const batchHandler = function() {
  return;//Dummy impl
};
const messagePact = new MessageConsumer({
  consumer: "FOO",
  provider: "BAR",
  dir: path.resolve(process.cwd(), "pacts")//Seems to be required even though .d.ts shows it as optional?
});
describe("the suite", function() {
  it("the test", function() {
    return messagePact.given("pre")
      .expectsToReceive("test")
      .withContent({ foo: "bar" })
      .verify(synchronousBodyHandler(batchHandler));
    });
});
@mefellows
Copy link
Member

Thanks, I noticed this the other day also. Should be a simple one to fix, by converting to an absolute path and having a fallback.

PR welcome ;)

@Thorn1089
Copy link
Contributor Author

I'll take a look. The issue seems to stem from the relative path being different in the src tree versus when the module is compiled and distributed (there's no extra src directory between common and the package.json file).

Thorn1089 added a commit to Thorn1089/pact-js that referenced this issue Apr 20, 2018
The package.json file appears at a different relative location in the source tree versus the compiled package.
It should be resolved regardless of the level of nesting present in the calling module.

Relates to pact-foundation#173
mefellows pushed a commit that referenced this issue Apr 20, 2018
…174)

The package.json file appears at a different relative location in the source tree versus the compiled package.
It should be resolved regardless of the level of nesting present in the calling module.

Relates to #173
@mefellows
Copy link
Member

This is fixed now, closing. Thanks @TomRK1089

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants