When I run my Pact tests with jest I keep seeing: ...
# pact-js
j
When I run my Pact tests with jest I keep seeing:
Copy code
pact-core@15.1.1: The pact native core has already been initialised at log level 'warn' log level 'trace' will be ignored
I can't find much info on this, I tried setting the LOG_LEVEL env var, I tried this:
Copy code
var pact = require("@pact-foundation/pact-core");
pact.logLevel("debug");
But to no avail so far, I think it has something to do with Jest? I set my Pact options like so:
Copy code
pactWith({ consumer: 'Frontend', provider: 'BE' }, provider => {
  beforeAll(() => {
    // @ts-ignore
    global.fetch = fetch
    // @ts-ignore
    global.Response = Response
    store = setupStore()

    provider.opts = {
      ssl: false,
      host: backendUrl.hostname,
      consumer: 'Frontend',
      provider: 'BE',
      logLevel: 'trace',
      dir: path.resolve(process.cwd(), 'pacts'), // Output directory
      spec: SpecificationVersion.SPECIFICATION_VERSION_V3, // Consider using V2, it doesn't require anything extra and is considered stable
      port: Number(backendUrl.port),
    }
Full log in thread >
Copy code
RUNS  gatsby/src/state/accountV2/tests/customerSlice.test.ts
[07:51:27.795] DEBUG (1524): pact-core@15.1.1: We detected your platform as: 

 RUNS  gatsby/src/state/accountV2/tests/customerSlice.test.ts
2024-08-02T05:51:28.157000Z  WARN ThreadId(02) pact_models::pact: Note: Existing pact is an older specification version (V2), and will be upgraded
[07:51:28.157] WARN (1524): pact-core@15.1.1: The pact native core has already been initialised at log level 'warn'
 PASS  gatsby/src/state/accountV2/tests/customerSlice.test.tsed log level 'trace' will be ignored
  Pact between Frontend and BE
    with 30000 ms timeout for Pact
      ✓ Customer fetching works and results are cached properly (18 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.268 s
Ran all test suites matching /\/Users\/jorvd\/dev\/DeepL\/frontend_repo\/frontend_vscode\/main\/gatsby\/src\/state\/accountV2\/tests\/customerSlice.test.ts/i.
jorvd@Jors-MacBook-Pro main %
m
hmm might be the jest-pact setup, I’m not overly familiar with it. Basically the log level can’t be modified after being set
j
Thank you Matt, that got me exactly in the right direction, it's embarrassingly simple:
Copy code
pactWith({ consumer: 'Frontend', provider: 'BE', logLevel: 'trace' }, provider => {
When using
ject-pact
set your options in the p*actWith* function, not as
provider.opts
since they get overwritten
👍 1
m
ah, thanks for letting us know.
BTW you probably don’t need jest pact anymore with the latest Pact JS, the additional DSL is not adding much. You can use it, of course, if you prefer it
j
That's good to know thank you. We're currently looking into getting the enterprise plan for our company, which has a huge FE repo. I'm just a QA there and no frontend dev and trying convert some of our mocks to Pact. Somehow the only way I got it to work was using jest-pact, but I will see if I can try again to go with 'regular' pact, since it's much better documented as well.
👍 1
m
Don’t sell yourself short my friend - but if you need any help, we’re here 🙂
❤️ 1
j
Thank you Matt, I might make use of that offer already: Trying to switch from Consumer first testing to BDCT, and therefore set my pactflow credentials up in env vars. I'm getting this error though:
Copy code
Error making request to <https://deepl.pactflow.io/provider-contracts/provider/BE-openapi-spec/publish> status=400 {"title":"Validation errors","type":"<https://problems-registry.smartbear.com/validation-error>","status":400,"instance":"/","errors":[{"detail":"is missing","pointer":"#/contract/contentType"}]}
It seem like a pretty clear error but I can't find where to set this contentType, any suggestions? I'm using this Makefile to run a single test (which passes) after which I'm trying to publish, this is the step that fails:
Copy code
PACTICIPANT ?= "BE-openapi-spec"

## ====================
## Pactflow Provider Publishing
## ====================
PACT_CLI="docker run --rm -v ${PWD}:/app -w "/app" -e PACT_BROKER_BASE_URL -e PACT_BROKER_TOKEN pactfoundation/pact-cli"
OAS_FILE_PATH?=be-openapi.json
OAS_FILE_CONTENT_TYPE?=application/json

REPORT_FILE_PATH?=api/README.md
REPORT_FILE_CONTENT_TYPE?=text/markdown
VERIFIER_TOOL?=newman

# Export all variable to sub-make if .env exists
ifneq (,$(wildcard ./.env))
    include .env
    export
endif

default:
	cat ./Makefile

ci: ci-test publish_pacts can_i_deploy

# Run the ci target from a developer machine with the environment variables
# set as if it was on CI.
# Use this for quick feedback when playing around with your workflows.
fake_ci:
	@CI=true \
	GIT_COMMIT=`git rev-parse --short HEAD` \
	GIT_BRANCH=`git rev-parse --abbrev-ref HEAD` \
	make ci

## =====================
## Build/test tasks
## =====================

ci-test:
	@echo "\n========== STAGE: CI Tests ==========\n"
	node 'node_modules/.bin/jest' '/Users/jorvd/dev/company/frontend_repo/frontend_vscode/main/gatsby/src/state/accountV2/tests/customerSlice2.test.ts'     

## =====================
## Pact tasks
## =====================

publish_pacts:
	@echo "\n========== STAGE: publish provider contract (spec + results) - success ==========\n"
	PACTICIPANT=${PACTICIPANT} \
	"${PACT_CLI}" pactflow publish-provider-contract \
	/app/${OAS_FILE_PATH} \
	--provider ${PACTICIPANT} \
	--provider-app-version ${GIT_COMMIT} \
	

deploy: deploy_app record_deployment

can_i_deploy:
	@echo "\n========== STAGE: can-i-deploy? 🌉 ==========\n"
	"${PACT_CLI}" broker can-i-deploy --pacticipant ${PACTICIPANT} --version ${GIT_COMMIT} --to-environment test

deploy_app:
	@echo "\n========== STAGE: deploy ==========\n"
	@echo "Deploying to test"

record_deployment:
	"${PACT_CLI}" broker record-deployment --pacticipant ${PACTICIPANT} --version ${GIT_COMMIT} --environment test

## =====================
## Misc
## =====================

.env:
	cp -n .env.example .env || true
Sorry nevermind, it was again simple, just needed to add:
Copy code
--content-type ${OAS_FILE_CONTENT_TYPE} \
👍 1