jest-transform-mdx.js 679 B

12345678910111213141516171819202122
  1. const path = require('path');
  2. const { ScriptTransformer } = require('@jest/transform');
  3. const { dedent } = require('ts-dedent');
  4. const { compile } = require('@storybook/mdx2-csf');
  5. module.exports = {
  6. async processAsync(src, filename, config, { instrument }) {
  7. const code = await compile(src, { skipCsf: false });
  8. const result = dedent`
  9. /* @jsx mdx */
  10. import React from 'react'
  11. import { mdx } from '@mdx-js/react'
  12. ${code}
  13. `;
  14. const extension = path.extname(filename);
  15. const jsFileName = `${filename.slice(0, -extension.length)}.js`;
  16. return new ScriptTransformer(config).transformSource(jsFileName, result, instrument);
  17. },
  18. };