Hello everyone!
I wanted to create a really simple React app and add a Pact test on top of it, but I'm having some trouble making Pact play along well with Jest.
So i started by using https://github.com/facebookincubator/create-react-app to create that React app. It generates a small React app containing basically two classes under /src: App.js & App.test.js. If I run npm test, everything runs smooth.
Then I ran npm install --save-dev pact to install Pact.
Next thing I did was editing App.test.js to just import Pact, nothing else, this is how the test looks like:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import pact from 'pact'; // this is the only change I introduced
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
Now if I run npm test again, the test crashes with this error message:
console.error node_modules/dtrace-provider/dtrace-provider.js:26
{ Error: Cannot find module './build/Release/DTraceProviderBindings' from 'dtrace-provider.js'
at Resolver.resolveModule (/Users/felipe/workspace/lixao/pact-js-react-issue/node_modules/jest-resolve/build/index.js:179:17)
at Resolver._getVirtualMockPath (/Users/felipe/workspace/lixao/pact-js-react-issue/node_modules/jest-resolve/build/index.js:296:23)
at Resolver._getAbsolutPath (/Users/felipe/workspace/lixao/pact-js-react-issue/node_modules/jest-resolve/build/index.js:279:10)
at Resolver.getModuleID (/Users/felipe/workspace/lixao/pact-js-react-issue/node_modules/jest-resolve/build/index.js:252:31)
at Runtime._shouldMock (/Users/felipe/workspace/lixao/pact-js-react-issue/node_modules/jest-runtime/build/index.js:574:37)
at Runtime.requireModuleOrMock (/Users/felipe/workspace/lixao/pact-js-react-issue/node_modules/jest-runtime/build/index.js:402:14)
at Object.<anonymous> (/Users/felipe/workspace/lixao/pact-js-react-issue/node_modules/dtrace-provider/dtrace-provider.js:17:23)
at Runtime._execModule (/Users/felipe/workspace/lixao/pact-js-react-issue/node_modules/jest-runtime/build/index.js:513:13)
at Runtime.requireModule (/Users/felipe/workspace/lixao/pact-js-react-issue/node_modules/jest-runtime/build/index.js:329:14)
at Runtime.requireModuleOrMock (/Users/felipe/workspace/lixao/pact-js-react-issue/node_modules/jest-runtime/build/index.js:405:19) code: 'MODULE_NOT_FOUND' }
Am I doing anything wrong here?
I tried npm install --save dtrace-provider but that didn't change anything. I've also tried doing what's documented at https://github.com/pact-foundation/pact-js#note-on-jest, and no luck.
Thanks a lot for the excelent work with the whole Pact family!
Having the same issue here.
Cool, I have been able to repro this. Marking as bug.
For me the tests still work, this is more of an annoying output. Seemingly this issue is quite frequent.
I'm guessing it's related to pact-node which uses bunyan under the hood.
Upon investigation, it seems that bunyan has an optional dependency on dtrace-provider which doesn't seem to reliably compile/work on OSX. This appears to be a very common issue related to the dtrace provider.
As you can see, when installing pact it comes along for the ride:
npm i pact
...
[email protected] /private/tmp/my-app
โโโ [email protected]
โโโฌ [email protected]
โโโฌ @pact-foundation/[email protected]
โโโฌ @pact-foundation/[email protected]
โ โโโ @pact-foundation/[email protected]
โโโฌ @pact-foundation/[email protected]
โ โโโ @pact-foundation/[email protected]
โโโฌ [email protected]
โโโ [email protected]
I've tried all of the suggestions and nothing worked, manually running the compilation didn't fail aside from a few spurious warnings and created what looked like bindings, just not in the intended location.
This workaround worked for me, although it's an obviously crappy solution:
npm uninstall bunyan && npm i bunyan --no-optional
Curious though, I'm not sure why specifically in this configuration it warns.
I figured out that to work around this issue we just need to create a separate script, one that ignores Jest all together.
What I did was moving my Pact tests to src/pacts and then added a script to my package.json, like this:
"scripts": {
...
"pacts": "BABEL_ENV=pacts ./node_modules/.bin/mocha --recursive --compilers js:babel-core/register \"pacts/**/*.pact.js\" --timeout 10000"
},
Then, if I run npm run pacts, my Pact tests run fine and I don't need to change anything for the tests created by create-react-app to work.
Thanks @felipecao, great find - I'll update the docs with this (somewhere).
@mefellows, please re-open this, the error is still there. Also, the bunyan issue is open since January 2015, so I guess the best solution would be to get rid of the dependency, if possible (haven't checked the code yet).
Steps to reproduce:
const Pact = require('@pact-foundation/pact')yarn add --dev @pact-foundation/pact
yarn test
=> the reported "DTraceProviderBindings" error appears.
The issue is in upstream pact-node package - perhaps we need to look at removing it from there.
cc: @mboudreau
Released v5.7.0 with this change @mbark.
Thank you for the fast merge, everything looks good now! โญ๏ธ
๐