Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset basenames in Jest snapshots #13319

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renders assets based on relative path 1`] = `
<View>
<Image
source={
Object {
"testUri": "Libraries/Image/__tests__/img/img1.png",
}
}
/>
<Image
source={
Object {
"testUri": "Libraries/Image/__tests__/img/img2.png",
}
}
/>
</View>
`;
25 changes: 25 additions & 0 deletions Libraries/Image/__tests__/assetRelativePathInSnapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

jest.disableAutomock();

const React = require('React');
const ReactTestRenderer = require('react-test-renderer');
const Image = require('Image');
const View = require('View');

it('renders assets based on relative path', () => {
expect(ReactTestRenderer.create(
<View>
<Image source={require('./img/img1.png')} />
<Image source={require('./img/img2.png')} />
</View>
)).toMatchSnapshot();
});
Binary file added Libraries/Image/__tests__/img/img1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Libraries/Image/__tests__/img/img2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion jest-preset.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
]
},
"moduleNameMapper": {
"^[./a-zA-Z0-9$_-]+\\.(bmp|gif|jpg|jpeg|png|psd|svg|webp)$": "RelativeImageStub",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this broke importing files with multiple device resolution variations. Metro bundler will resolve require('./file.png') to ./file@2x.png if you're on a 2x screen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reporting! Could you give a bit more detail what the problem is? Apparently this change has broken your tests. Could you give some steps to reproduce?

I immediately notice that the regex ^[./a-zA-Z0-9$_-]+\\.(bmp|gif|jpg|jpeg|png|psd|svg|webp)$ does not match the @ character in ./file@2x.png. The regex has been moved from moduleNameMapper to the transform mapper on line 17 in the same file. Perhaps the change from ./file.png to ./file@2x.png happens between the mappers. @oblador, could you edit the regex on line 17 to ^[./a-zA-Z0-9@$_-]+\\.(bmp|gif|jpg|jpeg|png|psd|svg|webp)$, so that it also matches ./file@2x.png?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also breaks tests completely on Windows for me. Please see #19370 for details

"^React$": "<rootDir>/node_modules/react"
},
"modulePathIgnorePatterns": [
"<rootDir>/node_modules/react-native/Libraries/react-native/"
],
"transform": {
"^.+\\.js$": "babel-jest",
"^[./a-zA-Z0-9$_-]+\\.(bmp|gif|jpg|jpeg|png|psd|svg|webp)$": "<rootDir>/node_modules/react-native/jest/assetFileTransformer.js"
},
"transformIgnorePatterns": [
"node_modules/(?!(jest-)?react-native|react-clone-referenced-element)"
],
Expand Down
26 changes: 26 additions & 0 deletions jest/assetFileTransformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

/* eslint-env node */

const path = require('path');
const createCacheKeyFunction = require('fbjs-scripts/jest/createCacheKeyFunction');

module.exports = {
// Mocks asset requires to return the filename. Makes it possible to test that
// the correct images are loaded for components. Essentially
// require('img1.png') becomes `Object { "testUri": 'path/to/img1.png' }` in
// the Jest snapshot.
process: (_, filename) =>
`module.exports = {
testUri: ${JSON.stringify(path.relative('.', filename))}
};`,
getCacheKey: createCacheKeyFunction([__filename]),
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
},
"jest": {
"transform": {
"^[./a-zA-Z0-9$_-]+\\.(bmp|gif|jpg|jpeg|png|psd|svg|webp)$": "<rootDir>/jest/assetFileTransformer.js",
".*": "./jest/preprocessor.js"
},
"setupFiles": [
"./jest/setup.js"
],
"timers": "fake",
"moduleNameMapper": {
"^React$": "<rootDir>/Libraries/react-native/React.js",
"^[./a-zA-Z0-9$_-]+\\.png$": "RelativeImageStub"
"^React$": "<rootDir>/Libraries/react-native/React.js"
},
"testPathIgnorePatterns": [
"Libraries/Renderer",
Expand Down