From 5aaf7380b6c9f6fe490daea3c648fa30016c7da7 Mon Sep 17 00:00:00 2001 From: Chunpeng Huo Date: Tue, 14 Apr 2020 00:11:54 +1000 Subject: [PATCH] feat(ts-jest): wrap ts-jest to support conventions --- package-lock.json | 110 +++++++++++++++++++++++++++++++++ package.json | 1 + packages/ts-jest/.eslintrc.js | 6 ++ packages/ts-jest/.npmrc | 1 + packages/ts-jest/CHANGELOG.md | 1 + packages/ts-jest/LICENSE | 21 +++++++ packages/ts-jest/README.md | 47 ++++++++++++++ packages/ts-jest/package.json | 48 ++++++++++++++ packages/ts-jest/src/index.ts | 58 +++++++++++++++++ packages/ts-jest/tsconfig.json | 9 +++ 10 files changed, 302 insertions(+) create mode 100644 packages/ts-jest/.eslintrc.js create mode 100644 packages/ts-jest/.npmrc create mode 100644 packages/ts-jest/CHANGELOG.md create mode 100644 packages/ts-jest/LICENSE create mode 100644 packages/ts-jest/README.md create mode 100644 packages/ts-jest/package.json create mode 100644 packages/ts-jest/src/index.ts create mode 100644 packages/ts-jest/tsconfig.json diff --git a/package-lock.json b/package-lock.json index 14e14d80d8f..9a93d3c91a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4267,6 +4267,15 @@ "node-releases": "^1.1.44" } }, + "bs-logger": { + "version": "0.2.6", + "resolved": "https://registry.npmjs.org/bs-logger/-/bs-logger-0.2.6.tgz", + "integrity": "sha512-pd8DCoxmbgc7hyPKOvxtqNcjYoOsABPQdcCUjGp3d42VR2CX1ORhk2A87oqqu5R1kk+76nsxZupkmyd+MVtCog==", + "dev": true, + "requires": { + "fast-json-stable-stringify": "2.x" + } + }, "bser": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/bser/-/bser-2.1.1.tgz", @@ -16343,6 +16352,107 @@ "integrity": "sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=", "dev": true }, + "ts-jest": { + "version": "25.3.1", + "resolved": "https://registry.npmjs.org/ts-jest/-/ts-jest-25.3.1.tgz", + "integrity": "sha512-O53FtKguoMUByalAJW+NWEv7c4tus5ckmhfa7/V0jBb2z8v5rDSLFC1Ate7wLknYPC1euuhY6eJjQq4FtOZrkg==", + "dev": true, + "requires": { + "bs-logger": "0.x", + "buffer-from": "1.x", + "fast-json-stable-stringify": "2.x", + "json5": "2.x", + "lodash.memoize": "4.x", + "make-error": "1.x", + "micromatch": "4.x", + "mkdirp": "1.x", + "resolve": "1.x", + "semver": "6.x", + "yargs-parser": "18.x" + }, + "dependencies": { + "braces": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", + "integrity": "sha512-b8um+L1RzM3WDSzvhm6gIz1yfTbBt6YTlcEKAvsmqCZZFw46z626lVj9j1yEPW33H5H+lBQpZMP1k8l+78Ha0A==", + "dev": true, + "requires": { + "fill-range": "^7.0.1" + } + }, + "camelcase": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", + "dev": true + }, + "fill-range": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", + "integrity": "sha512-qOo9F+dMUmC2Lcb4BbVvnKJxTPjCm+RRpe4gDuGrzkL7mEVl/djYSu2OdQ2Pa302N4oqkSg9ir6jaLWJ2USVpQ==", + "dev": true, + "requires": { + "to-regex-range": "^5.0.1" + } + }, + "is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true + }, + "json5": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/json5/-/json5-2.1.3.tgz", + "integrity": "sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA==", + "dev": true, + "requires": { + "minimist": "^1.2.5" + } + }, + "micromatch": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.2.tgz", + "integrity": "sha512-y7FpHSbMUMoyPbYUSzO6PaZ6FyRnQOpHuKwbo1G+Knck95XVU4QAiKdGEnj5wwoS7PlOgthX/09u5iFJ+aYf5Q==", + "dev": true, + "requires": { + "braces": "^3.0.1", + "picomatch": "^2.0.5" + } + }, + "minimist": { + "version": "1.2.5", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz", + "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==", + "dev": true + }, + "mkdirp": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz", + "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", + "dev": true + }, + "to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, + "requires": { + "is-number": "^7.0.0" + } + }, + "yargs-parser": { + "version": "18.1.2", + "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-18.1.2.tgz", + "integrity": "sha512-hlIPNR3IzC1YuL1c2UwwDKpXlNFBqD1Fswwh1khz5+d8Cq/8yc/Mn0i+rQXduu8hcrFKvO7Eryk+09NecTQAAQ==", + "dev": true, + "requires": { + "camelcase": "^5.0.0", + "decamelize": "^1.2.0" + } + } + } + }, "ts-loader": { "version": "6.2.1", "resolved": "https://registry.npmjs.org/ts-loader/-/ts-loader-6.2.1.tgz", diff --git a/package.json b/package.json index fbc6db9f67b..05d5dcd1694 100644 --- a/package.json +++ b/package.json @@ -126,6 +126,7 @@ "touch": "^3.1.0", "ts-loader": "^6.2.1", "ts-node": "^8.5.4", + "ts-jest": "^25.3.1", "tsconfig-paths": "^3.9.0", "tslib": "^1.10.0", "tslint": "^5.20.1", diff --git a/packages/ts-jest/.eslintrc.js b/packages/ts-jest/.eslintrc.js new file mode 100644 index 00000000000..cc875cb3fc0 --- /dev/null +++ b/packages/ts-jest/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + extends: ['../../.eslintrc.js'], + rules: { + 'import/no-nodejs-modules': 'off' + } +}; diff --git a/packages/ts-jest/.npmrc b/packages/ts-jest/.npmrc new file mode 100644 index 00000000000..43c97e719a5 --- /dev/null +++ b/packages/ts-jest/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/packages/ts-jest/CHANGELOG.md b/packages/ts-jest/CHANGELOG.md new file mode 100644 index 00000000000..420e6f23d0e --- /dev/null +++ b/packages/ts-jest/CHANGELOG.md @@ -0,0 +1 @@ +# Change Log diff --git a/packages/ts-jest/LICENSE b/packages/ts-jest/LICENSE new file mode 100644 index 00000000000..0d4338e06b9 --- /dev/null +++ b/packages/ts-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. diff --git a/packages/ts-jest/README.md b/packages/ts-jest/README.md new file mode 100644 index 00000000000..2b5def058d5 --- /dev/null +++ b/packages/ts-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/ts-jest.svg?maxAge=3600)](https://www.npmjs.com/package/@aurelia/ts-jest) +# @aurelia/ts-jest + +## Installing + +For the latest stable version: + +```bash +npm i -D @aurelia/ts-jest +``` + +For our nightly builds: + +```bash +npm i -D @aurelia/ts-jest@dev +``` + +## Usage + +In `jest.config.js` or `package.json`: + +```js +"transform": { + "\\.(js|html)$": "@aurelia/ts-jest" +} +``` + +Use ShadowDOM: + +```js +"transform": { + "\\.(js|html)$": ["@aurelia/ts-jest", { defaultShadowOptions: { mode: 'open' } }] +} +``` + +Use CSS Modules: + +```js +"transform": { + "\\.(js|html)$": ["@aurelia/ts-jest", { useCSSModule: true }] +} +``` \ No newline at end of file diff --git a/packages/ts-jest/package.json b/packages/ts-jest/package.json new file mode 100644 index 00000000000..b27b31c9cda --- /dev/null +++ b/packages/ts-jest/package.json @@ -0,0 +1,48 @@ +{ + "name": "@aurelia/ts-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 ts-jest", + "dev": "tsc -b -w" + }, + "publishConfig": { + "access": "public" + }, + "dependencies": { + "@aurelia/plugin-conventions": "0.6.0", + "@jest/transform": "^25.3.0", + "@jest/types": "^25.3.0", + "ts-jest": "^25.3.1" + }, + "devDependencies": { + "@types/node": "^12.12.21", + "tslib": "^1.10.0", + "typescript": "^3.7.4" + } +} diff --git a/packages/ts-jest/src/index.ts b/packages/ts-jest/src/index.ts new file mode 100644 index 00000000000..976b68293ea --- /dev/null +++ b/packages/ts-jest/src/index.ts @@ -0,0 +1,58 @@ +import { IOptionalPreprocessOptions, preprocess, preprocessOptions } from '@aurelia/plugin-conventions'; +import { + canInstrument as tsCanInstrument, + getCacheKey as tsGetCacheKey, + process as tsProcess +} from 'ts-jest'; +import { Config } from '@jest/types'; +import { TransformOptions, TransformedSource, CacheKeyOptions } from '@jest/transform'; +import * as path from 'path'; + +function createTransformer(conventionsOptions = {}) { + const au2Options = preprocessOptions(conventionsOptions); + + function getCacheKey( + fileData: string, + filePath: Config.Path, + configStr: string, + options: CacheKeyOptions + ): string { + const tsKey = tsGetCacheKey!(fileData, filePath, configStr, options); + return `${tsKey}:${JSON.stringify(au2Options)}`; + } + + // Wrap ts-jest process + function process( + sourceText: string, + sourcePath: Config.Path, + config: Config.ProjectConfig, + transformOptions?: TransformOptions + ): TransformedSource { + try { + const result = preprocess( + { path: sourcePath, contents: sourceText }, + au2Options + ); + let newSourcePath = sourcePath; + if (result !== undefined) { + if (au2Options.templateExtensions.includes(path.extname(sourcePath))) { + // Rewrite foo.html to foo.html.ts, or foo.md to foo.md.ts + newSourcePath += '.ts'; + } + return tsProcess(result.code, newSourcePath, config, transformOptions); + } + } catch (e) { + // ignore + } + return tsProcess(sourceText, sourcePath, config, transformOptions); + } + + return { + canInstrument: tsCanInstrument, + getCacheKey, + process + }; +} + +const { canInstrument, getCacheKey, process } = createTransformer(); +export { canInstrument, getCacheKey, process, createTransformer }; diff --git a/packages/ts-jest/tsconfig.json b/packages/ts-jest/tsconfig.json new file mode 100644 index 00000000000..d07dd23ac98 --- /dev/null +++ b/packages/ts-jest/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../tsconfig-build.json", + "compilerOptions": { + "rootDir": "src", + "declarationDir": "dist", + "outDir": "dist/esnext" + }, + "include": ["src"] +}