|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import {tmpdir} from 'os'; |
| 9 | +import * as path from 'path'; |
| 10 | +import { |
| 11 | + JestPackageJson, |
| 12 | + cleanup, |
| 13 | + createEmptyPackage, |
| 14 | + runYarnInstall, |
| 15 | + writeFiles, |
| 16 | +} from '../Utils'; |
| 17 | +import runJest, {json as runWithJson} from '../runJest'; |
| 18 | + |
| 19 | +const DIR = path.resolve(tmpdir(), 'to-match-inline-snapshot-with-jsx'); |
| 20 | + |
| 21 | +const babelConfig = { |
| 22 | + presets: [ |
| 23 | + ['@babel/preset-env', {targets: {node: 'current'}}], |
| 24 | + '@babel/preset-react', |
| 25 | + ], |
| 26 | +}; |
| 27 | + |
| 28 | +const pkg: JestPackageJson = { |
| 29 | + dependencies: { |
| 30 | + react: '^17.0.0', |
| 31 | + }, |
| 32 | + devDependencies: { |
| 33 | + '@babel/core': '^7.14.4', |
| 34 | + '@babel/preset-env': '^7.14.4', |
| 35 | + '@babel/preset-react': '^7.13.13', |
| 36 | + 'react-test-renderer': '^17.0.2', |
| 37 | + }, |
| 38 | + jest: { |
| 39 | + testEnvironment: 'jsdom', |
| 40 | + }, |
| 41 | +}; |
| 42 | + |
| 43 | +beforeEach(() => { |
| 44 | + cleanup(DIR); |
| 45 | + |
| 46 | + createEmptyPackage(DIR, pkg); |
| 47 | + |
| 48 | + writeFiles(DIR, { |
| 49 | + '__tests__/MismatchingSnapshot.test.js': ` |
| 50 | + import React from 'react'; |
| 51 | + import renderer from 'react-test-renderer'; |
| 52 | +
|
| 53 | + test('<div>x</div>', () => { |
| 54 | + expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\` |
| 55 | + <div> |
| 56 | + y |
| 57 | + </div> |
| 58 | + \`); |
| 59 | + });`, |
| 60 | + }); |
| 61 | + |
| 62 | + runYarnInstall(DIR, { |
| 63 | + YARN_ENABLE_GLOBAL_CACHE: 'true', |
| 64 | + YARN_NODE_LINKER: 'node-modules', |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +afterAll(() => { |
| 69 | + cleanup(DIR); |
| 70 | +}); |
| 71 | + |
| 72 | +it('successfully runs the tests with external babel config', () => { |
| 73 | + writeFiles(DIR, { |
| 74 | + 'babel.config.js': `module.exports = ${JSON.stringify(babelConfig)};`, |
| 75 | + }); |
| 76 | + |
| 77 | + const normalRun = runWithJson(DIR, []); |
| 78 | + expect(normalRun.exitCode).toBe(1); |
| 79 | + expect(normalRun.stderr).toContain('1 snapshot failed from 1 test suite.'); |
| 80 | + expect(normalRun.json.testResults[0].message).toMatchInlineSnapshot(` |
| 81 | + " ● <div>x</div> |
| 82 | +
|
| 83 | + expect(received).toMatchInlineSnapshot(snapshot) |
| 84 | +
|
| 85 | + Snapshot name: \`<div>x</div> 1\` |
| 86 | +
|
| 87 | + - Snapshot - 1 |
| 88 | + + Received + 1 |
| 89 | +
|
| 90 | + <div> |
| 91 | + - y |
| 92 | + + x |
| 93 | + </div> |
| 94 | +
|
| 95 | + 3 | |
| 96 | + 4 | test('<div>x</div>', () => { |
| 97 | + > 5 | expect(renderer.create(<div>x</div>).toJSON()).toMatchInlineSnapshot(\` |
| 98 | + | ^ |
| 99 | + 6 | <div> |
| 100 | + 7 | y |
| 101 | + 8 | </div> |
| 102 | +
|
| 103 | + at Object.toMatchInlineSnapshot (__tests__/MismatchingSnapshot.test.js:5:50) |
| 104 | + " |
| 105 | + `); |
| 106 | + |
| 107 | + const updateSnapshotRun = runJest(DIR, ['--updateSnapshot']); |
| 108 | + |
| 109 | + expect(updateSnapshotRun.exitCode).toBe(0); |
| 110 | + expect(updateSnapshotRun.stderr).toContain('1 snapshot updated.'); |
| 111 | +}); |
0 commit comments