Improve Polly.js config (#3040)

* Fix Polly.js configuration

* Update test ids

* Fix CR comments

* Remove env var
This commit is contained in:
Jonatan Witoszek 2023-02-06 14:05:45 +01:00 committed by GitHub
parent 84849f1eea
commit 2ddbb8bf9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 15 deletions

37
package-lock.json generated
View file

@ -116,6 +116,7 @@
"@types/apollo-upload-client": "^17.0.0", "@types/apollo-upload-client": "^17.0.0",
"@types/color-convert": "^2.0.0", "@types/color-convert": "^2.0.0",
"@types/fuzzaldrin": "^2.1.2", "@types/fuzzaldrin": "^2.1.2",
"@types/is-ci": "^3.0.0",
"@types/jscodeshift": "^0.11.3", "@types/jscodeshift": "^0.11.3",
"@types/lodash-es": "^4.17.3", "@types/lodash-es": "^4.17.3",
"@types/pollyjs__adapter-node-http": "^2.0.1", "@types/pollyjs__adapter-node-http": "^2.0.1",
@ -9025,6 +9026,15 @@
"integrity": "sha512-c3Xy026kOF7QOTn00hbIllV1dLR9hG9NkSrLQgCVs8NF6sBU+VGWjD3wLPhmh1TYAc7ugCFsvHYMN4VcBN1U1A==", "integrity": "sha512-c3Xy026kOF7QOTn00hbIllV1dLR9hG9NkSrLQgCVs8NF6sBU+VGWjD3wLPhmh1TYAc7ugCFsvHYMN4VcBN1U1A==",
"dev": true "dev": true
}, },
"node_modules/@types/is-ci": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@types/is-ci/-/is-ci-3.0.0.tgz",
"integrity": "sha512-Q0Op0hdWbYd1iahB+IFNQcWXFq4O0Q5MwQP7uN0souuQ4rPg1vEYcnIOfr1gY+M+6rc8FGoRaBO1mOOvL29sEQ==",
"dev": true,
"dependencies": {
"ci-info": "^3.1.0"
}
},
"node_modules/@types/is-function": { "node_modules/@types/is-function": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/@types/is-function/-/is-function-1.0.0.tgz", "resolved": "https://registry.npmjs.org/@types/is-function/-/is-function-1.0.0.tgz",
@ -13584,10 +13594,16 @@
} }
}, },
"node_modules/ci-info": { "node_modules/ci-info": {
"version": "3.7.0", "version": "3.7.1",
"resolved": "https://registry.npmjs.org/ci-info/-/ci-info-3.7.0.tgz", "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-3.7.1.tgz",
"integrity": "sha512-2CpRNYmImPx+RXKLq6jko/L07phmS9I02TyqkcNU20GCF/GgaWvc58hPtjxDX8lPpkdwc9sNh72V9k00S7ezog==", "integrity": "sha512-4jYS4MOAaCIStSRwiuxc4B8MYhIe676yO1sYGzARnjXkWpmzZMMYxY6zu8WYWDhSuth5zhrQ1rhNSibyyvv4/w==",
"devOptional": true, "devOptional": true,
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/sibiraj-s"
}
],
"engines": { "engines": {
"node": ">=8" "node": ">=8"
} }
@ -46007,6 +46023,15 @@
"integrity": "sha512-c3Xy026kOF7QOTn00hbIllV1dLR9hG9NkSrLQgCVs8NF6sBU+VGWjD3wLPhmh1TYAc7ugCFsvHYMN4VcBN1U1A==", "integrity": "sha512-c3Xy026kOF7QOTn00hbIllV1dLR9hG9NkSrLQgCVs8NF6sBU+VGWjD3wLPhmh1TYAc7ugCFsvHYMN4VcBN1U1A==",
"dev": true "dev": true
}, },
"@types/is-ci": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/@types/is-ci/-/is-ci-3.0.0.tgz",
"integrity": "sha512-Q0Op0hdWbYd1iahB+IFNQcWXFq4O0Q5MwQP7uN0souuQ4rPg1vEYcnIOfr1gY+M+6rc8FGoRaBO1mOOvL29sEQ==",
"dev": true,
"requires": {
"ci-info": "^3.1.0"
}
},
"@types/is-function": { "@types/is-function": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/@types/is-function/-/is-function-1.0.0.tgz", "resolved": "https://registry.npmjs.org/@types/is-function/-/is-function-1.0.0.tgz",
@ -49815,9 +49840,9 @@
} }
}, },
"ci-info": { "ci-info": {
"version": "3.7.0", "version": "3.7.1",
"resolved": "https://registry.npmjs.org/ci-info/-/ci-info-3.7.0.tgz", "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-3.7.1.tgz",
"integrity": "sha512-2CpRNYmImPx+RXKLq6jko/L07phmS9I02TyqkcNU20GCF/GgaWvc58hPtjxDX8lPpkdwc9sNh72V9k00S7ezog==", "integrity": "sha512-4jYS4MOAaCIStSRwiuxc4B8MYhIe676yO1sYGzARnjXkWpmzZMMYxY6zu8WYWDhSuth5zhrQ1rhNSibyyvv4/w==",
"devOptional": true "devOptional": true
}, },
"cipher-base": { "cipher-base": {

View file

@ -124,6 +124,7 @@
"@types/apollo-upload-client": "^17.0.0", "@types/apollo-upload-client": "^17.0.0",
"@types/color-convert": "^2.0.0", "@types/color-convert": "^2.0.0",
"@types/fuzzaldrin": "^2.1.2", "@types/fuzzaldrin": "^2.1.2",
"@types/is-ci": "^3.0.0",
"@types/jscodeshift": "^0.11.3", "@types/jscodeshift": "^0.11.3",
"@types/lodash-es": "^4.17.3", "@types/lodash-es": "^4.17.3",
"@types/pollyjs__adapter-node-http": "^2.0.1", "@types/pollyjs__adapter-node-http": "^2.0.1",

View file

@ -8,7 +8,7 @@
}, },
"entries": [ "entries": [
{ {
"_id": "a1435ddf90b7742bd6534592ad110e86", "_id": "916a39b70c19064326e4caf3e7a38f9d",
"_order": 0, "_order": 0,
"cache": {}, "cache": {},
"request": { "request": {

View file

@ -8,7 +8,7 @@
}, },
"entries": [ "entries": [
{ {
"_id": "414a8e773ab89cc536de866e780f5320", "_id": "916a39b70c19064326e4caf3e7a38f9d",
"_order": 0, "_order": 0,
"cache": {}, "cache": {},
"request": { "request": {

View file

@ -8,7 +8,7 @@
}, },
"entries": [ "entries": [
{ {
"_id": "81637bd419aedd21a86324a846d9d163", "_id": "916a39b70c19064326e4caf3e7a38f9d",
"_order": 0, "_order": 0,
"cache": {}, "cache": {},
"request": { "request": {

View file

@ -1,30 +1,55 @@
import { ApolloClient, InMemoryCache } from "@apollo/client"; import { ApolloClient, InMemoryCache } from "@apollo/client";
import { BatchHttpLink } from "@apollo/client/link/batch-http"; import { BatchHttpLink } from "@apollo/client/link/batch-http";
import { getApiUrl } from "@dashboard/config"; import { getApiUrl } from "@dashboard/config";
import NodeHttpAdapter from "@pollyjs/adapter-node-http";
import FSPersister from "@pollyjs/persister-fs";
import { createFetch } from "@saleor/sdk"; import { createFetch } from "@saleor/sdk";
import isCI from "is-ci";
import path from "path"; import path from "path";
import { setupPolly } from "setup-polly-jest"; import { setupPolly } from "setup-polly-jest";
const POLLY_MODES = ["replay", "record", "passthrough", "stopped"] as const;
function getPollyMode() {
const env = process.env.POLLY_MODE as typeof POLLY_MODES[number];
if (!env) {
return POLLY_MODES[0]; // replay
}
if (POLLY_MODES.includes(env)) {
return env;
}
console.warn(`Unrecognised POLLY_MODE env variable value: ${env}`);
return POLLY_MODES[0]; // replay
}
function getPollyRecordIfMissing() {
const env = process.env.POLLY_RECORD_IF_MISSING;
if (!env) {
return !isCI;
}
return env === "true";
}
function setupApi() { function setupApi() {
setupPolly({ setupPolly({
adapters: [NodeHttpAdapter], adapters: [require("@pollyjs/adapter-node-http")],
matchRequestsBy: { matchRequestsBy: {
headers: false, headers: false,
url: { url: {
hash: false, hash: false,
hostname: false, hostname: true,
password: false, password: false,
pathname: false, pathname: true,
port: false, port: false,
protocol: false, protocol: false,
query: false, query: false,
username: false, username: false,
}, },
body: false,
}, },
persister: FSPersister, mode: getPollyMode(),
recordIfMissing: getPollyRecordIfMissing(),
persister: require("@pollyjs/persister-fs"),
persisterOptions: { persisterOptions: {
keepUnusedRequests: false,
fs: { fs: {
recordingsDir: path.resolve(__dirname, "../recordings"), recordingsDir: path.resolve(__dirname, "../recordings"),
}, },