Git Product home page Git Product logo

Comments (14)

tarciosaraiva avatar tarciosaraiva commented on September 17, 2024 1

you're right @mefellows might update that now

from pact-js.

 avatar commented on September 17, 2024

This seem to be related with pact-foundation/pact-js-core#20

from pact-js.

tarciosaraiva avatar tarciosaraiva commented on September 17, 2024

Will have a look at this today. Did you try installing a previous version of the library? The latest one went through a refactor last week and I wonder if it caused a side effect somewhere - or maybe it is really the pact-node issue you refer to.

Would you be able to tell 1) what environment are you on and 2) if you can try 1.0.0-rc.4 instead of the latest and how you go with it?

Thanks

from pact-js.

 avatar commented on September 17, 2024

The environment this is running is:

npm:  3.10.3
node: 6.4.0

And I just tried the rc.4 release, but that did not work.

If I move the initialization of the variable provider outside the callback from the server start(). The pact interaction gets registered on the pact file. The test fails at the end, and during the execution I see this too:

 [SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()
 Unhandled promise rejection ProgressEvent { isTrusted: [Getter] } ( 6x)

from pact-js.

tarciosaraiva avatar tarciosaraiva commented on September 17, 2024

Moving the variable outside you can't be sure if the server really started.

I just noticed this line: Cannot read property 'finalize' of undefined

And also paying to attention to Jest, the async behaviour is different from Jasmine based on what I seen on the documentation and it doesn't look like it contains beforeAll and afterAll hooks like the latest versions of Jasmine do based on this part of the docs.

Maybe you have to adapt your code?

from pact-js.

 avatar commented on September 17, 2024

Yes, that is what I am assuming. The undefined is because of that. provider is undefined until the server starts. It never starts it timeout and then run those lines for the afterEach and afterAll. Sure I will try that! thanks and I will update

from pact-js.

tarciosaraiva avatar tarciosaraiva commented on September 17, 2024

No worries @dolfo-pivotal - BTW first time I've seen someone using Jest with Pact. Would be great to have your contribution on a test case / doco 👍

from pact-js.

tarciosaraiva avatar tarciosaraiva commented on September 17, 2024

How did you go @dolfo-pivotal ?

from pact-js.

dolfolife avatar dolfolife commented on September 17, 2024

@tarciosaraiva this is my personal account. XD I could not allocated more work on the client, but I created a example using jest. https://github.com/rodolfo2488/pact-test-example/blob/master/client/__tests__/mainTest.js that works, but:

I have to tell jest to use the "node" testEnvironment

I get weird errors/warnings on the jsdom mode with jest like

crypto not usable, falling back to insecure Math.random()
ProgressEvent is untrusted

Note: this is because something is rejecting the request when it is by a script.

I could not use the pact.verify call

I need to read more the implementation of that method to see where I can fit that in the test. So far I am not using it.

I had to use cors true or it won't work

I have no clue why is this.

But I will continue working on that when I have time. A friend of mine started a project for spring boot verifier on the provider side that as well I am using it on the server side of that example. check that out.

Feel free to corrent my mainTest.js or suggest changes on it.

from pact-js.

tarciosaraiva avatar tarciosaraiva commented on September 17, 2024

hi @rodolfo2488 I think I identified the issue. Jest loads up JSDOM by default, even though the Jest configuration states that a browser environment is off by default.

This is causing Pact to instantiate a XMLHttpRequest object that is basically invalid:

    XMLHttpRequest {
      onabort: null,
      onerror: null,
      onload: null,
      onloadend: null,
      onloadstart: null,
      onprogress: null,
      ontimeout: null,
      upload: 
       XMLHttpRequestUpload {
         onabort: null,
         onerror: null,
         onload: null,
         onloadend: null,
         onloadstart: null,
         onprogress: null,
         ontimeout: null,
         _ownerDocument: Document { location: [Getter/Setter] } },
      onreadystatechange: null }

I'm digging further to see how we can fix this. Will keep you posted.

from pact-js.

tarciosaraiva avatar tarciosaraiva commented on September 17, 2024

OK @rodolfo2488 @dolfo-pivotal I understand what's going on now.

TL;DR: Jest uses JSDOM by default and brings along A LOT of polyfills to mimic a real browser environment (RBE) which passes the supporting libraries checks for RBE's thus using polyfills of questionable implementation.

JSDOM, as you know, is highly used by Facebook, Airbnb, etc to simulate a RBE in Node especially for React testing.

Most libraries that deal with HTTP/S requests that try to be universal usually check fora RBE like this:

    typeof window !== 'undefined' &&
    typeof document !== 'undefined' &&
    typeof document.createElement === 'function'

The code above is actually from axios and the full method is like this

function isStandardBrowserEnv() {
  return (
    typeof window !== 'undefined' &&
    typeof document !== 'undefined' &&
    typeof document.createElement === 'function'
  );
}

Pact JS does a similar check. I don't think JSDOM was meant to be used so heavily like this with real XHR objects being instantiated and requests making their way through - which actually work as you seen the log being populated - but the response somehow is not mapped correctly on the polyfill causing the request to fail and always be rejected by the Promise.

The guys from Jest thought about that and provided a configuration option named testEnvironment. If you set it to node then everything works.

I will be pushing an examples folder that contain a sample with Jest for Node. For the browser though, I strongly recommend you use something like Karma if you can.

Hope that helps and thanks for the issue. At least now I know that JSDOM might be something to look out for and potentially bring more support for React testing with Pact.

from pact-js.

tarciosaraiva avatar tarciosaraiva commented on September 17, 2024

hi @rodolfo2488 here's the example code based on your sample: https://github.com/pact-foundation/pact-js/tree/master/examples/jest

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

Nice work @tarciosaraiva, is it worth noting this in the README more explicitly for future reference?

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

I've updated the README with a reference to the details of this issue, closing.

from pact-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.