Skip to content

Commit

Permalink
[zero][system] Create jest transformer to test
Browse files Browse the repository at this point in the history
zero-runtime styled components
  • Loading branch information
brijeshb42 committed Sep 20, 2023
1 parent 7d063c0 commit 92e90fc
Show file tree
Hide file tree
Showing 4 changed files with 417 additions and 68 deletions.
3 changes: 3 additions & 0 deletions packages/zero-jest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @mui/zero-jest

Jest transformer for zero-runtime styled testing support.
56 changes: 56 additions & 0 deletions packages/zero-jest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@mui/zero-jest",
"version": "0.0.1-alpha.1",
"private": true,
"author": "MUI Team",
"description": "Jest transformer for zero-runtime styled testing support.",
"main": "./src/index.ts",
"keywords": [
"zero runtime",
"css-in-js",
"mui",
"jest"
],
"repository": {
"type": "git",
"url": "https://github.com/mui/material-ui.git",
"directory": "packages/zero-jest"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/mui/material-ui/issues"
},
"homepage": "@TODO",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/mui"
},
"scripts": {
"build": "yarn build:legacy && yarn build:modern && yarn build:node && yarn build:stable && yarn build:copy-files",
"build:legacy": "node ../../scripts/build.mjs legacy",
"build:modern": "node ../../scripts/build.mjs modern",
"build:node": "node ../../scripts/build.mjs node",
"build:stable": "node ../../scripts/build.mjs stable",
"build:copy-files": "node ../../scripts/copyFiles.mjs",
"prebuild": "rimraf build tsconfig.build.tsbuildinfo",
"release": "yarn build && npm publish build",
"test": "cd ../../ && cross-env NODE_ENV=test mocha 'packages/zero-babel-plugin/**/*.test.{js,ts,tsx}'"
},
"dependencies": {
"@linaria/babel-preset": "^4.5.4",
"@linaria/utils": "^4.5.3",
"@mui/zero-tag-processor": "0.0.1-alpha.1",
"babel-jest": "^29.7.0"
},
"devDependencies": {
"@jest/transform": "^29.7.0"
},
"peerDependencies": {},
"sideEffects": false,
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=12.0.0"
}
}
83 changes: 83 additions & 0 deletions packages/zero-jest/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { TransformCacheCollection, transform as linariaTransform } from '@linaria/babel-preset';
import { syncResolve, asyncResolveFallback } from '@linaria/utils';
import babelJest from 'babel-jest';

const transformCache = new TransformCacheCollection();

function getLinariaConfig(filename, options) {
const { theme = {}, cssVariablesPrefix = 'mui' } = options ?? {};
return {
filename,
pluginOptions: {
themeArgs: {
theme,
},
cssVariablesPrefix,
},
};
}

/**
* @typedef {Object} TransformerConfig
* @property {unknown} theme
*/

/**
*
* @param {TransformerConfig} userOptions
* @returns
*/
function createZeroTransformer(userOptions) {
const { theme, cssVariablesPrefix = 'mui', ...rest } = userOptions;
const babelTransformer = babelJest.createTransformer({
...rest,
plugins: (userOptions?.rest ?? []).concat('@mui/zero-tag-processor/pre-linaria-plugin'),
});
// eslint-disable-next-line global-require
const { transformSync: linariaTransformSync } = require('@linaria/babel-preset/lib/transform');

/** @type {import("@jest/transform").Transformer<TransformerConfig>} */
const zeroTransformer = {
...babelTransformer,
process(sourceText, sourcePath, options) {
const { code } = babelTransformer.process(sourceText, sourcePath, options);
const linariaResult = linariaTransformSync(
code ?? sourceText,
getLinariaConfig(sourcePath, {
theme,
cssVariablesPrefix,
}),
syncResolve,
{},
transformCache,
);
return {
code: linariaResult?.code ?? sourceText,
map: linariaResult?.map,
};
},
async processAsync(sourceText, sourcePath, options) {
const { code } = babelTransformer.processAsync(sourceText, sourcePath, options);
const linariaResult = await linariaTransform(
code ?? sourceText,
getLinariaConfig(sourcePath, {
theme,
cssVariablesPrefix,
}),
asyncResolveFallback,
{},
transformCache,
);
return {
code: linariaResult?.code ?? code ?? sourceText,
map: linariaResult?.map,
};
},
};

return zeroTransformer;
}

const transformerFactory = { createTransformer: createZeroTransformer };

export default transformerFactory;
Loading

0 comments on commit 92e90fc

Please sign in to comment.