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

feat: make prettier optional for inline snapshots #7792

Merged
merged 110 commits into from Nov 15, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
6ba49b8
vscode auto-formatting. will revert eventually
mmkal Feb 3, 2019
ca8cc43
use string replacement rather than prettier to insert snapshots
mmkal Feb 3, 2019
15701bf
make formatting go back to how it was?
mmkal Feb 3, 2019
2658e2c
add end of file newline
mmkal Feb 3, 2019
9aeac1a
fallback to @babel/parser when prettier isn't present
mmkal Feb 3, 2019
e9a2b07
use spyOn instead of $FlowFixMe mock
mmkal Feb 3, 2019
9594ea2
update changelog
mmkal Feb 3, 2019
3bc79e7
remove trailing whitespace
mmkal Feb 3, 2019
1555d65
make sure snapshot creation works with property matchers and without …
mmkal Feb 3, 2019
cc921cc
lint
mmkal Feb 4, 2019
0c2547e
use prettier --write
mmkal Feb 4, 2019
69016b1
add @babel/parser as snapshot dependency
mmkal Feb 4, 2019
716a620
prettier didn't take last time for some reason
mmkal Feb 4, 2019
dbb3bcf
shorten comment
mmkal Feb 4, 2019
93bb88b
use the same plugins as prettier
mmkal Feb 5, 2019
59c0cb1
Merge branch 'uglier-inline-snapshots' of https://github.com/mmkal/je…
mmkal Feb 5, 2019
ac57288
prettier fixes
mmkal Feb 5, 2019
bee86ee
Merge branch 'master' into uglier-inline-snapshots
mmkal Feb 7, 2019
188d731
use loadPartialConfig from @babel/core rather than hard-coded list
mmkal Feb 12, 2019
6ecb0a4
add @babel/core dependency
mmkal Feb 12, 2019
2ed041e
Merge branch 'master' into uglier-inline-snapshots
mmkal Mar 4, 2019
2f3f2e1
fix merge errors
mmkal Mar 4, 2019
8c820d6
case test fake frames to Frame type
mmkal Mar 4, 2019
5ef7328
make vscode stop messing up formatting
mmkal Mar 4, 2019
741ca7f
use babel generate instead of hacky comma hacks
mmkal Mar 4, 2019
4f5ea4b
use imports and flatten test.each
mmkal Mar 4, 2019
4a4d86c
prettier fix #123532
mmkal Mar 4, 2019
a4d30a7
move changelog entry
mmkal Mar 4, 2019
f77b104
lint fixes
mmkal Mar 4, 2019
e94e95d
Merge branch 'master' into uglier-inline-snapshots
mmkal Mar 4, 2019
f5dd3d3
Merge branch 'master' into uglier-inline-snapshots
mmkal Mar 11, 2019
a102014
lint
mmkal Mar 11, 2019
9215a44
prettier changelog
mmkal Mar 11, 2019
f9bbf05
Merge branch 'master' into uglier-inline-snapshots
mmkal Mar 12, 2019
c504dee
Merge branch 'master' into uglier-inline-snapshots
mmkal Mar 21, 2019
97e609e
Merge branch 'master' into uglier-inline-snapshots
mmkal Apr 11, 2019
3e92e6c
fix conflicts
mmkal Apr 11, 2019
5f26766
add todo
mmkal Apr 11, 2019
eea3199
placeholder prettier formatting block
mmkal Apr 11, 2019
442f1cd
reign in @babel/generate's creative license a bit
mmkal Apr 11, 2019
121ddaf
run prettier twice ¯\_(ツ)_/¯
mmkal Apr 11, 2019
9a46420
trim babel's creativity again
mmkal Apr 11, 2019
ae376f6
clean up
mmkal Apr 11, 2019
38c3c32
prettier
mmkal Apr 11, 2019
b92f988
use Function to avoid fixing types all over the place
mmkal Apr 11, 2019
a8ce8c1
oops
mmkal Apr 11, 2019
2d7d3b0
Merge branch 'master' into uglier-inline-snapshots
mmkal May 15, 2019
173835e
Merge branch 'master' into uglier-inline-snapshots
mmkal Jun 23, 2019
6fae9d1
prettier
mmkal Jun 23, 2019
87d79d2
fix: remove callee property name check
mmkal Jun 23, 2019
c787b8d
prettier
mmkal Jun 23, 2019
81d63aa
alphabetize imports
mmkal Jun 25, 2019
5205259
move changelog entry to master
mmkal Jun 25, 2019
d97f6cf
check plugin is missing before adding
mmkal Jun 29, 2019
127d492
prettier
mmkal Jun 29, 2019
2ef0b1b
prettier
mmkal Jun 29, 2019
e0d50a5
prettier!
mmkal Jun 29, 2019
4c39c17
Merge remote-tracking branch 'upstream/master' into uglier-inline-sna…
mmkal Oct 19, 2019
11b2309
run yarn install to update lockfile
mmkal Oct 19, 2019
49ae9ea
prettier
mmkal Oct 19, 2019
fa29eab
null check
mmkal Oct 19, 2019
8262b0d
Merge branch 'master' into uglier-inline-snapshots
SimenB Nov 9, 2019
b4d7dd1
fix: lint issues
mmkal Nov 9, 2019
22c1e4e
make @babel/* packages peerDependencies
mmkal Nov 9, 2019
12393d7
oh dear more lint
mmkal Nov 9, 2019
6d4815f
ah needed to update prettier locally
mmkal Nov 9, 2019
b2dfa56
I forgot lockfiles are kinda trustworthy now
mmkal Nov 9, 2019
2bc8090
move generator, parser and traverse back to dependencies
mmkal Nov 9, 2019
1b10dc8
Merge remote-tracking branch 'upstream/master' into uglier-inline-sna…
jeysal Apr 10, 2020
5fe9929
require.resolve Babel syntax plugin
jeysal Apr 10, 2020
449999d
correct changelog position
jeysal Apr 10, 2020
5ae7f0f
extract runPrettier fn
jeysal Apr 10, 2020
8ebb456
Babel parse correctly
jeysal Apr 10, 2020
0d2ea99
dedupe babel deps
SimenB Apr 11, 2020
355bde6
Update packages/jest-snapshot/src/inline_snapshots.ts
jeysal Apr 12, 2020
26acdd0
plugin-syntax-typescript dep
jeysal Apr 12, 2020
6f48559
preset current node syntax and dep cleanup
jeysal Apr 12, 2020
784b3d7
babel core peer dep
jeysal Apr 12, 2020
81f12a4
lint
jeysal Apr 12, 2020
dae5678
Merge branch 'master' into uglier-inline-snapshots
SimenB Apr 12, 2020
27cf6e0
Merge remote-tracking branch 'upstream/master' into uglier-inline-sna…
jeysal Apr 26, 2020
3f89b25
basic jest-runtime require.resolve test
jeysal Apr 27, 2020
eb5b9d3
implement require.resolve option outsideJestVm for internal modules only
jeysal Apr 27, 2020
e141ace
review improvements
jeysal Apr 28, 2020
faed042
lint
jeysal Apr 28, 2020
49bed60
Merge branch 'runtime-require-resolve-outside' into uglier-inline-sna…
jeysal Apr 28, 2020
fc086cc
WIP require prettier/Babel from outsideJestVm
jeysal Apr 28, 2020
a2a05bd
use Symbol key for resolve option
jeysal Apr 28, 2020
a6d6d36
Merge branch 'runtime-require-resolve-outside' into uglier-inline-sna…
jeysal Apr 28, 2020
7efccac
Update CHANGELOG.md
jeysal Apr 28, 2020
13c960e
Merge branch 'master' into uglier-inline-snapshots
jeysal Nov 8, 2020
06a7fb0
changelog merge
jeysal Nov 8, 2020
478d8f6
merge fixup
jeysal Nov 8, 2020
b3e2f73
yarn.lock merge
jeysal Nov 8, 2020
f375ece
merge amends
jeysal Nov 8, 2020
a641fdd
merge amends
jeysal Nov 8, 2020
81bc81e
semantic merge for test
jeysal Nov 10, 2020
be68e58
Merge branch 'master' into uglier-inline-snapshots
jeysal Nov 10, 2020
36efffa
lint
jeysal Nov 11, 2020
c53a579
Merge branch 'master' into uglier-inline-snapshots
jeysal Nov 14, 2020
f261822
use babel stuff
jeysal Nov 14, 2020
9651814
Merge branch 'master' into uglier-inline-snapshots
jeysal Nov 14, 2020
857d5eb
ts-expect-error now needed because we no longer define the requireOut…
jeysal Nov 14, 2020
7f97e87
prettier 2 devdep
jeysal Nov 14, 2020
d72b98f
types
jeysal Nov 14, 2020
8364ca1
babel-preset-current-node-syntax v1
jeysal Nov 14, 2020
5536359
jest-circus config is passed already
jeysal Nov 14, 2020
c4156b0
update snapshot formatting
jeysal Nov 14, 2020
b6327e4
use upstream types
SimenB Nov 15, 2020
69f8f0d
remove obsolete circus deps
jeysal Nov 15, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

### Features

- `[jest-snapshots]`: Make prettier optional for inline snapshots - fall back to string replacement ([#7792](https://github.com/facebook/jest/pull/7792))
mmkal marked this conversation as resolved.
Show resolved Hide resolved
- `[jest-resolve]`: Pass default resolver into custom resolvers ([#7714](https://github.com/facebook/jest/pull/7714))
- `[jest-cli]`: `global{Setup,Teardown}` use default export with es modules ([#7750](https://github.com/facebook/jest/pull/7750))
- `[jest-runtime]` Better error messages when the jest environment is used after teardown by async code ([#7756](https://github.com/facebook/jest/pull/7756))
Expand Down
1 change: 1 addition & 0 deletions packages/jest-snapshot/package.json
Expand Up @@ -9,6 +9,7 @@
"license": "MIT",
"main": "build/index.js",
"dependencies": {
"@babel/parser": "^7.2.3",
"@babel/types": "^7.0.0",
"chalk": "^2.0.1",
"jest-diff": "^24.0.0",
Expand Down
175 changes: 147 additions & 28 deletions packages/jest-snapshot/src/__tests__/inline_snapshots.test.js
Expand Up @@ -53,10 +53,9 @@ afterEach(() => {

test('saveInlineSnapshots() replaces empty function call with a template literal', () => {
const filename = path.join(__dirname, 'my.test.js');
// $FlowFixMe mock
fs.readFileSync = (jest.fn(
() => `expect(1).toMatchInlineSnapshot();\n`,
): any);
jest
.spyOn(fs, 'readFileSync')
.mockImplementation(() => `expect(1).toMatchInlineSnapshot();\n`);

saveInlineSnapshots(
[
Expand All @@ -75,14 +74,113 @@ test('saveInlineSnapshots() replaces empty function call with a template literal
);
});

test('saveInlineSnapshots() without prettier leaves formatting outside of snapshots alone', () => {
const filename = path.join(__dirname, 'my.test.js');
jest.spyOn(fs, 'readFileSync').mockImplementation(
() =>
`
const a = [1, 2];
expect(a).toMatchInlineSnapshot(\`an out-of-date and also multi-line
snapshot\`);
expect(a).toMatchInlineSnapshot();
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
`.trim() + '\n',
);

saveInlineSnapshots(
[2, 4, 5].map(line => ({
frame: {column: 11, file: filename, line},
snapshot: `[1, 2]`,
})),
null,
babelTraverse,
);

expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
`const a = [1, 2];
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
`,
);
});

test('saveInlineSnapshots() can handle typescript without prettier', () => {
const filename = path.join(__dirname, 'my.test.ts');
jest.spyOn(fs, 'readFileSync').mockImplementation(
() =>
`
interface Foo {
foo: string
}
const a: [Foo, Foo] = [{ foo: 'one' }, { foo: 'two' }];
expect(a).toMatchInlineSnapshot();
`.trim() + '\n',
);

saveInlineSnapshots(
[
{
frame: {column: 11, file: filename, line: 5},
snapshot: `[{ foo: 'one' }, { foo: 'two' }]`,
},
],
null,
babelTraverse,
);

expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
`
interface Foo {
foo: string
}
const a: [Foo, Foo] = [{ foo: 'one' }, { foo: 'two' }];
expect(a).toMatchInlineSnapshot(\`[{ foo: 'one' }, { foo: 'two' }]\`);
`.trim() + '\n',
);
});

test('saveInlineSnapshots() can use prettier to fix formatting for whole file', () => {
const filename = path.join(__dirname, 'my.test.js');
jest.spyOn(fs, 'readFileSync').mockImplementation(
() =>
`
const a = [1, 2];
expect(a).toMatchInlineSnapshot(\`an out-of-date and also multi-line
snapshot\`);
expect(a).toMatchInlineSnapshot();
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
`.trim() + '\n',
);

saveInlineSnapshots(
[2, 4, 5].map(line => ({
frame: {column: 11, file: filename, line},
snapshot: `[1, 2]`,
})),
prettier,
babelTraverse,
);

expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
`const a = [1, 2];
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
`,
);
});

test.each([['babylon'], ['flow'], ['typescript']])(
'saveInlineSnapshots() replaces existing template literal - %s parser',
parser => {
const filename = path.join(__dirname, 'my.test.js');
// $FlowFixMe mock
fs.readFileSync = (jest.fn(
() => 'expect(1).toMatchInlineSnapshot(`2`);\n',
): any);
jest
.spyOn(fs, 'readFileSync')
.mockImplementation(() => 'expect(1).toMatchInlineSnapshot(`2`);\n');

prettier.resolveConfig.sync.mockReturnValue({parser});

Expand All @@ -108,10 +206,9 @@ test.each([['babylon'], ['flow'], ['typescript']])(

test('saveInlineSnapshots() replaces existing template literal with property matchers', () => {
const filename = path.join(__dirname, 'my.test.js');
// $FlowFixMe mock
fs.readFileSync = (jest.fn(
() => 'expect(1).toMatchInlineSnapshot({}, `2`);\n',
): any);
jest
.spyOn(fs, 'readFileSync')
.mockImplementation(() => 'expect(1).toMatchInlineSnapshot({}, `2`);\n');

saveInlineSnapshots(
[
Expand All @@ -130,12 +227,37 @@ test('saveInlineSnapshots() replaces existing template literal with property mat
);
});

test.each([[prettier], [null]])(
'saveInlineSnapshots() creates template literal with property matchers',
prettierModule => {
const filename = path.join(__dirname, 'my.test.js');
jest
.spyOn(fs, 'readFileSync')
.mockImplementation(() => 'expect(1).toMatchInlineSnapshot({});\n');

saveInlineSnapshots(
[
{
frame: {column: 11, file: filename, line: 1},
snapshot: `1`,
},
],
prettierModule,
babelTraverse,
);

expect(fs.writeFileSync).toHaveBeenCalledWith(
filename,
'expect(1).toMatchInlineSnapshot({}, `1`);\n',
);
},
);

test('saveInlineSnapshots() throws if frame does not match', () => {
const filename = path.join(__dirname, 'my.test.js');
// $FlowFixMe mock
fs.readFileSync = (jest.fn(
() => 'expect(1).toMatchInlineSnapshot();\n',
): any);
jest
.spyOn(fs, 'readFileSync')
.mockImplementation(() => 'expect(1).toMatchInlineSnapshot();\n');

const save = () =>
saveInlineSnapshots(
Expand All @@ -154,10 +276,9 @@ test('saveInlineSnapshots() throws if frame does not match', () => {

test('saveInlineSnapshots() throws if multiple calls to to the same location', () => {
const filename = path.join(__dirname, 'my.test.js');
// $FlowFixMe mock
fs.readFileSync = (jest.fn(
() => 'expect(1).toMatchInlineSnapshot();\n',
): any);
jest
.spyOn(fs, 'readFileSync')
.mockImplementation(() => 'expect(1).toMatchInlineSnapshot();\n');

const frame = {column: 11, file: filename, line: 1};
const save = () =>
Expand All @@ -174,10 +295,9 @@ test('saveInlineSnapshots() throws if multiple calls to to the same location', (

test('saveInlineSnapshots() uses escaped backticks', () => {
const filename = path.join(__dirname, 'my.test.js');
// $FlowFixMe mock
fs.readFileSync = (jest.fn(
() => 'expect("`").toMatchInlineSnapshot();\n',
): any);
jest
.spyOn(fs, 'readFileSync')
.mockImplementation(() => 'expect("`").toMatchInlineSnapshot();\n');

const frame = {column: 13, file: filename, line: 1};
saveInlineSnapshots([{frame, snapshot: '`'}], prettier, babelTraverse);
Expand All @@ -190,10 +310,9 @@ test('saveInlineSnapshots() uses escaped backticks', () => {

test('saveInlineSnapshots() works with non-literals in expect call', () => {
const filename = path.join(__dirname, 'my.test.js');
// $FlowFixMe mock
fs.readFileSync = (jest.fn(
() => `expect({a: 'a'}).toMatchInlineSnapshot();\n`,
): any);
jest
.spyOn(fs, 'readFileSync')
.mockImplementation(() => `expect({a: 'a'}).toMatchInlineSnapshot();\n`);
prettier.resolveConfig.sync.mockReturnValue({
bracketSpacing: false,
singleQuote: true,
Expand Down