Skip to content

Commit

Permalink
Fix/inline snapshot indent (#30)
Browse files Browse the repository at this point in the history
* re-do jest indents with snapshot patch

* add script to remove all inline snapshots

* linter
  • Loading branch information
rlindner81 authored Sep 28, 2023
1 parent 8c26a4c commit 86aac28
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 254 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
],
"scripts": {
"test": "jest",
"test:remove-inline-snapshots": "npx replace '\\.toMatchInlineSnapshot\\(\\s*`[\\s\\S]*?`\\s*\\);' '.toMatchInlineSnapshot();' test -r --include='*.test.js'",
"lint": "npm run prettier && npm run eslint",
"lint:ci": "npm run prettier:ci && npm run eslint:ci",
"eslint": "eslint --fix .",
Expand Down
32 changes: 32 additions & 0 deletions patches/jest-snapshot+29.7.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
diff --git a/node_modules/jest-snapshot/build/InlineSnapshots.js b/node_modules/jest-snapshot/build/InlineSnapshots.js
index 3481ad9..3c227dd 100644
--- a/node_modules/jest-snapshot/build/InlineSnapshots.js
+++ b/node_modules/jest-snapshot/build/InlineSnapshots.js
@@ -193,11 +193,24 @@ const saveSnapshotsForFile = (snapshots, sourceFilePath, rootDir, prettier) => {

// A hack to prevent unexpected line breaks in the generated code
node.loc.end.line = node.loc.start.line;
+ const codeParts = generate(node, {
+ retainLines: true
+ }).code.trim().split("\n");
+ const codeIndented = codeParts
+ .map((part, index) => {
+ switch (index) {
+ case 0:
+ return part;
+ case codeParts.length - 1:
+ return " ".repeat(node.loc.start.column) + part;
+ default:
+ return " ".repeat(node.loc.start.column + 2) + part;
+ }
+ })
+ .join("\n");
return (
sourceSoFar.slice(0, node.start) +
- generate(node, {
- retainLines: true
- }).code.trim() +
+ codeIndented +
sourceSoFar.slice(node.end)
);
},
260 changes: 130 additions & 130 deletions test/integration-local/featureToggles.integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,29 +259,29 @@ describe("local integration test", () => {

expect(featureTogglesLoggerSpy.error).toHaveBeenCalledTimes(0);
expect(featureTogglesLoggerSpy.info.mock.calls).toMatchInlineSnapshot(`
[
[
"finished initialization with %i feature toggle%s with %s",
8,
"s",
"NO_REDIS",
],
]
`);
[
[
"finished initialization with %i feature toggle%s with %s",
8,
"s",
"NO_REDIS",
],
]
`);
expect(featureTogglesLoggerSpy.warning).toHaveBeenCalledTimes(0);
expect(featureTogglesLoggerSpy.error).toHaveBeenCalledTimes(0);
expect(redisWrapperLoggerSpy.info).toHaveBeenCalledTimes(0);
expect(redisWrapperLoggerSpy.warning.mock.calls).toMatchInlineSnapshot(`
[
[
"%s | %O",
"caught error event: connect ECONNREFUSED 127.0.0.1:6379",
{
"clientName": "main",
},
],
]
`);
[
[
"%s | %O",
"caught error event: connect ECONNREFUSED 127.0.0.1:6379",
{
"clientName": "main",
},
],
]
`);
});

it("getFeatureValue, changeFeatureValue without scopes", async () => {
Expand All @@ -295,22 +295,22 @@ describe("local integration test", () => {

expect(await changeFeatureValue(FEATURE.E, newValue)).toBeUndefined();
expect(stateFromInfo(getFeatureInfo(FEATURE.E))).toMatchInlineSnapshot(`
{
"rootValue": 9,
}
`);
{
"rootValue": 9,
}
`);
expect(getFeatureValue(FEATURE.E)).toEqual(newValue);

expect(featureTogglesLoggerSpy.info.mock.calls).toMatchInlineSnapshot(`
[
[
"finished initialization with %i feature toggle%s with %s",
8,
"s",
"NO_REDIS",
],
]
`);
[
[
"finished initialization with %i feature toggle%s with %s",
8,
"s",
"NO_REDIS",
],
]
`);
expect(featureTogglesLoggerSpy.warning).toHaveBeenCalledTimes(0);
expect(featureTogglesLoggerSpy.error).toHaveBeenCalledTimes(0);
expect(redisWrapperLoggerSpy.info).toHaveBeenCalledTimes(0);
Expand Down Expand Up @@ -385,72 +385,72 @@ describe("local integration test", () => {

expect(await changeFeatureValue(FEATURE.E, scopeNewValue, scopeMap)).toBeUndefined();
expect(stateFromInfo(getFeatureInfo(FEATURE.E))).toMatchInlineSnapshot(`
{
"scopedValues": {
"component::c1##tenant::t1": 3,
},
}
`);
{
"scopedValues": {
"component::c1##tenant::t1": 3,
},
}
`);
expect(getFeatureValue(FEATURE.E, subScopeMap)).toEqual(scopeNewValue);
expect(getFeatureValue(FEATURE.E, scopeMap)).toEqual(scopeNewValue);
expect(getFeatureValue(FEATURE.E, superScopeMap)).toEqual(rootOldValue);
expect(getFeatureValue(FEATURE.E)).toEqual(rootOldValue);

expect(await changeFeatureValue(FEATURE.E, superScopeNewValue, superScopeMap)).toBeUndefined();
expect(stateFromInfo(getFeatureInfo(FEATURE.E))).toMatchInlineSnapshot(`
{
"scopedValues": {
"component::c1##tenant::t1": 3,
"tenant::t1": 2,
},
}
`);
{
"scopedValues": {
"component::c1##tenant::t1": 3,
"tenant::t1": 2,
},
}
`);
expect(getFeatureValue(FEATURE.E, subScopeMap)).toEqual(scopeNewValue);
expect(getFeatureValue(FEATURE.E, scopeMap)).toEqual(scopeNewValue);
expect(getFeatureValue(FEATURE.E, superScopeMap)).toEqual(superScopeNewValue);
expect(getFeatureValue(FEATURE.E)).toEqual(rootOldValue);

expect(await changeFeatureValue(FEATURE.E, subScopeNewValue, subScopeMap)).toBeUndefined();
expect(stateFromInfo(getFeatureInfo(FEATURE.E))).toMatchInlineSnapshot(`
{
"scopedValues": {
"component::c1##layer::l1##tenant::t1": 4,
"component::c1##tenant::t1": 3,
"tenant::t1": 2,
},
}
`);
{
"scopedValues": {
"component::c1##layer::l1##tenant::t1": 4,
"component::c1##tenant::t1": 3,
"tenant::t1": 2,
},
}
`);
expect(getFeatureValue(FEATURE.E, subScopeMap)).toEqual(subScopeNewValue);
expect(getFeatureValue(FEATURE.E, scopeMap)).toEqual(scopeNewValue);
expect(getFeatureValue(FEATURE.E, superScopeMap)).toEqual(superScopeNewValue);
expect(getFeatureValue(FEATURE.E)).toEqual(rootOldValue);

expect(await changeFeatureValue(FEATURE.E, rootNewValue)).toBeUndefined();
expect(stateFromInfo(getFeatureInfo(FEATURE.E))).toMatchInlineSnapshot(`
{
"rootValue": 1,
"scopedValues": {
"component::c1##layer::l1##tenant::t1": 4,
"component::c1##tenant::t1": 3,
"tenant::t1": 2,
},
}
`);
{
"rootValue": 1,
"scopedValues": {
"component::c1##layer::l1##tenant::t1": 4,
"component::c1##tenant::t1": 3,
"tenant::t1": 2,
},
}
`);
expect(getFeatureValue(FEATURE.E, subScopeMap)).toEqual(subScopeNewValue);
expect(getFeatureValue(FEATURE.E, scopeMap)).toEqual(scopeNewValue);
expect(getFeatureValue(FEATURE.E, superScopeMap)).toEqual(superScopeNewValue);
expect(getFeatureValue(FEATURE.E)).toEqual(rootNewValue);

expect(featureTogglesLoggerSpy.info.mock.calls).toMatchInlineSnapshot(`
[
[
"finished initialization with %i feature toggle%s with %s",
8,
"s",
"NO_REDIS",
],
]
`);
[
[
"finished initialization with %i feature toggle%s with %s",
8,
"s",
"NO_REDIS",
],
]
`);
expect(featureTogglesLoggerSpy.warning).toHaveBeenCalledTimes(0);
expect(featureTogglesLoggerSpy.error).toHaveBeenCalledTimes(0);
expect(redisWrapperLoggerSpy.info).toHaveBeenCalledTimes(0);
Expand All @@ -474,30 +474,30 @@ describe("local integration test", () => {
expect(await changeFeatureValue(FEATURE.E, subScopeNewValue, subScopeMap)).toBeUndefined();
expect(await changeFeatureValue(FEATURE.E, rootNewValue)).toBeUndefined();
expect(stateFromInfo(getFeatureInfo(FEATURE.E))).toMatchInlineSnapshot(`
{
"rootValue": 1,
"scopedValues": {
"component::c1##layer::l1##tenant::t1": 4,
"component::c1##tenant::t1": 3,
"tenant::t1": 2,
},
}
`);
{
"rootValue": 1,
"scopedValues": {
"component::c1##layer::l1##tenant::t1": 4,
"component::c1##tenant::t1": 3,
"tenant::t1": 2,
},
}
`);
expect(getFeatureValue(FEATURE.E, subScopeMap)).toEqual(subScopeNewValue);
expect(getFeatureValue(FEATURE.E, scopeMap)).toEqual(scopeNewValue);
expect(getFeatureValue(FEATURE.E, superScopeMap)).toEqual(superScopeNewValue);
expect(getFeatureValue(FEATURE.E)).toEqual(rootNewValue);

expect(await changeFeatureValue(FEATURE.E, scopeNewValue, scopeMap, { clearSubScopes: true })).toBeUndefined();
expect(stateFromInfo(getFeatureInfo(FEATURE.E))).toMatchInlineSnapshot(`
{
"rootValue": 1,
"scopedValues": {
"component::c1##tenant::t1": 3,
"tenant::t1": 2,
},
}
`);
{
"rootValue": 1,
"scopedValues": {
"component::c1##tenant::t1": 3,
"tenant::t1": 2,
},
}
`);
expect(getFeatureValue(FEATURE.E, subScopeMap)).toEqual(scopeNewValue);
expect(getFeatureValue(FEATURE.E, scopeMap)).toEqual(scopeNewValue);
expect(getFeatureValue(FEATURE.E, superScopeMap)).toEqual(superScopeNewValue);
Expand All @@ -511,15 +511,15 @@ describe("local integration test", () => {
expect(getFeatureValue(FEATURE.E)).toEqual(rootOldValue);

expect(featureTogglesLoggerSpy.info.mock.calls).toMatchInlineSnapshot(`
[
[
"finished initialization with %i feature toggle%s with %s",
8,
"s",
"NO_REDIS",
],
]
`);
[
[
"finished initialization with %i feature toggle%s with %s",
8,
"s",
"NO_REDIS",
],
]
`);
expect(featureTogglesLoggerSpy.warning).toHaveBeenCalledTimes(0);
expect(featureTogglesLoggerSpy.error).toHaveBeenCalledTimes(0);
expect(redisWrapperLoggerSpy.info).toHaveBeenCalledTimes(0);
Expand Down Expand Up @@ -592,41 +592,41 @@ describe("local integration test", () => {
expect(await validateFeatureValue(FEATURE.C, "")).toMatchInlineSnapshot(`[]`);
registerFeatureValueValidation(FEATURE.C, failingValidator1);
expect(await validateFeatureValue(FEATURE.C, "")).toMatchInlineSnapshot(`
[
{
"errorMessage": "registered validator "{0}" failed for value "{1}" with error {2}",
"errorMessageValues": [
"failingValidator1",
"",
"bla1",
],
"featureKey": "test/feature_c",
},
]
`);
[
{
"errorMessage": "registered validator "{0}" failed for value "{1}" with error {2}",
"errorMessageValues": [
"failingValidator1",
"",
"bla1",
],
"featureKey": "test/feature_c",
},
]
`);
registerFeatureValueValidation(FEATURE.C, failingValidator2);
expect(await validateFeatureValue(FEATURE.C, "")).toMatchInlineSnapshot(`
[
{
"errorMessage": "registered validator "{0}" failed for value "{1}" with error {2}",
"errorMessageValues": [
"failingValidator1",
"",
"bla1",
],
"featureKey": "test/feature_c",
},
{
"errorMessage": "registered validator "{0}" failed for value "{1}" with error {2}",
"errorMessageValues": [
"failingValidator2",
"",
"bla2",
],
"featureKey": "test/feature_c",
},
]
`);
[
{
"errorMessage": "registered validator "{0}" failed for value "{1}" with error {2}",
"errorMessageValues": [
"failingValidator1",
"",
"bla1",
],
"featureKey": "test/feature_c",
},
{
"errorMessage": "registered validator "{0}" failed for value "{1}" with error {2}",
"errorMessageValues": [
"failingValidator2",
"",
"bla2",
],
"featureKey": "test/feature_c",
},
]
`);
});
});
});
Loading

0 comments on commit 86aac28

Please sign in to comment.