Skip to content

Commit

Permalink
feat(jest-snapshot): add support to cts and mts typescript files to i…
Browse files Browse the repository at this point in the history
…nline snapshots (#13975)
  • Loading branch information
mshima committed Mar 2, 2023
1 parent 0e8ed24 commit 4156f86
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,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

0 comments on commit 4156f86

Please sign in to comment.