我正在运行 pact-js 存储库中的消费者示例。重现下面的示例(我修改了文件以避免打字稿):
import { PactV3, MatchersV3 } from '@pact-foundation/pact';
import path from 'path';
// Create a 'pact' between the two applications in the integration we are testing
const provider = new PactV3({
dir: path.resolve(process.cwd(), 'pacts'),
consumer: 'MyConsumer',
provider: 'MyProvider',
});
// API Client that will fetch dogs from the Dog API
// This is the target of our Pact test
getMeDogs = (from) => {
return axios.request({
baseURL: this.url,
params: { from },
headers: { Accept: 'application/json' },
method: 'GET',
url: '/dogs',
});
};
const dogExample = { dog: 1 };
const EXPECTED_BODY = MatchersV3.eachLike(dogExample);
describe('GET /dogs', () => {
it('returns an HTTP 200 and a list of docs', () => {
// Arrange: Setup our expected interactions
//
// We use Pact to mock out the backend API
provider
.given('I have a list of dogs')
.uponReceiving('a request for all dogs with the builder pattern')
.withRequest({
method: 'GET',
path: '/dogs',
query: { from: 'today' },
headers: { Accept: 'application/json' },
})
.willRespondWith({
status: 200,
headers: { 'Content-Type': 'application/json' },
body: EXPECTED_BODY,
});
return provider.executeTest( async (mockserver) => {
// Act: test our API client behaves correctly
//
// Note we configure the DogService API client dynamically to
// point to the mock service Pact created for us, instead of
// the real one
dogService = new DogService(mockserver.url);
const response = await dogService.getMeDogs('today')
// Assert: check the result
expect(response.data[0]).to.deep.eq(dogExample);
});
});
});
我创建了一个新目录并执行了
npm init
。我的 package.json 看起来像:
{
"name": "repo1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@pact-foundation/pact": "^12.1.1"
},
"type": "module"
}
还有几件事:
运行上面的示例(保存到 cons.js)会生成以下错误:
[15:38:11.420] ERROR (28229): [email protected]: Failed to find native module for darwin-x64: TypeError: Cannot read properties of undefined (reading 'pactffiInitWithLogLevel')
有关如何运行此程序的任何指示都会有所帮助。
我必须升级笔记本电脑上的操作系统版本。操作系统升级后安装node、npm、Xcode开发工具后,我不再收到错误。
我知道OP有macos,但我在docker中使用
node:18-alpine
图像运行时遇到了同样的问题。我的解决方案是迁移到稍微重一点的图像:node:18-slim
。
根本原因隐藏在吞咽异常中:
Error: Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /app/node_modules/@pact-foundation/pact-core/prebuilds/linux-x64/libpact_ffi.so)
at Object.Module._extensions..node (node:internal/modules/cjs/loader:1452:18)
at Module.load (node:internal/modules/cjs/loader:1197:32)
此文件未在
node:18-alpine
中呈现。
调试它的另一个指针是
bash
进入docker镜像并在libpact二进制文件上运行ldd
(更改你的路径):
ldd /app/node_modules/@pact-foundation/pact-core/prebuilds/linux-x64/libpact_ffi.so
如果缺少所需的依赖库,您将看到错误:
ldd /app/node_modules/@pact-foundation/pact-core/prebuilds/linux-x64/libpact_ffi.so
/lib/ld-musl-x86_64.so.1 (0x7f478f89c000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x7f478dd99000)
librt.so.1 => /lib/ld-musl-x86_64.so.1 (0x7f478f89c000)
libpthread.so.0 => /lib/ld-musl-x86_64.so.1 (0x7f478f89c000)
libm.so.6 => /lib/ld-musl-x86_64.so.1 (0x7f478f89c000)
libdl.so.2 => /lib/ld-musl-x86_64.so.1 (0x7f478f89c000)
libc.so.6 => /lib/ld-musl-x86_64.so.1 (0x7f478f89c000)
Error loading shared library ld-linux-x86-64.so.2: No such file or directory (needed by /app/node_modules/@pact-foundation/pact-core/prebuilds/linux-x64/libpact_ffi.so)
Error relocating /app/node_modules/@pact-foundation/pact-core/prebuilds/linux-x64/libpact_ffi.so: gnu_get_libc_version: symbol not found