-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
jest-diff CLI #7781
Closed
Closed
jest-diff CLI #7781
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`different 1`] = ` | ||
- Expected | ||
+ Received | ||
|
||
Array [ | ||
- 42, | ||
+ 1337, | ||
Object { | ||
- "a": true, | ||
+ "b": false, | ||
}, | ||
] | ||
|
||
`; | ||
|
||
exports[`help 1`] = ` | ||
jest-diff.js a.json b.json | ||
|
||
Options: | ||
-v, --version Show version number [boolean] | ||
-h, --help Show help [boolean] | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import execa from 'execa'; | ||
import {tmpdir} from 'os'; | ||
import {resolve} from 'path'; | ||
import {wrap} from 'jest-snapshot-serializer-raw'; | ||
import stripAnsi from 'strip-ansi'; | ||
|
||
import {cleanup, writeFiles} from '../Utils'; | ||
|
||
const DIR = resolve(tmpdir(), 'jest-diff-cli'); | ||
const JEST_DIFF = resolve( | ||
__dirname, | ||
'../../packages/jest-diff/bin/jest-diff.js', | ||
); | ||
|
||
beforeAll(() => | ||
writeFiles(DIR, { | ||
'a.json': '[42, {"a": true}]', | ||
'b.json': '[1337, {"b": false}]', | ||
'not.json': 'not JSON', | ||
}), | ||
); | ||
afterAll(() => cleanup(DIR)); | ||
|
||
test('equal', async () => { | ||
const {code, stdout} = await execa(JEST_DIFF, ['a.json', 'a.json'], { | ||
cwd: DIR, | ||
}); | ||
expect(code).toBe(0); | ||
expect(stdout).toBe(''); | ||
}); | ||
|
||
test('different', async () => { | ||
const {code, stdout} = await execa(JEST_DIFF, ['a.json', 'b.json'], { | ||
cwd: DIR, | ||
reject: false, | ||
}); | ||
expect(code).toBe(1); | ||
expect(wrap(stripAnsi(stdout))).toMatchSnapshot(); | ||
}); | ||
|
||
// --- IO-related --- | ||
|
||
test('file does not exist', async () => { | ||
const {code, stderr} = await execa(JEST_DIFF, ['a.json', 'nope.jpg'], { | ||
cwd: DIR, | ||
reject: false, | ||
}); | ||
expect(code).toBe(20); | ||
expect(stderr).toMatch(/no such file.*nope\.jpg/); | ||
}); | ||
|
||
test('not JSON', async () => { | ||
const {code, stderr} = await execa(JEST_DIFF, ['a.json', 'not.json'], { | ||
cwd: DIR, | ||
reject: false, | ||
}); | ||
expect(code).toBe(30); | ||
expect(stderr).toMatch(/Failed to parse.*JSON/); | ||
}); | ||
|
||
// --- CLI-related --- | ||
|
||
describe('invalid usage', () => { | ||
test('too few arguments', async () => { | ||
const {code, stderr} = await execa(JEST_DIFF, ['a'], {reject: false}); | ||
expect(code).toBe(2); | ||
expect(stderr).toEqual( | ||
expect.stringContaining('Not enough non-option arguments'), | ||
); | ||
}); | ||
|
||
test('too many arguments', async () => { | ||
const {code, stderr} = await execa(JEST_DIFF, ['a', 'b', 'c'], { | ||
reject: false, | ||
}); | ||
expect(code).toBe(2); | ||
expect(stderr).toEqual( | ||
expect.stringContaining('Too many non-option arguments'), | ||
); | ||
}); | ||
}); | ||
|
||
test('version', async () => { | ||
const {code, stdout} = await execa(JEST_DIFF, ['-v']); | ||
expect(code).toBe(0); | ||
expect(stdout).toMatch(/\d{2}\.\d{1,2}\.\d{1,2}[\-\S]*/); | ||
}); | ||
|
||
test('help', async () => { | ||
const {code, stdout} = await execa(JEST_DIFF, ['-h']); | ||
expect(code).toBe(0); | ||
expect(wrap(stdout)).toMatchSnapshot(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
'use strict'; | ||
|
||
require('../build/cli').default(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import yargs from 'yargs'; | ||
import {readFileSync} from 'fs'; | ||
|
||
// eslint-disable-next-line import/default | ||
import jestDiff from '../'; | ||
import {NO_DIFF_MESSAGE} from '../constants'; | ||
|
||
/* eslint-disable sort-keys */ | ||
const EXIT_CODES = { | ||
// like GNU diff | ||
EQUAL: 0, | ||
DIFFERENT: 1, | ||
INVALID_USAGE: 2, | ||
|
||
// more specific than GNU diff, which exits with 2 in all error cases | ||
INTERNAL_ERROR: 10, | ||
IO_ERROR: 20, | ||
INVALID_JSON: 30, | ||
}; | ||
/* eslint-enable sort-keys */ | ||
|
||
/* eslint-disable-next-line consistent-return */ | ||
const read = (path: string) => { | ||
try { | ||
const data = readFileSync(path); | ||
try { | ||
return JSON.parse(String(data)); | ||
} catch (parseErr) { | ||
console.error(`Failed to parse file ${path} as JSON`, parseErr); | ||
process.exit(EXIT_CODES.INVALID_JSON); | ||
} | ||
} catch (readErr) { | ||
console.error(`Failed to read file ${path}`, readErr); | ||
process.exit(EXIT_CODES.IO_ERROR); | ||
} | ||
}; | ||
|
||
export default async () => { | ||
const parser = yargs(process.argv.slice(2)) | ||
.demandCommand(2, 2) // positional | ||
|
||
.version() | ||
.alias('v', 'version') | ||
|
||
.usage('$0 a.json b.json') | ||
.help() | ||
.alias('h', 'help') | ||
|
||
.fail((msg, err, yargs) => { | ||
if (err) throw err; | ||
console.error(yargs.help()); | ||
console.error(msg); | ||
process.exit(EXIT_CODES.INVALID_USAGE); | ||
}); | ||
|
||
const [aPath, bPath] = parser.argv._; | ||
const a = read(aPath); | ||
const b = read(bPath); | ||
|
||
const diffMsg = jestDiff(a, b); | ||
if (diffMsg == null) { | ||
console.error('diff unexpectedly returned null'); | ||
process.exit(EXIT_CODES.INTERNAL_ERROR); | ||
} else if (diffMsg === NO_DIFF_MESSAGE) { | ||
process.exit(EXIT_CODES.EQUAL); | ||
} else { | ||
console.log(diffMsg); | ||
process.exit(EXIT_CODES.DIFFERENT); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is IMO an example of where a separate CLI would force a better API.
It's imported to allow the CLI to remove a message that shouldn't really be in a diffing algorithm. Instead it should probably be an option (or structured return values, or something else entirely), but since we have access to private stuff, it's more natural to just import it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it really bugged me writing this that the return values of jest-diff are so weird 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, those constants :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we fix this without a breaking change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, adding an option which changes the returned thing (from a string to something structured) should be fine in a minor (as long as it's opt-in). Might be a bit painful WRT flow types but probably fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fool around with some non-breaking API designs.
Probably on the weekend. So much work, so little time for my Jest todo list :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, mee too :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding the
null
return values for asymmetric matchers: Would it be fine to just treat them like "no difference"? The reason they were originally introduced was #8146 - in that case, you would get the "No visual difference" message as well. Sooner or later we'll have to rethinkjest-diff
(andexpect
andpretty-format
) in light of #6184 , but for now it would be good to have just "differs, here's the string" and "does not differ (visually)" as returns values so that we can come up with a clean and simple APIThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Actually, what's now
SIMILAR_MESSAGE
will need to have a special case in the return value too :/ )There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should return something like
{differs: true, message: string, toJsonIgnored: boolean} | {differs: false}