https://pact.io logo
#pact-js
Title
# pact-js
d

Djalmar Gutierrez

03/03/2022, 8:36 PM
Hi anyone can help me. I’m using pact in a debian container. When we use mocha as test framework everything works fine, our project uses jasmine as main test framework, when I switch to jasmine I got this error:
Unhandled promise rejection: Error: Pact startup failed; tried calling service 10 times with no result.
Nothing else is changed.
m

Matt (pactflow.io / pact-js / pact-go)

03/03/2022, 9:26 PM
Are there any other logs?
d

Djalmar Gutierrez

03/03/2022, 9:40 PM
@Matt (pactflow.io / pact-js / pact-go) these are the complete logs
NODE_ENV=test npx jasmine spec/contract/bsapi.spec.js
Running tests with 'NODE_ENV=test'
Started
[2022-03-03 20:44:30.699 +0000] INFO (133 on 7d526b155d90): pact-node@10.17.1: Creating Pact Server with options:
{"timeout":100000,"consumer":"ares-integration-services","cors":false,"dir":"/app/pacts","host":"0.0.0.0","log":"/app/logs/mockserver-integration.log","logLevel":"DEBUG","pactfileWriteMode":"overwrite","provider":"BSAPI","spec":2,"ssl":false,"port":1234,"pactFileWriteMode":"overwrite"}
[2022-03-03 20:44:30.713 +0000] DEBUG (133 on 7d526b155d90): pact-node@10.17.1: Starting pact binary '/app/node_modules/@pact-foundation/pact-node/standalone/linux-x64-1.88.81/pact/bin/pact-mock-service', with arguments [service --consumer ares-integration-services --cors false --pact_dir /app/pacts --host 0.0.0.0 --log /app/logs/mockserver-integration.log --log-level DEBUG --provider BSAPI --pact_specification_version 2 --ssl false --port 1234 --pact-file-write-mode overwrite]
[2022-03-03 20:44:30.719 +0000] DEBUG (133 on 7d526b155d90): pact-node@10.17.1: Created '/app/node_modules/@pact-foundation/pact-node/standalone/linux-x64-1.88.81/pact/bin/pact-mock-service' process with PID: 144
[2022-03-03 20:44:36.922 +0000] DEBUG (133 on 7d526b155d90): pact-node@10.17.1: INFO  WEBrick 1.3.1
INFO  ruby 2.2.2 (2015-04-13) [x86_64-linux]
[2022-03-03 20:44:36.922 +0000] DEBUG (133 on 7d526b155d90): pact-node@10.17.1: INFO  WEBrick::HTTPServer#start: pid=144 port=1234
[2022-03-03 20:46:10.641 +0000] ERROR (133 on 7d526b155d90): pact@9.17.2: The pact mock service doesn't appear to be running
- Please check the logs above to ensure that there are no pact service startup failures
- Please check that pact lifecycle methods are called in the correct order (setup() needs to be called before this method)
- Please check that your test code waits for the promises returned from lifecycle methods to complete before calling the next one
- To learn more about what is happening during your pact run, try setting logLevel: 'DEBUG'
F[2022-03-03 20:46:10.649 +0000] ERROR (133 on 7d526b155d90): pact@9.17.2: The pact mock service doesn't appear to be running
- Please check the logs above to ensure that there are no pact service startup failures
- Please check that pact lifecycle methods are called in the correct order (setup() needs to be called before this method)
- Please check that your test code waits for the promises returned from lifecycle methods to complete before calling the next one
- To learn more about what is happening during your pact run, try setting logLevel: 'DEBUG'
Failures:
1) BSAPI /bsapi/api/v1c/monitoring-series?baseline-id=ORD-2109329-02 when request is not authenticated should fail requiring authentication
Message:
Error: The pact mock service wasn't running when verify was called
Stack:
Error: The pact mock service wasn't running when verify was called
at Pact.verify (/app/node_modules/@pact-foundation/pact/src/httpPact.js:108:35)
at UserContext.<anonymous> (/app/spec/contract/bsapi.spec.js:59:28)
at <Jasmine>
Suite error: BSAPI
Message:
Unhandled promise rejection: Error: Pact startup failed; tried calling service 10 times with no result.
Stack:
Error: Pact startup failed; tried calling service 10 times with no result.
at Server.retry (/app/node_modules/@pact-foundation/pact-node/src/service.js:177:33)
at _rejected (/app/node_modules/q/q.js:864:24)
at /app/node_modules/q/q.js:890:30
at Promise.when (/app/node_modules/q/q.js:1142:31)
at Promise.promise.promiseDispatch (/app/node_modules/q/q.js:808:41)
at /app/node_modules/q/q.js:624:44
at runSingle (/app/node_modules/q/q.js:137:13)
at flush (/app/node_modules/q/q.js:125:13)
at processTicksAndRejections (node:internal/process/task_queues:78:11)
Message:
Error: Timeout - Async function did not complete within 100000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)
Stack:
Error: Timeout - Async function did not complete within 100000ms (set by jasmine.DEFAULT_TIMEOUT_INTERVAL)
at <Jasmine>
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)
Message:
Error: The pact mock service wasn't running when finalize was called
Stack:
Error: The pact mock service wasn't running when finalize was called
at Pact.finalize (/app/node_modules/@pact-foundation/pact/src/httpPact.js:141:35)
at UserContext.<anonymous> (/app/spec/contract/bsapi.spec.js:61:27)
at <Jasmine>
at processImmediate (node:internal/timers:464:21)
1 spec, 2 failures
Finished in 99.957 seconds
m

Matt (pactflow.io / pact-js / pact-go)

03/03/2022, 9:42 PM
can you please share your test setup? The error message has some helpful guides on what to do - are you sure
setup()
is called in the correct place etc.?
y

Yousaf Nabi (pactflow.io)

03/04/2022, 12:25 PM
Would also be good to see your test setup files for Mocha and the equivalent for Jasmine. It looks like the pact mock service hasn't started, which may have been in a mocha specific setup file which you haven't got for jasmine. we have a mocha and jasmine example but as part of
karma
here which uses pact-web which probably isn't what you are after
d

Djalmar Gutierrez

03/04/2022, 12:59 PM
@Yousaf Nabi (pactflow.io) I don’t have any setup for mocha nor jasmine. I call both like this in the package.json: “testcontract” “NODE_ENV=test jasmine spec/contract/*.spec.js” and for mocha “testcontract” “NODE_ENV=test mocha --exit --recursive -R spec spec/contract/*.spec.js --timeout 10000"
y

Yousaf Nabi (pactflow.io)

03/04/2022, 1:04 PM
Ahh thanks that is useful, so I assume your setup is in the test file(s) hook itself. what is the in the contents of the log file
app/logs/mockserver-integration.log
Also if you put three backticks before your code output in slack, you will get a nice formatted code block which is much easier to read 🙂 ````your code text`
👍 1
d

Djalmar Gutierrez

03/04/2022, 1:04 PM
@Matt (pactflow.io / pact-js / pact-go) I believe it is correctly setup, because the same code(just framework changes) is working in mocha That is the test setup
const path = require('path')
const { Pact } = require('@pact-foundation/pact')
describe('API', () => {
let provider
jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000
provider = new Pact({
consumer: 'ares-integration-services',
provider: 'BSAPI',
port: 1234,
log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), 'pacts'),
logLevel: 'DEBUG',
timeout: 100000
})
beforeAll(async () => {
await provider.setup()
provider.addInteraction({
state: 'the request is not authenticated',
uponReceiving: 'a request for monitoring series',
withRequest: {
method: 'GET',
path: '/api'
},
willRespondWith: {
status: 401,
headers: {
'Content-Type': 'application/problem+json; charset=utf-8'
}
}
})
})
describe('/api', () => {
describe('when request is not authenticated', () => {
it('should fail requiring authentication', async () => {
expect(true).toBe(true)
})
})
})
afterEach(() => provider.verify())
afterAll(() => provider.finalize())
})
y

Yousaf Nabi (pactflow.io)

03/04/2022, 1:06 PM
I'll take a look and try and put some examples against a pact-js branch buddy
🙏 1
d

Djalmar Gutierrez

03/04/2022, 1:12 PM
In the log file there is nothing, when run with jasmine With mocha i got this, I know there is an error but the connection is working properly
Copy code
I, [2022-03-04T13:11:11.827524 #29]  INFO -- : Registered expected interaction GET /api
W, [2022-03-04T13:11:11.830281 #29]  WARN -- : Verifying - actual interactions do not match expected interactions. 
Missing requests:
	GET /api



D, [2022-03-04T13:11:11.830585 #29] DEBUG -- : {
  "description": "a request for monitoring series",
  "providerState": "the request is not authenticated",
  "request": {
    "method": "GET",
    "path": "/api"
  },
  "response": {
    "status": 401,
    "headers": {
      "Content-Type": "application/problem+json; charset=utf-8"
    }
  },
  "metadata": null
}
W, [2022-03-04T13:11:11.831952 #29]  WARN -- : Missing requests:
	GET /api



I, [2022-03-04T13:11:11.845377 #29]  INFO -- : Cleared interactions
y

Yousaf Nabi (pactflow.io)

03/04/2022, 1:26 PM
so to switch between
jasmine
and
mocha
I need to change the
beforeAll
/
afterAll
to `before`/`after` . In your script, you are using
expect
are you getting that from
chai
globally?
d

Djalmar Gutierrez

03/04/2022, 1:27 PM
you are right, and comment the jasmine.DEFAULT_…..
y

Yousaf Nabi (pactflow.io)

03/04/2022, 1:29 PM
So this is a run in jasmine, with your code snippet above
Copy code
╰─ npm run test:jasmine                                                                       ─╯

> mochajs-pact-example@1.0.0 test:jasmine
> NODE_ENV=test rimraf pacts && jasmine test/*.jasmine.spec.js

[2022-03-04 13:28:40.017 +0000] INFO (59112 on Yousafs-MacBook-Pro.local): pact-node@10.13.3: Creating Pact Server with options: 
{"consumer":"ares-integration-services","cors":false,"dir":"/Users/saf/dev/pactoss/pact-js/examples/mocha/pacts","host":"127.0.0.1","log":"/Users/saf/dev/pactoss/pact-js/examples/mocha/logs/mockserver-integration.log","pactFileWriteMode":"overwrite","port":1234,"provider":"BSAPI","spec":2,"ssl":false}
Randomized with seed 44043
Started
[2022-03-04 13:28:40.025 +0000] DEBUG (59112 on Yousafs-MacBook-Pro.local): pact-node@10.13.3: Starting pact binary 'standalone/darwin-1.88.63/pact/bin/pact-mock-service', with arguments [service --consumer ares-integration-services --pact_dir /Users/saf/dev/pactoss/pact-js/examples/mocha/pacts --host 127.0.0.1 --log /Users/saf/dev/pactoss/pact-js/examples/mocha/logs/mockserver-integration.log --pact-file-write-mode overwrite --port 1234 --provider BSAPI --pact_specification_version 2]
[2022-03-04 13:28:40.029 +0000] DEBUG (59112 on Yousafs-MacBook-Pro.local): pact-node@10.13.3: Created 'standalone/darwin-1.88.63/pact/bin/pact-mock-service' process with PID: 59113
[2022-03-04 13:28:40.428 +0000] DEBUG (59112 on Yousafs-MacBook-Pro.local): pact-node@10.13.3: INFO  WEBrick 1.3.1
INFO  ruby 2.2.2 (2015-04-13) [x86_64-darwin13]

[2022-03-04 13:28:40.428 +0000] DEBUG (59112 on Yousafs-MacBook-Pro.local): pact-node@10.13.3: INFO  WEBrick::HTTPServer#start: pid=59113 port=1234

[2022-03-04 13:28:40.550 +0000] INFO (59112 on Yousafs-MacBook-Pro.local): pact@9.16.0: Setting up Pact with Consumer "ares-integration-services" and Provider "BSAPI"
    using mock service on Port: "1234"

Pact verification failed!
Actual interactions do not match expected interactions for mock MockService.

Missing requests:
        GET /api


See /Users/saf/dev/pactoss/pact-js/examples/mocha/logs/mockserver-integration.log for details.

F[2022-03-04 13:28:40.570 +0000] INFO (59112 on Yousafs-MacBook-Pro.local): pact@9.16.0: Pact File Written
[2022-03-04 13:28:40.570 +0000] INFO (59112 on Yousafs-MacBook-Pro.local): pact-node@10.13.3: Removing Pact process with PID: 59113
[2022-03-04 13:28:40.571 +0000] INFO (59112 on Yousafs-MacBook-Pro.local): pact-node@10.13.3: Deleting Pact Server with options: 
{"consumer":"ares-integration-services","cors":false,"dir":"/Users/saf/dev/pactoss/pact-js/examples/mocha/pacts","host":"127.0.0.1","log":"/Users/saf/dev/pactoss/pact-js/examples/mocha/logs/mockserver-integration.log","pactFileWriteMode":"overwrite","port":1234,"provider":"BSAPI","spec":2,"ssl":false}
[2022-03-04 13:28:40.572 +0000] DEBUG (59112 on Yousafs-MacBook-Pro.local): pact-node@10.13.3: INFO  going to shutdown ...
INFO  WEBrick::HTTPServer#start done.



Failures:
1) API /api when request is not authenticated should fail requiring authentication
  Message:
    Error: Pact verification failed - expected interactions did not match actual.
  Stack:
        at new VerificationError (/Users/saf/dev/pactoss/pact-js/examples/mocha/node_modules/@pact-foundation/pact/src/errors/verificationError.js:19:42)
        at /Users/saf/dev/pactoss/pact-js/examples/mocha/node_modules/@pact-foundation/pact/src/httpPact.js:108:23
        at processTicksAndRejections (node:internal/process/task_queues:96:5)

1 spec, 1 failure
Finished in 0.554 seconds
Randomized with seed 44043 (jasmine --random=true --seed=44043)
This is my command to start
"test:jasmine": "rimraf pacts && jasmine test/*.jasmine.spec.js"
so it looks like the mock provider started ok here. You mentioned this was also running inside a docker container?
d

Djalmar Gutierrez

03/04/2022, 1:32 PM
Yes, I’m using a debian stretch image as a container
y

Yousaf Nabi (pactflow.io)

03/04/2022, 1:32 PM
I'll try the same versions of pact as you and then can stick this in a container and see what happens.
pact-node@10.17.1
pact@9.17.2
👍 1
got a dockerfile handy?
d

Djalmar Gutierrez

03/04/2022, 1:36 PM
I have it, but it has some sensitive information in it(it would take some time to clear), I’m kinda afraid to share it, sorry
y

Yousaf Nabi (pactflow.io)

03/04/2022, 1:39 PM
No that is absolutely fine my friend, appreciate that, I can setup a debian image easily enough 🙂 I have an example here in fork https://github.com/YOU54F/pact-js/commit/404e83ab4dadbf505711055410aba80a683b1b80 under the jasmineExamples branch. If you go to
examples/mocha
and run
npm i
and then
npm test:jasmine
that is working for me. The mocha version is complaining that
expect(...).toBe is not a function
you can run that with
npm:test
There is a docker example file in pact-js so I will switch that out for debian and see how we go 🙂 https://github.com/pact-foundation/pact-js/tree/feat/docker-example/docker
d

Djalmar Gutierrez

03/04/2022, 2:46 PM
@Yousaf Nabi (pactflow.io) I was able to solve the issue, there was a ‘hidden’ nock configuration that was blocking any connection 🙃, I really appreciate your help, thank you so much.
yay 2
y

Yousaf Nabi (pactflow.io)

03/04/2022, 2:54 PM
Oh fantastic dude! And no problem
57 Views