├── .github ├── FUNDING.yml └── workflows │ └── master.yml ├── .gitignore ├── .npmignore ├── .prettierignore ├── .prettierrc.json ├── CHANGELOG.md ├── README.md ├── babel.config.js ├── cra.js ├── index.js ├── jest.config.cra.js ├── jest.config.js ├── jest.config.mdx-options-file.js ├── jest.config.mdx-options.js ├── jest.config.user-settings.js ├── mdx-options.cjs ├── package.json ├── test.js ├── test.md ├── test.mdx-options.js ├── test.user-settings.js ├── test └── fixtures │ └── mdx-options.js └── yarn.lock /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | ko_fi: bitttttten 4 | -------------------------------------------------------------------------------- /.github/workflows/master.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | publish: 10 | name: Test 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v1 14 | - uses: actions/setup-node@v1 15 | with: 16 | node-version: 14.x 17 | - name: 📥 Install deps 18 | run: yarn 19 | - name: envinfo 20 | run: npx envinfo --preset jest 21 | - name: 🧪 Test 22 | run: yarn test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vscode/ 3 | node_modules/ 4 | build 5 | lib 6 | .DS_Store 7 | *.tgz 8 | lerna-debug.log 9 | npm-debug.log* 10 | yarn-debug.log* 11 | false/ 12 | yarn-error.log* 13 | /.changelog 14 | .npm/ 15 | 16 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | babel.config.js 2 | test.* 3 | jest.* 4 | .prettier* 5 | .github/ 6 | test/ 7 | false/ -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | build 2 | coverage 3 | node_modules 4 | storybook-static 5 | public -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "endOfLine": "lf", 3 | "semi": false, 4 | "singleQuote": false, 5 | "tabWidth": 4, 6 | "useTabs": true, 7 | "trailingComma": "es5" 8 | } 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ## [3.3.0](https://github.com/bitttttten/jest-transformer-md/compare/v3.2.0...v3.3.0) (2021-05-26) 6 | 7 | ## [3.2.0](https://github.com/bitttttten/jest-transformer-md/compare/v3.1.0...v3.2.0) (2021-05-26) 8 | 9 | 10 | ### Features 11 | 12 | * support all jest config formats ([d795a9f](https://github.com/bitttttten/jest-transformer-md/commit/d795a9f3a9eaa31136009a09274af1e4ffbfa812)) 13 | * support babel-jest with versions below 27 too ([832de5d](https://github.com/bitttttten/jest-transformer-md/commit/832de5d2d7141819773324ba6d0f92691cbf3f37)) 14 | 15 | ## [3.1.0](https://github.com/bitttttten/jest-transformer-md/compare/v3.0.0...v3.1.0) (2021-05-26) 16 | 17 | - use async transformer and await on mdx result ([5c867ef](https://github.com/bitttttten/jest-transformer-md/commit/5c867ef)) 18 | 19 | ## 3.0.0 (2021-05-26) 20 | 21 | ### Features 22 | 23 | - add in mdx config file support ([79b5538](https://github.com/bitttttten/jest-transformer-md/commit/79b55387315550eed58745f3c9c1c80bf3d74414)) 24 | - add in support for Jest 27, and drop support for Jest 26 (for < Jest 26 support please install version 2 of jest-transformer-mdx) ([c71e4f6](https://github.com/bitttttten/jest-transformer-mdx/pull/23/commits/c71e4f60f9b13ba721d17ab83d4fa549d17535db)) 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jest-transformer-mdx 2 | 3 | [![Github release version](https://img.shields.io/github/tag/bitttttten/jest-transformer-mdx.svg)](https://github.com/bitttttten/jest-transformer-mdx/releases) 4 | [![Commits since release](https://img.shields.io/github/commits-since/bitttttten/jest-transformer-mdx/v2.2.0.svg)](https://github.com/bitttttten/jest-transformer-mdx/compare/v2.2.0...master) 5 | [![npm release version](https://img.shields.io/npm/v/jest-transformer-mdx.svg)](https://www.npmjs.com/package/jest-transformer-mdx) 6 | 7 | ## Introduction 8 | 9 | Jest transformer for [MDX](https://mdxjs.com/) with [frontMatter](https://github.com/c8r/x0/blob/master/lib/mdx-fm-loader.js) support. 10 | 11 | ## Instructions 12 | 13 | ### Install 14 | 15 | `yarn add jest-transformer-mdx` 16 | 17 | ### Add to your jest config 18 | 19 | ```js 20 | // jest.config.js 21 | module.exports = { 22 | // A map from regular expressions to paths to transformers 23 | transform: { 24 | "^.+\\.(md|mdx)$": "jest-transformer-mdx", 25 | }, 26 | } 27 | ``` 28 | 29 | And that should be it! `jest-transformer-mdx` will pick up your babel config and use your jest config. 30 | 31 | ### jest 26 or below 32 | 33 | To support jest 26 or below, please install at version 2. Version 3 only supports jest 27 and above. 34 | 35 | `yarn add jest-transformer-mdx@2` 36 | 37 | ### Example 38 | 39 | Look inside [this library's test](https://github.com/bitttttten/jest-transformer-mdx/blob/master/test.js) and the related [markdown file](https://github.com/bitttttten/jest-transformer-mdx/blob/master/test.md) to see live a example. 40 | 41 | ### Configuration 42 | 43 | You can configure this transformer by using a different syntax in your jest config, an array of the path to the transformer followed by an options object. 44 | 45 | #### Options 46 | 47 | #### frontMatterName 48 | 49 | ```js 50 | // jest.config.js 51 | module.exports = { 52 | transform: { 53 | "^.+\\.(md|mdx)$": [ 54 | "jest-transformer-mdx", 55 | { 56 | frontMatterName: "meta", 57 | mdxOptions: { 58 | rehypePlugins: [require("rehype-slug")], 59 | }, 60 | }, 61 | ], 62 | }, 63 | } 64 | ``` 65 | 66 | Use this option to rename the exported frontMatter object. This module exports the frontMatter object named as "frontMatter", so [in your component and tests](https://github.com/bitttttten/jest-transformer-mdx/blob/d23701d641f826fface8511e70734073ca2ad29b/test.js#L2) you could only access the frontMatter object through `require('./hello-world.mdx').frontMatter`. If this does not suite your workflow, then use this option to rename it. 67 | 68 | #### mdxOptions 69 | 70 | ```js 71 | // jest.config.js 72 | module.exports = { 73 | transform: { 74 | "^.+\\.(md|mdx)$": [ 75 | "jest-transformer-mdx/mdx-options", 76 | { 77 | frontMatterName: "meta", 78 | mdxOptions: { 79 | rehypePlugins: [require("rehype-slug")], 80 | }, 81 | }, 82 | ], 83 | }, 84 | } 85 | ``` 86 | 87 | Note: edit your transform property to import from `jest-transformer-mdx/mdx-options`. 88 | 89 | Use this option to configure mdx. Perhaps you have added some custom plugins, and need that reflected in this transformer. Note that you can either pass in an inline object like the above example, or you can pass in a path to a file that exports your mdx options like the below example, which is useful if your mdx options is not JSON-serializable: 90 | 91 | ```js 92 | // jest.config.js 93 | module.exports = { 94 | transform: { 95 | "^.+\\.(md|mdx)$": [ 96 | "jest-transformer-mdx", 97 | { 98 | mdxOptions: "config/mdx-options.js", 99 | }, 100 | ], 101 | }, 102 | } 103 | ``` 104 | 105 | ```js 106 | // config/mdx-options.js 107 | const options = { 108 | rehypePlugins: [rehypeSlug], 109 | } 110 | 111 | module.exports = options 112 | ``` 113 | 114 | #### Interface 115 | 116 | ```ts 117 | interface Options { 118 | // rename the object that the frontmatter object will get exported as 119 | frontMatterName?: string 120 | } 121 | ``` 122 | 123 | ### create-react-app & configless 124 | 125 | You can also use this module in `create-react-app`-like apps where the config is not exposed. Edit your transform property to import from `jest-transformer-mdx/cra`. This method does not support any of the configuration options mentioned above yet. 126 | 127 | ```js 128 | // jest.config.js 129 | module.exports = { 130 | transform: { 131 | "^.+\\.(md|mdx)$": "jest-transformer-mdx/cra", 132 | }, 133 | } 134 | ``` 135 | 136 | ## Credits 137 | 138 | Made possible by 139 | 140 | 141 | -------------------------------------------------------------------------------- /babel.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | presets: [ 3 | [ 4 | "@babel/preset-env", 5 | { 6 | targets: { 7 | node: "current" 8 | } 9 | } 10 | ], 11 | "babel-preset-react-app" 12 | ] 13 | }; 14 | -------------------------------------------------------------------------------- /cra.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const matter = require('gray-matter') 4 | const stringifyObject = require('stringify-object') 5 | const mdx = require('@mdx-js/mdx') 6 | const babel = require('@babel/core') 7 | 8 | function parseFrontMatter(src) { 9 | const { content, data } = matter(src) 10 | 11 | return `export const frontMatter = ${stringifyObject( 12 | data, 13 | )}; 14 | 15 | ${content}` 16 | } 17 | 18 | function createTransformer(src) { 19 | const withFrontMatter = parseFrontMatter(src) 20 | const jsx = mdx.sync(withFrontMatter) 21 | const toTransform = `import {mdx} from '@mdx-js/react';${jsx}` 22 | 23 | return babel.transform(toTransform, { 24 | presets: [require.resolve('babel-preset-react-app')], 25 | babelrc: false, 26 | configFile: false, 27 | filename: 'null', 28 | }).code 29 | } 30 | 31 | module.exports = { 32 | process: createTransformer, 33 | } 34 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | const path = require("path") 4 | const matter = require("gray-matter") 5 | const stringifyObject = require("stringify-object") 6 | const mdx = require("@mdx-js/mdx") 7 | const babelJest = require("babel-jest") 8 | 9 | // we support either a path to a file, or the options itself 10 | // see: https://github.com/bitttttten/jest-transformer-mdx/pull/20 11 | function resolveMdxOptions(src) { 12 | if (typeof src === "string") { 13 | return require(path.resolve(process.cwd(), src)) 14 | } 15 | return src 16 | } 17 | 18 | function parseFrontMatter(src, frontMatterName = "frontMatter") { 19 | const { content, data } = matter(src) 20 | 21 | return `export const ${frontMatterName} = ${stringifyObject(data)}; 22 | 23 | ${content}` 24 | } 25 | 26 | // this helper resolves both jest 27, and versions of jest below 27 27 | // as the way that transformer config is picked up has changed 28 | // see: https://github.com/bitttttten/jest-transformer-mdx/issues/22 29 | function resolveOptions(config) { 30 | if (config?.transformerConfig) { 31 | return config.transformerConfig 32 | } 33 | 34 | if (config?.transform && Array.isArray(config.transform)) { 35 | for (let i = 0; i < config.transform.length; i++) { 36 | if (new RegExp(config.transform[i][0]).test(filename)) { 37 | return config.transform[i][2] 38 | } 39 | } 40 | } 41 | 42 | return {} 43 | } 44 | 45 | module.exports = { 46 | process(src, filepath, config) { 47 | const options = resolveOptions(config) 48 | const mdxOptions = resolveMdxOptions(options?.mdxOptions) 49 | 50 | const withFrontMatter = parseFrontMatter(src, options?.frontMatterName) 51 | 52 | const jsx = mdx.sync(withFrontMatter, { ...mdxOptions, filepath }) 53 | 54 | const toTransform = `import {mdx} from '@mdx-js/react';${jsx}` 55 | 56 | // supports babel-jest@27 (which exports with .default) and older versions 57 | // see: https://github.com/bitttttten/jest-transformer-mdx/issues/22 58 | const babelProcess = babelJest.default?.process ?? babelJest.process 59 | return babelProcess(toTransform, filepath, config).code 60 | }, 61 | } 62 | -------------------------------------------------------------------------------- /jest.config.cra.js: -------------------------------------------------------------------------------- 1 | /* 2 | * For a detailed explanation regarding each configuration property, visit: 3 | * https://jestjs.io/docs/configuration 4 | */ 5 | const path = require("path") 6 | 7 | module.exports = { 8 | // All imported modules in your tests should be mocked automatically 9 | // automock: false, 10 | 11 | // Stop running tests after `n` failures 12 | // bail: 0, 13 | 14 | // The directory where Jest should store its cached dependency information 15 | // cacheDirectory: "/private/var/folders/zw/mkp1m62d5jv_p2g0yjnp78vw0000gn/T/jest_dx", 16 | 17 | // Automatically clear mock calls and instances between every test 18 | clearMocks: true, 19 | 20 | // Indicates whether the coverage information should be collected while executing the test 21 | // collectCoverage: false, 22 | 23 | // An array of glob patterns indicating a set of files for which coverage information should be collected 24 | // collectCoverageFrom: undefined, 25 | 26 | // The directory where Jest should output its coverage files 27 | // coverageDirectory: undefined, 28 | 29 | // An array of regexp pattern strings used to skip coverage collection 30 | // coveragePathIgnorePatterns: [ 31 | // "/node_modules/" 32 | // ], 33 | 34 | // Indicates which provider should be used to instrument code for coverage 35 | // coverageProvider: "babel", 36 | 37 | // A list of reporter names that Jest uses when writing coverage reports 38 | // coverageReporters: [ 39 | // "json", 40 | // "text", 41 | // "lcov", 42 | // "clover" 43 | // ], 44 | 45 | // An object that configures minimum threshold enforcement for coverage results 46 | // coverageThreshold: undefined, 47 | 48 | // A path to a custom dependency extractor 49 | // dependencyExtractor: undefined, 50 | 51 | // Make calling deprecated APIs throw helpful error messages 52 | // errorOnDeprecated: false, 53 | 54 | // Force coverage collection from ignored files using an array of glob patterns 55 | // forceCoverageMatch: [], 56 | 57 | // A path to a module which exports an async function that is triggered once before all test suites 58 | // globalSetup: undefined, 59 | 60 | // A path to a module which exports an async function that is triggered once after all test suites 61 | // globalTeardown: undefined, 62 | 63 | // A set of global variables that need to be available in all test environments 64 | // globals: {}, 65 | 66 | // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. 67 | // maxWorkers: "50%", 68 | 69 | // An array of directory names to be searched recursively up from the requiring module's location 70 | // moduleDirectories: [ 71 | // "node_modules" 72 | // ], 73 | 74 | // An array of file extensions your modules use 75 | // moduleFileExtensions: [ 76 | // "js", 77 | // "jsx", 78 | // "ts", 79 | // "tsx", 80 | // "json", 81 | // "node" 82 | // ], 83 | 84 | // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module 85 | // moduleNameMapper: {}, 86 | 87 | // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader 88 | // modulePathIgnorePatterns: [], 89 | 90 | // Activates notifications for test results 91 | // notify: false, 92 | 93 | // An enum that specifies notification mode. Requires { notify: true } 94 | // notifyMode: "failure-change", 95 | 96 | // A preset that is used as a base for Jest's configuration 97 | // preset: undefined, 98 | 99 | // Run tests from one or more projects 100 | // projects: undefined, 101 | 102 | // Use this configuration option to add custom reporters to Jest 103 | // reporters: undefined, 104 | 105 | // Automatically reset mock state between every test 106 | // resetMocks: false, 107 | 108 | // Reset the module registry before running each individual test 109 | // resetModules: false, 110 | 111 | // A path to a custom resolver 112 | // resolver: undefined, 113 | 114 | // Automatically restore mock state between every test 115 | // restoreMocks: false, 116 | 117 | // The root directory that Jest should scan for tests and modules within 118 | // rootDir: undefined, 119 | 120 | // A list of paths to directories that Jest should use to search for files in 121 | // roots: [ 122 | // "" 123 | // ], 124 | 125 | // Allows you to use a custom runner instead of Jest's default test runner 126 | // runner: "jest-runner", 127 | 128 | // The paths to modules that run some code to configure or set up the testing environment before each test 129 | // setupFiles: [], 130 | 131 | // A list of paths to modules that run some code to configure or set up the testing framework before each test 132 | // setupFilesAfterEnv: [], 133 | 134 | // The number of seconds after which a test is considered as slow and reported as such in the results. 135 | // slowTestThreshold: 5, 136 | 137 | // A list of paths to snapshot serializer modules Jest should use for snapshot testing 138 | // snapshotSerializers: [], 139 | 140 | // The test environment that will be used for testing 141 | testEnvironment: "jsdom", 142 | 143 | // Options that will be passed to the testEnvironment 144 | // testEnvironmentOptions: {}, 145 | 146 | // Adds a location field to test results 147 | // testLocationInResults: false, 148 | 149 | // The glob patterns Jest uses to detect test files 150 | testMatch: ["**/test.js"], 151 | 152 | // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped 153 | testPathIgnorePatterns: ["/node_modules/"], 154 | 155 | // The regexp pattern or array of patterns that Jest uses to detect test files 156 | // testRegex: [], 157 | 158 | // This option allows the use of a custom results processor 159 | // testResultsProcessor: undefined, 160 | 161 | // This option allows use of a custom test runner 162 | // testRunner: "jest-circus/runner", 163 | 164 | // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href 165 | // testURL: "http://localhost", 166 | 167 | // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout" 168 | // timers: "real", 169 | 170 | // A map from regular expressions to paths to transformers 171 | transform: { 172 | "^.+\\.js$": "babel-jest", 173 | "^.+\\.(md|mdx)$": path.resolve(__dirname, "cra.js"), 174 | }, 175 | 176 | // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation 177 | transformIgnorePatterns: ["/node_modules/", "\\.pnp\\.[^\\/]+$"], 178 | 179 | // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them 180 | // unmockedModulePathPatterns: undefined, 181 | 182 | // Indicates whether each individual test should be reported during the run 183 | verbose: true, 184 | 185 | // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode 186 | // watchPathIgnorePatterns: [], 187 | 188 | // Whether to use watchman for file crawling 189 | // watchman: true, 190 | } 191 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | /* 2 | * For a detailed explanation regarding each configuration property, visit: 3 | * https://jestjs.io/docs/configuration 4 | */ 5 | const path = require("path") 6 | 7 | module.exports = { 8 | // All imported modules in your tests should be mocked automatically 9 | // automock: false, 10 | 11 | // Stop running tests after `n` failures 12 | // bail: 0, 13 | 14 | // The directory where Jest should store its cached dependency information 15 | // cacheDirectory: "/private/var/folders/zw/mkp1m62d5jv_p2g0yjnp78vw0000gn/T/jest_dx", 16 | 17 | // Automatically clear mock calls and instances between every test 18 | clearMocks: true, 19 | 20 | // Indicates whether the coverage information should be collected while executing the test 21 | // collectCoverage: false, 22 | 23 | // An array of glob patterns indicating a set of files for which coverage information should be collected 24 | // collectCoverageFrom: undefined, 25 | 26 | // The directory where Jest should output its coverage files 27 | // coverageDirectory: undefined, 28 | 29 | // An array of regexp pattern strings used to skip coverage collection 30 | // coveragePathIgnorePatterns: [ 31 | // "/node_modules/" 32 | // ], 33 | 34 | // Indicates which provider should be used to instrument code for coverage 35 | // coverageProvider: "babel", 36 | 37 | // A list of reporter names that Jest uses when writing coverage reports 38 | // coverageReporters: [ 39 | // "json", 40 | // "text", 41 | // "lcov", 42 | // "clover" 43 | // ], 44 | 45 | // An object that configures minimum threshold enforcement for coverage results 46 | // coverageThreshold: undefined, 47 | 48 | // A path to a custom dependency extractor 49 | // dependencyExtractor: undefined, 50 | 51 | // Make calling deprecated APIs throw helpful error messages 52 | // errorOnDeprecated: false, 53 | 54 | // Force coverage collection from ignored files using an array of glob patterns 55 | // forceCoverageMatch: [], 56 | 57 | // A path to a module which exports an async function that is triggered once before all test suites 58 | // globalSetup: undefined, 59 | 60 | // A path to a module which exports an async function that is triggered once after all test suites 61 | // globalTeardown: undefined, 62 | 63 | // A set of global variables that need to be available in all test environments 64 | // globals: {}, 65 | 66 | // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. 67 | // maxWorkers: "50%", 68 | 69 | // An array of directory names to be searched recursively up from the requiring module's location 70 | // moduleDirectories: [ 71 | // "node_modules" 72 | // ], 73 | 74 | // An array of file extensions your modules use 75 | // moduleFileExtensions: [ 76 | // "js", 77 | // "jsx", 78 | // "ts", 79 | // "tsx", 80 | // "json", 81 | // "node" 82 | // ], 83 | 84 | // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module 85 | // moduleNameMapper: {}, 86 | 87 | // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader 88 | // modulePathIgnorePatterns: [], 89 | 90 | // Activates notifications for test results 91 | // notify: false, 92 | 93 | // An enum that specifies notification mode. Requires { notify: true } 94 | // notifyMode: "failure-change", 95 | 96 | // A preset that is used as a base for Jest's configuration 97 | // preset: undefined, 98 | 99 | // Run tests from one or more projects 100 | // projects: undefined, 101 | 102 | // Use this configuration option to add custom reporters to Jest 103 | // reporters: undefined, 104 | 105 | // Automatically reset mock state between every test 106 | // resetMocks: false, 107 | 108 | // Reset the module registry before running each individual test 109 | // resetModules: false, 110 | 111 | // A path to a custom resolver 112 | // resolver: undefined, 113 | 114 | // Automatically restore mock state between every test 115 | // restoreMocks: false, 116 | 117 | // The root directory that Jest should scan for tests and modules within 118 | // rootDir: undefined, 119 | 120 | // A list of paths to directories that Jest should use to search for files in 121 | // roots: [ 122 | // "" 123 | // ], 124 | 125 | // Allows you to use a custom runner instead of Jest's default test runner 126 | // runner: "jest-runner", 127 | 128 | // The paths to modules that run some code to configure or set up the testing environment before each test 129 | // setupFiles: [], 130 | 131 | // A list of paths to modules that run some code to configure or set up the testing framework before each test 132 | // setupFilesAfterEnv: [], 133 | 134 | // The number of seconds after which a test is considered as slow and reported as such in the results. 135 | // slowTestThreshold: 5, 136 | 137 | // A list of paths to snapshot serializer modules Jest should use for snapshot testing 138 | // snapshotSerializers: [], 139 | 140 | // The test environment that will be used for testing 141 | testEnvironment: "jsdom", 142 | 143 | // Options that will be passed to the testEnvironment 144 | // testEnvironmentOptions: {}, 145 | 146 | // Adds a location field to test results 147 | // testLocationInResults: false, 148 | 149 | // The glob patterns Jest uses to detect test files 150 | testMatch: ["**/test.js"], 151 | 152 | // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped 153 | testPathIgnorePatterns: ["/node_modules/"], 154 | 155 | // The regexp pattern or array of patterns that Jest uses to detect test files 156 | // testRegex: [], 157 | 158 | // This option allows the use of a custom results processor 159 | // testResultsProcessor: undefined, 160 | 161 | // This option allows use of a custom test runner 162 | // testRunner: "jest-circus/runner", 163 | 164 | // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href 165 | // testURL: "http://localhost", 166 | 167 | // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout" 168 | // timers: "real", 169 | 170 | // A map from regular expressions to paths to transformers 171 | transform: { 172 | "^.+\\.js$": "babel-jest", 173 | "^.+\\.(md|mdx)$": path.resolve(__dirname, "index.js"), 174 | }, 175 | 176 | // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation 177 | transformIgnorePatterns: ["/node_modules/", "\\.pnp\\.[^\\/]+$"], 178 | 179 | // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them 180 | // unmockedModulePathPatterns: undefined, 181 | 182 | // Indicates whether each individual test should be reported during the run 183 | verbose: true, 184 | 185 | // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode 186 | // watchPathIgnorePatterns: [], 187 | 188 | // Whether to use watchman for file crawling 189 | // watchman: true, 190 | } 191 | -------------------------------------------------------------------------------- /jest.config.mdx-options-file.js: -------------------------------------------------------------------------------- 1 | /* 2 | * For a detailed explanation regarding each configuration property, visit: 3 | * https://jestjs.io/docs/configuration 4 | */ 5 | const path = require("path") 6 | const rehypeSlug = require("rehype-slug") 7 | 8 | module.exports = { 9 | // All imported modules in your tests should be mocked automatically 10 | // automock: false, 11 | 12 | // Stop running tests after `n` failures 13 | // bail: 0, 14 | 15 | // The directory where Jest should store its cached dependency information 16 | // cacheDirectory: "/private/var/folders/zw/mkp1m62d5jv_p2g0yjnp78vw0000gn/T/jest_dx", 17 | 18 | // Automatically clear mock calls and instances between every test 19 | clearMocks: true, 20 | 21 | // Indicates whether the coverage information should be collected while executing the test 22 | // collectCoverage: false, 23 | 24 | // An array of glob patterns indicating a set of files for which coverage information should be collected 25 | // collectCoverageFrom: undefined, 26 | 27 | // The directory where Jest should output its coverage files 28 | // coverageDirectory: undefined, 29 | 30 | // An array of regexp pattern strings used to skip coverage collection 31 | // coveragePathIgnorePatterns: [ 32 | // "/node_modules/" 33 | // ], 34 | 35 | // Indicates which provider should be used to instrument code for coverage 36 | // coverageProvider: "babel", 37 | 38 | // A list of reporter names that Jest uses when writing coverage reports 39 | // coverageReporters: [ 40 | // "json", 41 | // "text", 42 | // "lcov", 43 | // "clover" 44 | // ], 45 | 46 | // An object that configures minimum threshold enforcement for coverage results 47 | // coverageThreshold: undefined, 48 | 49 | // A path to a custom dependency extractor 50 | // dependencyExtractor: undefined, 51 | 52 | // Make calling deprecated APIs throw helpful error messages 53 | // errorOnDeprecated: false, 54 | 55 | // Force coverage collection from ignored files using an array of glob patterns 56 | // forceCoverageMatch: [], 57 | 58 | // A path to a module which exports an async function that is triggered once before all test suites 59 | // globalSetup: undefined, 60 | 61 | // A path to a module which exports an async function that is triggered once after all test suites 62 | // globalTeardown: undefined, 63 | 64 | // A set of global variables that need to be available in all test environments 65 | // globals: {}, 66 | 67 | // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. 68 | // maxWorkers: "50%", 69 | 70 | // An array of directory names to be searched recursively up from the requiring module's location 71 | // moduleDirectories: [ 72 | // "node_modules" 73 | // ], 74 | 75 | // An array of file extensions your modules use 76 | // moduleFileExtensions: [ 77 | // "js", 78 | // "jsx", 79 | // "ts", 80 | // "tsx", 81 | // "json", 82 | // "node" 83 | // ], 84 | 85 | // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module 86 | // moduleNameMapper: {}, 87 | 88 | // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader 89 | // modulePathIgnorePatterns: [], 90 | 91 | // Activates notifications for test results 92 | // notify: false, 93 | 94 | // An enum that specifies notification mode. Requires { notify: true } 95 | // notifyMode: "failure-change", 96 | 97 | // A preset that is used as a base for Jest's configuration 98 | // preset: undefined, 99 | 100 | // Run tests from one or more projects 101 | // projects: undefined, 102 | 103 | // Use this configuration option to add custom reporters to Jest 104 | // reporters: undefined, 105 | 106 | // Automatically reset mock state between every test 107 | // resetMocks: false, 108 | 109 | // Reset the module registry before running each individual test 110 | // resetModules: false, 111 | 112 | // A path to a custom resolver 113 | // resolver: undefined, 114 | 115 | // Automatically restore mock state between every test 116 | // restoreMocks: false, 117 | 118 | // The root directory that Jest should scan for tests and modules within 119 | // rootDir: undefined, 120 | 121 | // A list of paths to directories that Jest should use to search for files in 122 | // roots: [ 123 | // "" 124 | // ], 125 | 126 | // Allows you to use a custom runner instead of Jest's default test runner 127 | // runner: "jest-runner", 128 | 129 | // The paths to modules that run some code to configure or set up the testing environment before each test 130 | // setupFiles: [], 131 | 132 | // A list of paths to modules that run some code to configure or set up the testing framework before each test 133 | // setupFilesAfterEnv: [], 134 | 135 | // The number of seconds after which a test is considered as slow and reported as such in the results. 136 | // slowTestThreshold: 5, 137 | 138 | // A list of paths to snapshot serializer modules Jest should use for snapshot testing 139 | // snapshotSerializers: [], 140 | 141 | // The test environment that will be used for testing 142 | testEnvironment: "jsdom", 143 | 144 | // Options that will be passed to the testEnvironment 145 | // testEnvironmentOptions: {}, 146 | 147 | // Adds a location field to test results 148 | // testLocationInResults: false, 149 | 150 | // The glob patterns Jest uses to detect test files 151 | testMatch: ["**/test.mdx-options.js"], 152 | 153 | // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped 154 | testPathIgnorePatterns: ["/node_modules/"], 155 | 156 | // The regexp pattern or array of patterns that Jest uses to detect test files 157 | // testRegex: [], 158 | 159 | // This option allows the use of a custom results processor 160 | // testResultsProcessor: undefined, 161 | 162 | // This option allows use of a custom test runner 163 | // testRunner: "jest-circus/runner", 164 | 165 | // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href 166 | // testURL: "http://localhost", 167 | 168 | // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout" 169 | // timers: "real", 170 | 171 | // A map from regular expressions to paths to transformers 172 | transform: { 173 | "^.+\\.js$": "babel-jest", 174 | "^.+\\.(md|mdx)$": [ 175 | path.resolve(__dirname, "mdx-options.cjs"), 176 | { 177 | // relative to project root 178 | mdxOptions: "test/fixtures/mdx-options.js", 179 | }, 180 | ], 181 | }, 182 | 183 | // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation 184 | transformIgnorePatterns: ["/node_modules/", "\\.pnp\\.[^\\/]+$"], 185 | 186 | // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them 187 | // unmockedModulePathPatterns: undefined, 188 | 189 | // Indicates whether each individual test should be reported during the run 190 | verbose: true, 191 | 192 | // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode 193 | // watchPathIgnorePatterns: [], 194 | 195 | // Whether to use watchman for file crawling 196 | // watchman: true, 197 | } 198 | -------------------------------------------------------------------------------- /jest.config.mdx-options.js: -------------------------------------------------------------------------------- 1 | /* 2 | * For a detailed explanation regarding each configuration property, visit: 3 | * https://jestjs.io/docs/configuration 4 | */ 5 | const path = require("path") 6 | const rehypeSlug = require("rehype-slug") 7 | 8 | module.exports = { 9 | // All imported modules in your tests should be mocked automatically 10 | // automock: false, 11 | 12 | // Stop running tests after `n` failures 13 | // bail: 0, 14 | 15 | // The directory where Jest should store its cached dependency information 16 | // cacheDirectory: "/private/var/folders/zw/mkp1m62d5jv_p2g0yjnp78vw0000gn/T/jest_dx", 17 | 18 | // Automatically clear mock calls and instances between every test 19 | clearMocks: true, 20 | 21 | // Indicates whether the coverage information should be collected while executing the test 22 | // collectCoverage: false, 23 | 24 | // An array of glob patterns indicating a set of files for which coverage information should be collected 25 | // collectCoverageFrom: undefined, 26 | 27 | // The directory where Jest should output its coverage files 28 | // coverageDirectory: undefined, 29 | 30 | // An array of regexp pattern strings used to skip coverage collection 31 | // coveragePathIgnorePatterns: [ 32 | // "/node_modules/" 33 | // ], 34 | 35 | // Indicates which provider should be used to instrument code for coverage 36 | // coverageProvider: "babel", 37 | 38 | // A list of reporter names that Jest uses when writing coverage reports 39 | // coverageReporters: [ 40 | // "json", 41 | // "text", 42 | // "lcov", 43 | // "clover" 44 | // ], 45 | 46 | // An object that configures minimum threshold enforcement for coverage results 47 | // coverageThreshold: undefined, 48 | 49 | // A path to a custom dependency extractor 50 | // dependencyExtractor: undefined, 51 | 52 | // Make calling deprecated APIs throw helpful error messages 53 | // errorOnDeprecated: false, 54 | 55 | // Force coverage collection from ignored files using an array of glob patterns 56 | // forceCoverageMatch: [], 57 | 58 | // A path to a module which exports an async function that is triggered once before all test suites 59 | // globalSetup: undefined, 60 | 61 | // A path to a module which exports an async function that is triggered once after all test suites 62 | // globalTeardown: undefined, 63 | 64 | // A set of global variables that need to be available in all test environments 65 | // globals: {}, 66 | 67 | // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. 68 | // maxWorkers: "50%", 69 | 70 | // An array of directory names to be searched recursively up from the requiring module's location 71 | // moduleDirectories: [ 72 | // "node_modules" 73 | // ], 74 | 75 | // An array of file extensions your modules use 76 | // moduleFileExtensions: [ 77 | // "js", 78 | // "jsx", 79 | // "ts", 80 | // "tsx", 81 | // "json", 82 | // "node" 83 | // ], 84 | 85 | // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module 86 | // moduleNameMapper: {}, 87 | 88 | // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader 89 | // modulePathIgnorePatterns: [], 90 | 91 | // Activates notifications for test results 92 | // notify: false, 93 | 94 | // An enum that specifies notification mode. Requires { notify: true } 95 | // notifyMode: "failure-change", 96 | 97 | // A preset that is used as a base for Jest's configuration 98 | // preset: undefined, 99 | 100 | // Run tests from one or more projects 101 | // projects: undefined, 102 | 103 | // Use this configuration option to add custom reporters to Jest 104 | // reporters: undefined, 105 | 106 | // Automatically reset mock state between every test 107 | // resetMocks: false, 108 | 109 | // Reset the module registry before running each individual test 110 | // resetModules: false, 111 | 112 | // A path to a custom resolver 113 | // resolver: undefined, 114 | 115 | // Automatically restore mock state between every test 116 | // restoreMocks: false, 117 | 118 | // The root directory that Jest should scan for tests and modules within 119 | // rootDir: undefined, 120 | 121 | // A list of paths to directories that Jest should use to search for files in 122 | // roots: [ 123 | // "" 124 | // ], 125 | 126 | // Allows you to use a custom runner instead of Jest's default test runner 127 | // runner: "jest-runner", 128 | 129 | // The paths to modules that run some code to configure or set up the testing environment before each test 130 | // setupFiles: [], 131 | 132 | // A list of paths to modules that run some code to configure or set up the testing framework before each test 133 | // setupFilesAfterEnv: [], 134 | 135 | // The number of seconds after which a test is considered as slow and reported as such in the results. 136 | // slowTestThreshold: 5, 137 | 138 | // A list of paths to snapshot serializer modules Jest should use for snapshot testing 139 | // snapshotSerializers: [], 140 | 141 | // The test environment that will be used for testing 142 | testEnvironment: "jsdom", 143 | 144 | // Options that will be passed to the testEnvironment 145 | // testEnvironmentOptions: {}, 146 | 147 | // Adds a location field to test results 148 | // testLocationInResults: false, 149 | 150 | // The glob patterns Jest uses to detect test files 151 | testMatch: ["**/test.mdx-options.js"], 152 | 153 | // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped 154 | testPathIgnorePatterns: ["/node_modules/"], 155 | 156 | // The regexp pattern or array of patterns that Jest uses to detect test files 157 | // testRegex: [], 158 | 159 | // This option allows the use of a custom results processor 160 | // testResultsProcessor: undefined, 161 | 162 | // This option allows use of a custom test runner 163 | // testRunner: "jest-circus/runner", 164 | 165 | // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href 166 | // testURL: "http://localhost", 167 | 168 | // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout" 169 | // timers: "real", 170 | 171 | // A map from regular expressions to paths to transformers 172 | transform: { 173 | "^.+\\.js$": "babel-jest", 174 | "^.+\\.(md|mdx)$": [ 175 | path.resolve(__dirname, "mdx-options.cjs"), 176 | { 177 | mdxOptions: { 178 | rehypePlugins: [rehypeSlug], 179 | }, 180 | }, 181 | ], 182 | }, 183 | 184 | // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation 185 | transformIgnorePatterns: ["/node_modules/", "\\.pnp\\.[^\\/]+$"], 186 | 187 | // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them 188 | // unmockedModulePathPatterns: undefined, 189 | 190 | // Indicates whether each individual test should be reported during the run 191 | verbose: true, 192 | 193 | // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode 194 | // watchPathIgnorePatterns: [], 195 | 196 | // Whether to use watchman for file crawling 197 | // watchman: true, 198 | } 199 | -------------------------------------------------------------------------------- /jest.config.user-settings.js: -------------------------------------------------------------------------------- 1 | /* 2 | * For a detailed explanation regarding each configuration property, visit: 3 | * https://jestjs.io/docs/configuration 4 | */ 5 | const path = require("path") 6 | 7 | module.exports = { 8 | // All imported modules in your tests should be mocked automatically 9 | // automock: false, 10 | 11 | // Stop running tests after `n` failures 12 | // bail: 0, 13 | 14 | // The directory where Jest should store its cached dependency information 15 | // cacheDirectory: "/private/var/folders/zw/mkp1m62d5jv_p2g0yjnp78vw0000gn/T/jest_dx", 16 | 17 | // Automatically clear mock calls and instances between every test 18 | clearMocks: true, 19 | 20 | // Indicates whether the coverage information should be collected while executing the test 21 | // collectCoverage: false, 22 | 23 | // An array of glob patterns indicating a set of files for which coverage information should be collected 24 | // collectCoverageFrom: undefined, 25 | 26 | // The directory where Jest should output its coverage files 27 | // coverageDirectory: undefined, 28 | 29 | // An array of regexp pattern strings used to skip coverage collection 30 | // coveragePathIgnorePatterns: [ 31 | // "/node_modules/" 32 | // ], 33 | 34 | // Indicates which provider should be used to instrument code for coverage 35 | // coverageProvider: "babel", 36 | 37 | // A list of reporter names that Jest uses when writing coverage reports 38 | // coverageReporters: [ 39 | // "json", 40 | // "text", 41 | // "lcov", 42 | // "clover" 43 | // ], 44 | 45 | // An object that configures minimum threshold enforcement for coverage results 46 | // coverageThreshold: undefined, 47 | 48 | // A path to a custom dependency extractor 49 | // dependencyExtractor: undefined, 50 | 51 | // Make calling deprecated APIs throw helpful error messages 52 | // errorOnDeprecated: false, 53 | 54 | // Force coverage collection from ignored files using an array of glob patterns 55 | // forceCoverageMatch: [], 56 | 57 | // A path to a module which exports an async function that is triggered once before all test suites 58 | // globalSetup: undefined, 59 | 60 | // A path to a module which exports an async function that is triggered once after all test suites 61 | // globalTeardown: undefined, 62 | 63 | // A set of global variables that need to be available in all test environments 64 | // globals: {}, 65 | 66 | // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. 67 | // maxWorkers: "50%", 68 | 69 | // An array of directory names to be searched recursively up from the requiring module's location 70 | // moduleDirectories: [ 71 | // "node_modules" 72 | // ], 73 | 74 | // An array of file extensions your modules use 75 | // moduleFileExtensions: [ 76 | // "js", 77 | // "jsx", 78 | // "ts", 79 | // "tsx", 80 | // "json", 81 | // "node" 82 | // ], 83 | 84 | // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module 85 | // moduleNameMapper: {}, 86 | 87 | // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader 88 | // modulePathIgnorePatterns: [], 89 | 90 | // Activates notifications for test results 91 | // notify: false, 92 | 93 | // An enum that specifies notification mode. Requires { notify: true } 94 | // notifyMode: "failure-change", 95 | 96 | // A preset that is used as a base for Jest's configuration 97 | // preset: undefined, 98 | 99 | // Run tests from one or more projects 100 | // projects: undefined, 101 | 102 | // Use this configuration option to add custom reporters to Jest 103 | // reporters: undefined, 104 | 105 | // Automatically reset mock state between every test 106 | // resetMocks: false, 107 | 108 | // Reset the module registry before running each individual test 109 | // resetModules: false, 110 | 111 | // A path to a custom resolver 112 | // resolver: undefined, 113 | 114 | // Automatically restore mock state between every test 115 | // restoreMocks: false, 116 | 117 | // The root directory that Jest should scan for tests and modules within 118 | // rootDir: undefined, 119 | 120 | // A list of paths to directories that Jest should use to search for files in 121 | // roots: [ 122 | // "" 123 | // ], 124 | 125 | // Allows you to use a custom runner instead of Jest's default test runner 126 | // runner: "jest-runner", 127 | 128 | // The paths to modules that run some code to configure or set up the testing environment before each test 129 | // setupFiles: [], 130 | 131 | // A list of paths to modules that run some code to configure or set up the testing framework before each test 132 | // setupFilesAfterEnv: [], 133 | 134 | // The number of seconds after which a test is considered as slow and reported as such in the results. 135 | // slowTestThreshold: 5, 136 | 137 | // A list of paths to snapshot serializer modules Jest should use for snapshot testing 138 | // snapshotSerializers: [], 139 | 140 | // The test environment that will be used for testing 141 | testEnvironment: "jsdom", 142 | 143 | // Options that will be passed to the testEnvironment 144 | // testEnvironmentOptions: {}, 145 | 146 | // Adds a location field to test results 147 | // testLocationInResults: false, 148 | 149 | // The glob patterns Jest uses to detect test files 150 | testMatch: ["**/test.user-settings.js"], 151 | 152 | // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped 153 | testPathIgnorePatterns: ["/node_modules/"], 154 | 155 | // The regexp pattern or array of patterns that Jest uses to detect test files 156 | // testRegex: [], 157 | 158 | // This option allows the use of a custom results processor 159 | // testResultsProcessor: undefined, 160 | 161 | // This option allows use of a custom test runner 162 | // testRunner: "jest-circus/runner", 163 | 164 | // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href 165 | // testURL: "http://localhost", 166 | 167 | // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout" 168 | // timers: "real", 169 | 170 | // A map from regular expressions to paths to transformers 171 | transform: { 172 | "^.+\\.js$": "babel-jest", 173 | "^.+\\.(md|mdx)$": [ 174 | path.resolve(__dirname, "index.js"), 175 | { frontMatterName: "meta" }, 176 | ], 177 | }, 178 | 179 | // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation 180 | transformIgnorePatterns: ["/node_modules/", "\\.pnp\\.[^\\/]+$"], 181 | 182 | // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them 183 | // unmockedModulePathPatterns: undefined, 184 | 185 | // Indicates whether each individual test should be reported during the run 186 | verbose: true, 187 | 188 | // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode 189 | // watchPathIgnorePatterns: [], 190 | 191 | // Whether to use watchman for file crawling 192 | // watchman: true, 193 | } 194 | -------------------------------------------------------------------------------- /mdx-options.cjs: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | const path = require("path") 4 | const matter = require("gray-matter") 5 | const stringifyObject = require("stringify-object") 6 | const mdx = require("@mdx-js/mdx") 7 | const babelJest = require("babel-jest") 8 | 9 | // we support either a path to a file, or the options itself 10 | // see: https://github.com/bitttttten/jest-transformer-mdx/pull/20 11 | function resolveMdxOptions(src) { 12 | if (typeof src === "string") { 13 | return require(path.resolve(process.cwd(), src)) 14 | } 15 | return src 16 | } 17 | 18 | function parseFrontMatter(src, frontMatterName = "frontMatterName") { 19 | const { content, data } = matter(src) 20 | 21 | return `export const ${frontMatterName} = ${stringifyObject(data)}; 22 | 23 | ${content}` 24 | } 25 | 26 | // this helper resolves both jest 27, and versions of jest below 27 27 | // as the way that transformer config is picked up has changed 28 | // see: https://github.com/bitttttten/jest-transformer-mdx/issues/22 29 | function resolveOptions(config) { 30 | if (config?.transformerConfig) { 31 | return config.transformerConfig 32 | } 33 | 34 | if (config?.transform && Array.isArray(config.transform)) { 35 | for (let i = 0; i < config.transform.length; i++) { 36 | if (new RegExp(config.transform[i][0]).test(filename)) { 37 | return config.transform[i][2] 38 | } 39 | } 40 | } 41 | 42 | return {} 43 | } 44 | 45 | module.exports = { 46 | async processAsync(src, filepath, config) { 47 | const options = resolveOptions(config) 48 | const mdxOptions = resolveMdxOptions(options?.mdxOptions) 49 | 50 | const withFrontMatter = parseFrontMatter(src, options?.frontMatterName) 51 | 52 | const jsx = await mdx(withFrontMatter, { ...mdxOptions, filepath }) 53 | 54 | const toTransform = `import {mdx} from '@mdx-js/react';${jsx}` 55 | 56 | // supports babel-jest@27 (which exports with .default) and older versions 57 | // see: https://github.com/bitttttten/jest-transformer-mdx/issues/22 58 | const babelProcess = babelJest.default?.process ?? babelJest.process 59 | return babelProcess(toTransform, filepath, config).code 60 | }, 61 | } 62 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jest-transformer-mdx", 3 | "version": "3.3.0", 4 | "description": "Jest transformer for MDX", 5 | "main": "index.js", 6 | "scripts": { 7 | "test:index": "jest --config jest.config.js", 8 | "test:cra": "jest --config jest.config.cra.js", 9 | "test:user-settings": "jest --config jest.config.user-settings.js", 10 | "test:mdx-options-inline": "NODE_OPTIONS=--experimental-vm-modules npx jest --no-cache --config jest.config.mdx-options.js", 11 | "test:mdx-options-file": "NODE_OPTIONS=--experimental-vm-modules npx jest --no-cache --config jest.config.mdx-options-file.js", 12 | "test:mdx-options": "npm run test:mdx-options-inline && npm run test:mdx-options-inline", 13 | "test": "npm run test:index && npm run test:cra && npm run test:user-settings" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "git+https://github.com/bitttttten/jest-transformer-md.git" 18 | }, 19 | "keywords": [ 20 | "jest", 21 | "transform", 22 | "markdown", 23 | "md", 24 | "frontmatter" 25 | ], 26 | "author": "bitttttten ", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/bitttttten/jest-transformer-md/issues" 30 | }, 31 | "homepage": "https://github.com/bitttttten/jest-transformer-md#readme", 32 | "dependencies": { 33 | "@mdx-js/mdx": "^1.0.21", 34 | "@mdx-js/react": "^1.0.21", 35 | "babel-jest": "^27.0.1", 36 | "babel-preset-react-app": "^9.0.0", 37 | "gray-matter": "^4.0.2", 38 | "react": "^16.8.6", 39 | "rehype-slug": "^4.0.1", 40 | "stringify-object": "^3.3.0" 41 | }, 42 | "peerDependencies": { 43 | "babel-jest": "^27.0.1", 44 | "jest": "^27.0.1" 45 | }, 46 | "devDependencies": { 47 | "@babel/core": "^7.14.3", 48 | "@babel/preset-env": "^7.6.2", 49 | "@testing-library/react": "^8.0.4", 50 | "jest": "^27.0.1", 51 | "prettier": "^1.18.2", 52 | "react-dom": "^16.8.6", 53 | "standard-version": "^9.3.0" 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Component, { frontMatter } from "./test.md"; 3 | import { render } from "@testing-library/react"; 4 | 5 | describe("jest transformer md", () => { 6 | test("that frontMatter is correct", () => { 7 | expect(frontMatter.title).toBe("Jest Transformer MD"); 8 | expect(frontMatter.path).toBe("https://soulpicks.com"); 9 | expect(frontMatter.from).toBe("Batman"); 10 | expect(frontMatter.location).toBe("Amsterdam"); 11 | }); 12 | test("that it still renders in react", () => { 13 | const { asFragment } = render(); 14 | expect(asFragment()).toMatchInlineSnapshot(` 15 | 16 |

17 | Every man who has lotted here over the centuries, has looked up to the light and imagined climbing to freedom. So easy, so simple! And like shipwrecked men turning to seawater foregoing uncontrollable thirst, many have died trying. And then here there can be no true despair without hope. So as I terrorize Gotham, I will feed its people hope to poison their souls. I will let them believe that they can survive so that you can watch them climbing over each other to stay in the sun. You can watch me torture an entire city. And then when you've truly understood the depth of your failure, we will fulfill Ra's Al Ghul's destiny. We will destroy Gotham. And then, when that is done, and Gotham is... ashes. Then you have my permission to die. 18 |

19 |

20 | Heading 1 21 |

22 |

23 | Heading 2 24 |

25 |

26 | Heading 3 27 |

28 |

29 | Heading 4 30 |

31 |
32 | Heading 5 33 |
34 |
35 | Heading 6 36 |
37 |

38 | This is a list: 39 |

40 |
    41 |
  • 42 | List item 1 43 |
  • 44 |
  • 45 | List item 2 46 |
  • 47 |
  • 48 | List item 3 49 |
  • 50 |
51 |
52 | `); 53 | }); 54 | }); 55 | -------------------------------------------------------------------------------- /test.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Jest Transformer MD 3 | path: https://soulpicks.com 4 | from: Batman 5 | location: Amsterdam 6 | --- 7 | 8 | Every man who has lotted here over the centuries, has looked up to the light and imagined climbing to freedom. So easy, so simple! And like shipwrecked men turning to seawater foregoing uncontrollable thirst, many have died trying. And then here there can be no true despair without hope. So as I terrorize Gotham, I will feed its people hope to poison their souls. I will let them believe that they can survive so that you can watch them climbing over each other to stay in the sun. You can watch me torture an entire city. And then when you've truly understood the depth of your failure, we will fulfill Ra's Al Ghul's destiny. We will destroy Gotham. And then, when that is done, and Gotham is... ashes. Then you have my permission to die. 9 | 10 | # Heading 1 11 | ## Heading 2 12 | ### Heading 3 13 | #### Heading 4 14 | ##### Heading 5 15 | ###### Heading 6 16 | 17 | This is a list: 18 | 19 | - List item 1 20 | - List item 2 21 | - List item 3 22 | -------------------------------------------------------------------------------- /test.mdx-options.js: -------------------------------------------------------------------------------- 1 | import React from "react" 2 | import Component, { frontMatter } from "./test.md" 3 | import { render } from "@testing-library/react" 4 | 5 | describe("jest transformer md", () => { 6 | test("that frontMatter is correct", () => { 7 | expect(frontMatter.title).toBe("Jest Transformer MD") 8 | expect(frontMatter.path).toBe("https://soulpicks.com") 9 | expect(frontMatter.from).toBe("Batman") 10 | expect(frontMatter.location).toBe("Amsterdam") 11 | }) 12 | test("it renders with rehype-slug", () => { 13 | const { asFragment } = render() 14 | expect(asFragment()).toMatchInlineSnapshot(` 15 | 16 |

17 | Every man who has lotted here over the centuries, has looked up to the light and imagined climbing to freedom. So easy, so simple! And like shipwrecked men turning to seawater foregoing uncontrollable thirst, many have died trying. And then here there can be no true despair without hope. So as I terrorize Gotham, I will feed its people hope to poison their souls. I will let them believe that they can survive so that you can watch them climbing over each other to stay in the sun. You can watch me torture an entire city. And then when you've truly understood the depth of your failure, we will fulfill Ra's Al Ghul's destiny. We will destroy Gotham. And then, when that is done, and Gotham is... ashes. Then you have my permission to die. 18 |

19 |

22 | Heading 1 23 |

24 |

27 | Heading 2 28 |

29 |

32 | Heading 3 33 |

34 |

37 | Heading 4 38 |

39 |
42 | Heading 5 43 |
44 |
47 | Heading 6 48 |
49 |

50 | This is a list: 51 |

52 |
    53 |
  • 54 | List item 1 55 |
  • 56 |
  • 57 | List item 2 58 |
  • 59 |
  • 60 | List item 3 61 |
  • 62 |
63 |
64 | `) 65 | }) 66 | }) 67 | -------------------------------------------------------------------------------- /test.user-settings.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import Component, { meta } from "./test.md"; 3 | import { render } from "@testing-library/react"; 4 | 5 | describe("jest transformer md", () => { 6 | test("that meta is correct", () => { 7 | expect(meta.title).toBe("Jest Transformer MD"); 8 | expect(meta.path).toBe("https://soulpicks.com"); 9 | expect(meta.from).toBe("Batman"); 10 | expect(meta.location).toBe("Amsterdam"); 11 | }); 12 | test("that it still renders in react", () => { 13 | const { asFragment } = render(); 14 | expect(asFragment()).toMatchInlineSnapshot(` 15 | 16 |

17 | Every man who has lotted here over the centuries, has looked up to the light and imagined climbing to freedom. So easy, so simple! And like shipwrecked men turning to seawater foregoing uncontrollable thirst, many have died trying. And then here there can be no true despair without hope. So as I terrorize Gotham, I will feed its people hope to poison their souls. I will let them believe that they can survive so that you can watch them climbing over each other to stay in the sun. You can watch me torture an entire city. And then when you've truly understood the depth of your failure, we will fulfill Ra's Al Ghul's destiny. We will destroy Gotham. And then, when that is done, and Gotham is... ashes. Then you have my permission to die. 18 |

19 |

20 | Heading 1 21 |

22 |

23 | Heading 2 24 |

25 |

26 | Heading 3 27 |

28 |

29 | Heading 4 30 |

31 |
32 | Heading 5 33 |
34 |
35 | Heading 6 36 |
37 |

38 | This is a list: 39 |

40 |
    41 |
  • 42 | List item 1 43 |
  • 44 |
  • 45 | List item 2 46 |
  • 47 |
  • 48 | List item 3 49 |
  • 50 |
51 |
52 | `); 53 | }); 54 | }); 55 | -------------------------------------------------------------------------------- /test/fixtures/mdx-options.js: -------------------------------------------------------------------------------- 1 | const rehypeSlug = require("rehype-slug") 2 | 3 | const options = { 4 | rehypePlugins: [rehypeSlug], 5 | } 6 | 7 | module.exports = options 8 | --------------------------------------------------------------------------------