我的最终目标是能够用Jest编写可以从@mdx-js/mdx
导入的测试,@mdx-js/mdx
是一个ESM模块,由于library package under development是为commonsjs配置的,所以我在jest.config.ts中使用了transform
和transformIgnorePatterns
方法。
/* eslint-disable */
import { readFileSync } from 'fs';
import type { Config } from 'jest';
// Reading the SWC compilation config and remove the "exclude"
// for the test files to be compiled by SWC
const { exclude: _, ...swcJestConfig } = JSON.parse(readFileSync(`${__dirname}/.swcrc`, 'utf-8'));
// disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves.
// If we do not disable this, SWC Core will read .swcrc and won't transform our test files due to "exclude"
if (swcJestConfig.swcrc === undefined) {
swcJestConfig.swcrc = false;
}
// Uncomment if using global setup/teardown files being transformed via swc
// https://nx.dev/packages/jest/documents/overview#global-setup/teardown-with-nx-libraries
// jest needs EsModule Interop to find the default exported setup/teardown functions
// swcJestConfig.module.noInterop = false;
export default {
displayName: 'jest-mdx-transformer',
preset: '../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['@swc/jest', swcJestConfig],
},
transformIgnorePatterns: [
'/node_modules/(?!(@mdx-js/mdx|develop|markdown-extensions|unist-util-stringify-position|vfile|vfile-message)/)',
],
moduleFileExtensions: ['ts', 'js', 'json', 'mdx', 'html'],
testEnvironment: 'node',
coverageDirectory: '../../coverage/libs/jest-mdx-transformer',
} satisfies Config;
字符串
我的典型过程是不断扩展这个RegExp,直到Jest不再抱怨在node_modules中转换ESM,直到我添加develop
包之前,它一直工作得很好。无论它是否存在,都会产生相同的错误:
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
/workspace/perpetual-cabbage/node_modules/devlop/lib/default.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export function deprecate(fn) {
^^^^^^
SyntaxError: Unexpected token 'export'
3 | describe('watheia.cabbage/jest-mdx-transformer', () => {
4 | it('should render mdx in jest', async () => {
> 5 | const result = await compile('# Hello, World!');
| ^
6 | expect(result).toBeDefined();
7 | });
8 | });
at Runtime.createScriptFromCode (../../node_modules/jest-runtime/build/index.js:1505:14)
at Object.<anonymous> (../../node_modules/@mdx-js/mdx/lib/core.js:136:17)
at Object.<anonymous> (../../node_modules/@mdx-js/mdx/lib/compile.js:40:15)
at Object.<anonymous> (../../node_modules/@mdx-js/mdx/index.js:46:18)
at Object.<anonymous> (test/jest-mdx-transformer.spec.ts:5:14)
型
为什么它转换了我添加的所有其他包,但没有开发?我看不出这个包有什么特别的不同。我希望它像那个RegExp中的所有其他包一样被编译。
虽然我想了解这里发生了什么,但我愿意接受任何可能的解决方案。
谢谢你的时间!
1条答案
按热度按时间t5fffqht1#
我能够提出a workaround,但我将等待接受答案,因为它不是可移植的,并且没有解决Jest无法按预期转换包的根本问题。
由于我使用的是Nx monorepo,我可以创建一个简单的wrapper library来从
@mdx-js/mdx
导出符号,然后使用rollup将这个库捆绑为ESM和CJS。接下来,更新消费库以从dist工件导入,Jest将能够从那里获取完全转换的CJS模块。不需要进一步的转换。字符串
如果可能的话,请更新构建系统,以便在运行测试之前调用构建。对于nx,您可以在project.json中设置
dependsOn
选项。