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(jest-snapshot): add support to cts and mts typescript files to inline snapshots #13975

Merged
merged 5 commits into from Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -10,6 +10,7 @@
- `[jest-message-util]` Add support for [AggregateError](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError) ([#13946](https://github.com/facebook/jest/pull/13946) & [#13947](https://github.com/facebook/jest/pull/13947))
- `[jest-message-util]` Add support for [Error causes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause) in `test` and `it` ([#13935](https://github.com/facebook/jest/pull/13935) & [#13966](https://github.com/facebook/jest/pull/13966))
- `[jest-reporters]` Add `summaryThreshold` option to summary reporter to allow overriding the internal threshold that is used to print the summary of all failed tests when the number of test suites surpasses it ([#13895](https://github.com/facebook/jest/pull/13895))
- `[jest-snapshot]` Add support to `cts` and `mts` TypeScript files to inline snapshots ([#13975](https://github.com/facebook/jest/pull/13975))
- `[jest-worker]` Add `start` method to worker farms ([#13937](https://github.com/facebook/jest/pull/13937))

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-snapshot/src/InlineSnapshots.ts
Expand Up @@ -81,7 +81,7 @@ const saveSnapshotsForFile = (
// TypeScript projects may not have a babel config; make sure they can be parsed anyway.
const presets = [require.resolve('babel-preset-current-node-syntax')];
const plugins: Array<PluginItem> = [];
if (/\.tsx?$/.test(sourceFilePath)) {
if (/\.([cm]?ts|tsx)$/.test(sourceFilePath)) {
plugins.push([
require.resolve('@babel/plugin-syntax-typescript'),
{isTSX: sourceFilePath.endsWith('x')},
Expand Down
43 changes: 23 additions & 20 deletions packages/jest-snapshot/src/__tests__/InlineSnapshots.test.ts
Expand Up @@ -132,40 +132,43 @@ expect(a).toMatchInlineSnapshot(\`[1, 2]\`);
);
});

test('saveInlineSnapshots() can handle typescript without prettier', () => {
const filename = path.join(dir, 'my.test.ts');
fs.writeFileSync(
filename,
`${`
test.each([['ts'], ['cts'], ['mts']])(
'saveInlineSnapshots() can handle typescript without prettier - %s extension',
extension => {
const filename = path.join(dir, `my.test.${extension}`);
fs.writeFileSync(
filename,
`${`
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} as Frame,
snapshot: "[{ foo: 'one' }, { foo: 'two' }]",
},
],
dir,
null,
);
saveInlineSnapshots(
[
{
frame: {column: 11, file: filename, line: 5} as Frame,
snapshot: "[{ foo: 'one' }, { foo: 'two' }]",
},
],
dir,
null,
);

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

test('saveInlineSnapshots() can handle tsx without prettier', () => {
const filename = path.join(dir, 'my.test.tsx');
Expand Down