storybook: Storyshot fail with Typescript
I am trying to use storyshot with Typescript but I get an error that I don’t manage to solve:
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import * as React from 'react';
SyntaxError: Unexpected token *
I also tried to follow https://medium.com/@mtiller/storybook-react-typescript-and-jest-c9059ea06fa7 without success.
To Reproduce
yarn add --dev @storybook/addon-storyshots
yarn add --dev @types/storybook__addon-storyshots
In my source folder I add a storybook.test.ts
import initStoryshots from '@storybook/addon-storyshots';
initStoryshots();
The wepack config is
module.exports = (baseConfig, env, config) => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
use: [
{
loader: require.resolve('ts-loader'),
},
],
});
config.resolve.extensions.push('.ts', '.tsx');
config.module.rules.push({
test: /\.stories\.tsx?$/,
loaders: [
{
loader: require.resolve('@storybook/addon-storysource/loader'),
options: { parser: 'typescript' },
},
],
enforce: 'pre',
});
return config;
};
and the jest config is
"jest": {
"setupFiles": [
"<rootDir>/setupTests.ts"
],
"transform": {
"\\.(ts|tsx)$": "ts-jest",
"\\.css$": "<rootDir>/node_modules/razzle/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|css|json)$)": "<rootDir>/node_modules/razzle/config/jest/fileTransform.js"
},
"testMatch": [
"!**/flycheck_*",
"<rootDir>/src/**/__tests__/**/*.(ts|js)?(x)",
"<rootDir>/src/**/?(*.)(spec|test).(ts|js)?(x)"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}"
]
},
Screenshots

About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 18 (1 by maintainers)
@apiel @DeX3 I was able to fix similar errors by renaming addons.js and config.js to addons.ts and config.ts
I successfully use Barrelsby to generate a barrel that exports all *.stories.tsx files, then just require that single file.
package.json:
.storybook/config.tsx:
To fix issues I went away from
ts-jestand now use babel version.https://jestjs.io/docs/en/getting-started
But there are still some tradeoffs - https://kulshekhar.github.io/ts-jest/user/babel7-or-ts
And it seems that
ts-jestcan’t be used with storyshot, because webpackrequirefix is about babel(macro or plugin way).So, my configs:
jest.config.js:
.babelrc:
.storybook/config.js:
(
require-context.macropackage is used)webpack.config.js: