Skip to content

Commit cae0583

Browse files
chore: Jest snapshot pluging for strip ansi (#1455)
1 parent 316324a commit cae0583

File tree

6 files changed

+43
-45
lines changed

6 files changed

+43
-45
lines changed

jest-setup.ts

-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,3 @@ jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
55
beforeEach(() => {
66
resetToDefaults();
77
});
8-
9-
// Disable colors in our local tests in order to generate clear snapshots
10-
process.env.COLORS = 'false';

jest.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
preset: './jest-preset',
3+
setupFilesAfterEnv: ['./jest-setup.ts'],
4+
testPathIgnorePatterns: [
5+
'timerUtils',
6+
'examples/',
7+
'experiments-app/',
8+
'experiments-rtl/',
9+
],
10+
testTimeout: 60000,
11+
transformIgnorePatterns: [
12+
'/node_modules/(?!(@react-native|react-native)/).*/',
13+
],
14+
snapshotSerializers: ['@relmify/jest-serializer-strip-ansi/always'],
15+
clearMocks: true,
16+
};

package.json

+1-17
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@babel/preset-react": "^7.18.6",
4242
"@babel/preset-typescript": "^7.18.6",
4343
"@callstack/eslint-config": "^13.0.1",
44+
"@relmify/jest-serializer-strip-ansi": "^1.0.2",
4445
"@testing-library/jest-native": "^5.4.0",
4546
"@types/jest": "^29.2.3",
4647
"@types/react": "^18.2.15",
@@ -88,22 +89,5 @@
8889
"build:ts:watch": "yarn build:ts --watch --preserveWatchOutput",
8990
"build": "yarn clean && yarn build:js && yarn build:ts && yarn copy-flowtypes",
9091
"prepare": "yarn build"
91-
},
92-
"jest": {
93-
"preset": "./jest-preset",
94-
"setupFilesAfterEnv": [
95-
"./jest-setup.ts"
96-
],
97-
"testPathIgnorePatterns": [
98-
"timerUtils",
99-
"examples/",
100-
"experiments-app/",
101-
"experiments-rtl/"
102-
],
103-
"testTimeout": 60000,
104-
"transformIgnorePatterns": [
105-
"/node_modules/(?!(@react-native|react-native)/).*/"
106-
],
107-
"clearMocks": true
10892
}
10993
}

src/helpers/format.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const format = (
1616
) =>
1717
prettyFormat(input, {
1818
plugins: [getCustomPlugin(options.mapProps), plugins.ReactElement],
19-
highlight: shouldHighlight(),
19+
highlight: true,
2020
printBasicPrototype: false,
2121
});
2222

@@ -40,8 +40,4 @@ const getCustomPlugin = (mapProps?: MapPropsFunction): NewPlugin => {
4040
};
4141
};
4242

43-
function shouldHighlight() {
44-
return process?.env?.COLORS !== 'false';
45-
}
46-
4743
export default format;

src/queries/__tests__/text.test.tsx

+20-20
Original file line numberDiff line numberDiff line change
@@ -516,38 +516,38 @@ test('error message renders the element tree, preserving only helpful props', as
516516
const view = render(<View accessibilityViewIsModal key="this is filtered" />);
517517

518518
expect(() => view.getByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
519-
"Unable to find an element with text: /foo/
519+
"Unable to find an element with text: /foo/
520520
521-
<View
522-
accessibilityViewIsModal={true}
523-
/>"
524-
`);
521+
<View
522+
accessibilityViewIsModal={true}
523+
/>"
524+
`);
525525

526526
expect(() => view.getAllByText(/foo/)).toThrowErrorMatchingInlineSnapshot(`
527-
"Unable to find an element with text: /foo/
527+
"Unable to find an element with text: /foo/
528528
529-
<View
530-
accessibilityViewIsModal={true}
531-
/>"
532-
`);
529+
<View
530+
accessibilityViewIsModal={true}
531+
/>"
532+
`);
533533

534534
await expect(view.findByText(/foo/)).rejects
535535
.toThrowErrorMatchingInlineSnapshot(`
536-
"Unable to find an element with text: /foo/
536+
"Unable to find an element with text: /foo/
537537
538-
<View
539-
accessibilityViewIsModal={true}
540-
/>"
541-
`);
538+
<View
539+
accessibilityViewIsModal={true}
540+
/>"
541+
`);
542542

543543
await expect(view.findAllByText(/foo/)).rejects
544544
.toThrowErrorMatchingInlineSnapshot(`
545-
"Unable to find an element with text: /foo/
545+
"Unable to find an element with text: /foo/
546546
547-
<View
548-
accessibilityViewIsModal={true}
549-
/>"
550-
`);
547+
<View
548+
accessibilityViewIsModal={true}
549+
/>"
550+
`);
551551
});
552552

553553
test('byText should return host component', () => {

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,11 @@
17911791
resolved "https://registry.yarnpkg.com/@react-native/polyfills/-/polyfills-2.0.0.tgz#4c40b74655c83982c8cf47530ee7dc13d957b6aa"
17921792
integrity sha512-K0aGNn1TjalKj+65D7ycc1//H9roAQ51GJVk5ZJQFb2teECGmzd86bYDC0aYdbRf7gtovescq4Zt6FR0tgXiHQ==
17931793

1794+
"@relmify/jest-serializer-strip-ansi@^1.0.2":
1795+
version "1.0.2"
1796+
resolved "https://registry.yarnpkg.com/@relmify/jest-serializer-strip-ansi/-/jest-serializer-strip-ansi-1.0.2.tgz#97f1cda5046f652bdf8ba7f8669df44b5f9187f7"
1797+
integrity sha512-cJv6weXt7Bk0PEpWNtw0UGOddIbEvNLPlbYb+594xxe5WXs3SSA29tCK57Vg6q46wHEpY1kOCmgQhE8EcIXPKw==
1798+
17941799
"@sideway/address@^4.1.3":
17951800
version "4.1.4"
17961801
resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.4.tgz#03dccebc6ea47fdc226f7d3d1ad512955d4783f0"

0 commit comments

Comments
 (0)