Skip to content

Commit

Permalink
feat(babel-jest): wrap babel-jest to support conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Apr 13, 2020
1 parent 12ad591 commit b774a3c
Show file tree
Hide file tree
Showing 12 changed files with 1,345 additions and 6 deletions.
1 change: 1 addition & 0 deletions lerna.json
Expand Up @@ -40,6 +40,7 @@
"packages/validation-i18n",
"packages/web-components",
"packages/webpack-loader",
"packages/babel-jest",
"test/1kcomponents",
"test/browserstack",
"test/cypress",
Expand Down
1,173 changes: 1,168 additions & 5 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -49,6 +49,8 @@
},
"devDependencies": {
"@commitlint/cli": "^8.2.0",
"@jest/transform": "^25.3.0",
"@jest/types": "^25.3.0",
"@types/acorn": "^4.0.5",
"@types/fancy-log": "^1.3.1",
"@types/jsdom": "^12.2.4",
Expand All @@ -66,6 +68,7 @@
"@types/webpack-env": "^1.14.1",
"@typescript-eslint/eslint-plugin": "^2.12.0",
"@typescript-eslint/parser": "^2.12.0",
"babel-jest": "^25.3.0",
"chalk": "^3.0.0",
"codecov": "^3.6.1",
"command-exists": "^1.2.8",
Expand Down
6 changes: 6 additions & 0 deletions packages/babel-jest/.eslintrc.js
@@ -0,0 +1,6 @@
module.exports = {
extends: ['../../.eslintrc.js'],
rules: {
'import/no-nodejs-modules': 'off'
}
};
1 change: 1 addition & 0 deletions packages/babel-jest/.npmrc
@@ -0,0 +1 @@
package-lock=false
1 change: 1 addition & 0 deletions packages/babel-jest/CHANGELOG.md
@@ -0,0 +1 @@
# Change Log
21 changes: 21 additions & 0 deletions packages/babel-jest/LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2010 - 2018 Blue Spire Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions packages/babel-jest/README.md
@@ -0,0 +1,47 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lernajs.io/)
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)
[![CircleCI](https://circleci.com/gh/aurelia/aurelia.svg?style=shield)](https://circleci.com/gh/aurelia/aurelia)
[![Test Coverage](https://api.codeclimate.com/v1/badges/5ac0e13689735698073a/test_coverage)](https://codeclimate.com/github/aurelia/aurelia/test_coverage)
[![npm](https://img.shields.io/npm/v/@aurelia/babel-jest.svg?maxAge=3600)](https://www.npmjs.com/package/@aurelia/babel-jest)
# @aurelia/babel-jest

## Installing

For the latest stable version:

```bash
npm i -D @aurelia/babel-jest
```

For our nightly builds:

```bash
npm i -D @aurelia/babel-jest@dev
```

## Usage

In `jest.config.js` or `package.json`:

```js
"transform": {
"^.+\\.js$": "@aurelia/babel-jest"
}
```

Use ShadowDOM:

```js
"transform": {
"^.+\\.js$": ["@aurelia/babel-jest", { defaultShadowOptions: { mode: 'open' } }]
}
```

Use CSS Modules:

```js
"transform": {
"^.+\\.js$": ["@aurelia/babel-jest", { useCSSModule: true }]
}
```
48 changes: 48 additions & 0 deletions packages/babel-jest/package.json
@@ -0,0 +1,48 @@
{
"name": "@aurelia/babel-jest",
"version": "0.6.0",
"main": "dist/esnext/index.js",
"module": "dist/esnext/index.js",
"types": "dist/index.d.ts",
"typings": "dist/index.d.ts",
"license": "MIT",
"homepage": "https://aurelia.io",
"repository": {
"type": "git",
"url": "https://github.com/aurelia/aurelia"
},
"bugs": {
"url": "https://github.com/aurelia/aurelia/issues"
},
"keywords": [
"aurelia",
"jest"
],
"files": [
"dist",
"src",
"README.md",
"CHANGELOG.md",
"LICENSE"
],
"scripts": {
"lint": "eslint --cache --ext .js,.ts src/",
"build": "tsc -b",
"bundle": "ts-node -P ../../tsconfig.json ../../scripts/bundle.ts umd,esm,system babel-jest",
"dev": "tsc -b -w"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@aurelia/plugin-conventions": "0.6.0",
"babel-jest": "^25.3.0"
},
"devDependencies": {
"@jest/transform": "^25.3.0",
"@jest/types": "^25.3.0",
"@types/node": "^12.12.21",
"tslib": "^1.10.0",
"typescript": "^3.7.4"
}
}
38 changes: 38 additions & 0 deletions packages/babel-jest/src/index.ts
@@ -0,0 +1,38 @@
import { IOptionalPreprocessOptions, preprocess, preprocessOptions } from '@aurelia/plugin-conventions';
import { createTransformer } from 'babel-jest';
import type { Config } from '@jest/types';
import type { TransformOptions, Transformer } from '@jest/transform';
import * as path from 'path';

const transformer = createTransformer();
const { process } = transformer;

function newProcess(
sourceText: string,
sourcePath: Config.Path,
config: Config.ProjectConfig,
transformOptions?: TransformOptions
) {
try {
const allOptions = preprocessOptions((transformOptions as IOptionalPreprocessOptions) || {});
const result = preprocess(
{ path: sourcePath, contents: sourceText },
allOptions
);
let newSourcePath = sourcePath;
if (result !== undefined) {
if (allOptions.templateExtensions.includes(path.extname(sourcePath))) {
// Rewrite foo.html to foo.html.js, or foo.md to foo.md.js
newSourcePath += '.js';
}
// TODO: to check. This might have complication on jest caches.
return process(result.code, newSourcePath, config, transformOptions);
}
} catch (e) {
// ignore
}
return process(sourceText, sourcePath, config, transformOptions);
}

transformer.process = newProcess;
export = transformer;
9 changes: 9 additions & 0 deletions packages/babel-jest/tsconfig.json
@@ -0,0 +1,9 @@
{
"extends": "../tsconfig-build.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "dist",
"outDir": "dist/esnext"
},
"include": ["src"]
}
3 changes: 2 additions & 1 deletion packages/tsconfig.json
Expand Up @@ -33,6 +33,7 @@
{ "path": "testing" },
{ "path": "validation" },
{ "path": "web-components" },
{ "path": "webpack-loader" }
{ "path": "webpack-loader" },
{ "path": "babel-jest" }
]
}

0 comments on commit b774a3c

Please sign in to comment.