无法在状态中生成参数(PACT 测试问题)

Can't generate params in state (PACT-testing problem)

我用@pact-foundation/pact@latest (for quick deployment of my case there is a simple jest例子)

使用此代码:

我已经生成了这个 PACT 文件:

根据规范3生成(这些是设置):

问题:

如何使最终的 PACT 文件包含 state params?像这样:

Here is an example。这里只是providerStates。但是我想通过官方的PACT库来获取它。

这还不可能。 Pact JS 尚不支持 pact spec v3 的完整功能集。请参阅 https://docs.pact.io/feature_support 了解跨语言的功能覆盖范围。

我写了一个小脚本可以暂时解决这个问题:

fix.js

const folder = "pacts" // folder with tests

const fs = require("fs")

try {
    fs.readdirSync(folder).forEach(fileName => {
        const fullPath = `${folder}/${fileName}`
        const json = JSON.parse(fs.readFileSync(fullPath).toString())

        json.interactions = json.interactions.map((el) => {
            if (Array.isArray(el.providerState)) {
                el.providerStates = el.providerState

                delete el.providerState
            }

            return el
        })

        json.metadata.pactSpecification.version = "3.0.0" // first create tests with specification 2, since matchers are not supported in version 3

        fs.writeFileSync(fullPath, JSON.stringify(json))
    });
} catch (e) {
    console.error(e)
}

要使用执行命令$node fix

或者添加到 package.json,到 scripts 对象,像这样:

package.json

...
"scripts": {
    "test:pact": "jest --config ./pact_jest.config.js && node ./fix.js",
    "publish:pact": "node ./publish.js"
 }
...