:wave: Hi everyone! I'm trying to setup consumer ...
# general
i
👋 Hi everyone! I'm trying to setup consumer test in a project with Angular+Jest but can't figure out how to do it. Any help would be greatly appreciated. I found out that
pact-web
project is discontinued, meaning that my only option is to run Angular tests in a node environment using
jest-pact
Is there anyone who could make it work with Angular?
m
You don’t have to use
jest-pact
, you can just use regular pact (jest pact probably doesn’t add a lot of value over and above vanilla Pact these days)
Is there a particular problem you’re encountering? There are a bunch of blogs on the internet that describe how to do it. The main thing is the ability to isolate your API client - that’s what needs to be tested
i
In a nutshell my problem is that I can't run pact in a
jsdom
environment in jest, and it makes a lot of sense. It needs to spin up a local server to setup interactions, and also it should be able to write contracts into the file. On the other hand i can't make Angular to work with
node
environment in jest.
And, i can't find any working example with Angular+Jest+Pact (without using
pact-web
)
👍 1
m
Ah! OK so problem #1 is to figure out how to run Angular in a
node
environment. Interesting.
Do you know what specifically angular needs in that environment?
i
well, my service is pretty simple. it makes a post request to the graphql endpoint using angular's
httpClient
👍 1
My problem might be related more to the Angular than pact itself... i actually made it work using dummy api requests using fetch api. But as soon as i'm trying to replace dummy fetch api requests to use angular service i end up with uncertain problems. My assumption is that it's not straightforward to run Angular services (using HTTPModule) in a node environemnt...
m
I’m sure it’s been done. @Alex Maiburg I think managed to do it recently, any tips you could share Alex?
Worth having a further search through https://www.linen.dev/s/pact-foundation in case anything appears to be useful
i
will look into it, thanks @Matt (pactflow.io / pact-js / pact-go)
👍 1
I believe I can summarize the entire problem with this specific error:
ReferenceError: XMLHttpRequest is not defined
This happens when I try to use
HTTPClient
to make a POST request. The root cause is that I am running Angular code in a Node.js environment, where
XMLHttpRequest
does not exist.
👍 1
m
got it, that makes sense. I’m assuming there must be a way to shim around this (but also I don’t know ng very well) e.g. the suggestion here: https://github.com/angular/angular/issues/18199
(global as any).XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
i
quick update: i made it work eventually. I was making a mistake when trying to run everything in a
node
environment in jest. turns out running it in
jsdom
is sufficient for pact. Another note: i was trying to use
ng-neat
for setting up tests, but it is a bit more tricky to set it up by making sure that the pact server is running before everything else. using angulars default
TestBed
seems to be better..
There were few things that confused me along this journey, one of which was the reference to https://github.com/niklas-wortmann/ngx-pact It is mentioned in the documentation under the Usage with Angular section: https://docs.pact.io/implementation_guides/javascript/docs/troubleshooting#usage-with-angular
ngx-pact is using
pact-web
which is obsolete. @Matt (pactflow.io / pact-js / pact-go) it might be good to remove it from the documentation and put it back when it's updated to the latest version.. Anyways, thanks for your support ✌️