From 3fcb4bd4da9d1cdb2c16bad24c2265ed23fbaf8c Mon Sep 17 00:00:00 2001 From: Huafu Gandon Date: Mon, 13 Aug 2018 15:35:18 +0200 Subject: [PATCH] feat: handles stringifyContentPathRegex --- .prettierignore | 7 ++ .prettierrc | 12 +++ e2e/__tests__/hoisting.test.ts | 2 +- e2e/__tests__/simple.test.ts | 2 +- e2e/__tests__/source-map.test.ts | 1 - package-lock.json | 21 ++--- package.json | 23 +++-- src/__helpers__/fakers.ts | 12 ++- .../ts-jest-transformer.spec.ts.snap | 2 + src/ts-jest-transformer.spec.ts | 22 +++++ src/ts-jest-transformer.ts | 15 ++-- tslint.json | 85 ++++++++++--------- 12 files changed, 125 insertions(+), 79 deletions(-) create mode 100644 .prettierignore create mode 100644 .prettierrc diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000000..f00f04789b --- /dev/null +++ b/.prettierignore @@ -0,0 +1,7 @@ +dist/ +e2e/ +!e2e/__helpers__ +!e2e/__serializers__ +!e2e/__tests__ +node_modules/ +src/**/__snapshots__/ diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000000..835232af64 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,12 @@ +semi: true +printWidth: 80 +singleQuote: true +tabWidth: 2 +useTabs: false +overrides: + - files: "**/*.js" + options: + trailing-comma: es5 + - files: "**/*.ts" + options: + trailingComma: all diff --git a/e2e/__tests__/hoisting.test.ts b/e2e/__tests__/hoisting.test.ts index df6fa0393b..8b52d95a68 100644 --- a/e2e/__tests__/hoisting.test.ts +++ b/e2e/__tests__/hoisting.test.ts @@ -2,7 +2,7 @@ import { configureTestCase } from '../__helpers__/test-case'; import { allPackageSets } from '../__helpers__/templates'; describe('Hoisting test', () => { - const testCase = configureTestCase('hoisting', { args: ['--no-cache'] }); + const testCase = configureTestCase('hoisting'); testCase.runWithTemplates(allPackageSets, 0, (runTest, { testLabel }) => { it(testLabel, () => { diff --git a/e2e/__tests__/simple.test.ts b/e2e/__tests__/simple.test.ts index 58c4949560..cde69d80a8 100644 --- a/e2e/__tests__/simple.test.ts +++ b/e2e/__tests__/simple.test.ts @@ -2,7 +2,7 @@ import { configureTestCase } from '../__helpers__/test-case'; import { allPackageSets } from '../__helpers__/templates'; describe('Simple test', () => { - const testCase = configureTestCase('simple', { args: ['--no-cache'] }); + const testCase = configureTestCase('simple'); testCase.runWithTemplates(allPackageSets, 0, (runTest, { testLabel }) => { it(testLabel, () => { diff --git a/e2e/__tests__/source-map.test.ts b/e2e/__tests__/source-map.test.ts index b387596c7c..dbdc6647e8 100644 --- a/e2e/__tests__/source-map.test.ts +++ b/e2e/__tests__/source-map.test.ts @@ -2,7 +2,6 @@ import { configureTestCase } from '../__helpers__/test-case'; import { allPackageSets } from '../__helpers__/templates'; const testCase = configureTestCase('source-maps', { - args: ['--no-cache'], writeIo: true, }); diff --git a/package-lock.json b/package-lock.json index a4710d1ff7..7bd9f515a3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -89,9 +89,9 @@ } }, "@types/node": { - "version": "10.5.7", - "resolved": "https://registry.npmjs.org/@types/node/-/node-10.5.7.tgz", - "integrity": "sha512-VkKcfuitP+Nc/TaTFH0B8qNmn+6NbI6crLkQonbedViVz7O2w8QV/GERPlkJ4bg42VGHiEWa31CoTOPs1q6z1w==", + "version": "10.5.8", + "resolved": "https://registry.npmjs.org/@types/node/-/node-10.5.8.tgz", + "integrity": "sha512-sWSjw+bYW/2W+1V3m8tVsm9PKJcxk3NHN7oRqNUfEdofKg0Imbdu1dQbFvLKjZQXEDXRN6IfSMACjJ7Wv4NGCQ==", "dev": true }, "abab": { @@ -228,12 +228,6 @@ } } }, - "app-root-path": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/app-root-path/-/app-root-path-2.1.0.tgz", - "integrity": "sha1-mL9lmTJ+zqGZMJhm6BQDaP0uZGo=", - "dev": true - }, "append-transform": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/append-transform/-/append-transform-1.0.0.tgz", @@ -3710,12 +3704,11 @@ } }, "lint-staged": { - "version": "7.2.0", - "resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-7.2.0.tgz", - "integrity": "sha512-jPoIMbmgtWMUrz/l0rhBVa1j6H71zr0rEoxDWBA333PZcaqBvELdg0Sf4tdGHlwrBM0GXaXMVgTRkLTm2vA7Jg==", + "version": "7.2.2", + "resolved": "https://registry.npmjs.org/lint-staged/-/lint-staged-7.2.2.tgz", + "integrity": "sha512-BWT3kx242hq5oaKJ8QiazPeHwJnEXImvjmgZfjljMI5HX6RrTxI3cTJXywre6GNafMONCD/suFnEiFmC69Gscg==", "dev": true, "requires": { - "app-root-path": "^2.0.1", "chalk": "^2.3.1", "commander": "^2.14.1", "cosmiconfig": "^5.0.2", @@ -3725,7 +3718,7 @@ "find-parent-dir": "^0.3.0", "is-glob": "^4.0.0", "is-windows": "^1.0.2", - "jest-validate": "^23.0.0", + "jest-validate": "^23.5.0", "listr": "^0.14.1", "lodash": "^4.17.5", "log-symbols": "^2.2.0", diff --git a/package.json b/package.json index 7f46c27811..4dfd31a2ad 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,7 @@ "prepare": "npm run build", "prepublishOnly": "npm run test", "precommit": "lint-staged", - "postcommit": "git reset", - "format": "prettier --single-quote --trailing-comma all --write \"{src,scripts}/**/*.ts\" && prettier --single-quote --trailing-comma es5 --write \"{src,scripts}/**/*.js\"" + "postcommit": "git reset" }, "repository": { "type": "git", @@ -50,14 +49,14 @@ "@types/gist-package-json": "git+https://gist.github.com/5c1cc527fe6b5b7dba41fec7fe54bf6e.git", "@types/jest": "^23.3.1", "@types/lodash.set": "^4.3.4", - "@types/node": "^10.5.7", + "@types/node": "^10.5.8", "closest-file-data": "^0.1.4", "cross-spawn": "^6.0.5", "doctoc": "^1.3.1", "fs-extra": "^7.0.0", "husky": "^0.14.3", "jest": "^23.4.1", - "lint-staged": "^7.2.0", + "lint-staged": "^7.2.2", "lodash.set": "^4.3.2", "npm-run-all": "^4.1.3", "prettier": "^1.14.2", @@ -67,16 +66,14 @@ "typescript": "^3.0.1" }, "lint-staged": { - "*.js": [ - "prettier --write --single-quote --trailing-comma es5", - "git add" - ], - "*.ts": [ - "prettier --write --single-quote --trailing-comma all", - "git add" - ] + "linters": { + "*.{js,ts}": [ + "prettier --write", + "git add" + ] + } }, "engines": { "node": ">= 6" } -} +} \ No newline at end of file diff --git a/src/__helpers__/fakers.ts b/src/__helpers__/fakers.ts index 04b26284cd..9cc2c45710 100644 --- a/src/__helpers__/fakers.ts +++ b/src/__helpers__/fakers.ts @@ -1,7 +1,5 @@ import { TsJestGlobalOptions } from '../types'; -import { resolve, relative } from 'path'; -import spyThese from './spy-these'; -import realFs from 'fs'; +import { resolve } from 'path'; export function filePath(relPath: string): string { return resolve(__dirname, '..', '..', relPath); @@ -28,6 +26,14 @@ describe('hello', function () { `; } +export function htmlSource() { + return ` +
+ some text with \`backtilt\` +
+`; +} + export function typescriptSource() { return ` import upper from './upper'; diff --git a/src/__snapshots__/ts-jest-transformer.spec.ts.snap b/src/__snapshots__/ts-jest-transformer.spec.ts.snap index 7f52f44c72..b7281ae16e 100644 --- a/src/__snapshots__/ts-jest-transformer.spec.ts.snap +++ b/src/__snapshots__/ts-jest-transformer.spec.ts.snap @@ -27,3 +27,5 @@ var lower_1 = __importDefault(require(\\"./lower\\"));describe('hello', function }); //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZpbGUudHMiXSwibmFtZXMiOlsiamVzdCIsIm1vY2siLCJzIiwidG9VcHBlckNhc2UiLCJfX2ltcG9ydERlZmF1bHQiLCJtb2QiLCJfX2VzTW9kdWxlIiwidXBwZXJfMSIsInJlcXVpcmUiLCJsb3dlcl8xIiwiZGVzY3JpYmUiLCJ0ZXN0IiwidG9Mb3dlckNhc2UiLCJleHBlY3QiLCJkZWZhdWx0IiwidG9CZSJdLCJtYXBwaW5ncyI6IjtBQUNBOztBQU1BQSxLQUFLQyxJQUFML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ZCAmJiBtb2QuX19lc01vZHVsZSkgPyBtb2QgOiB7IFwiZGVmYXVsdFwiOiBtb2QgfTtcbn07XG52YXIgdXBwZXJfMSA9IF9faW1wb3J0RGVmYXVsdChyZXF1aXJlKFwiLi91cHBlclwiKSk7XG52YXIgbG93ZXJfMSA9IF9faW1wb3J0RGVmYXVsdChyZXF1aXJlKFwiLi9sb3dlclwiKSk7XG5qZXN0Lm1vY2soJy4vdXBwZXInLCBmdW5jdGlvbiAoKSB7IHJldHVybiBmdW5jdGlvbiAocykgeyByZXR1cm4gcy50b1VwcGVyQ2FzZSgpOyB9OyB9KTtcbmRlc2NyaWJlKCdoZWxsbycsIGZ1bmN0aW9uICgpIHtcbiAgICB0ZXN0KCdteSB0ZXN0JywgZnVuY3Rpb24gKCkge1xuICAgICAgICBleHBlY3QodXBwZXJfMS5kZWZhdWx0KCdoZWxsbycpKS50b0JlKCdIRUxMTycpO1xuICAgICAgICBleHBlY3QobG93ZXJfMS5kZWZhdWx0KCdIRUxMTycpKS50b0JlKCdoZWxsbycpO1xuICAgICAgICBqZXN0Lm1vY2soJy4vbG93ZXInLCBmdW5jdGlvbiAoKSB7IHJldHVybiBmdW5jdGlvbiAocykgeyByZXR1cm4gcy50b0xvd2VyQ2FzZSgpOyB9OyB9KTtcbiAgICB9KTtcbn0pO1xuIl19" `; + +exports[`process stringifyContentPathRegex should create a module with stringified content as export 1`] = `"module.exports=\\"\\\\n
\\\\n some text with \`backtilt\`\\\\n
\\\\n\\""`; diff --git a/src/ts-jest-transformer.spec.ts b/src/ts-jest-transformer.spec.ts index 8230439d1f..b439054806 100644 --- a/src/ts-jest-transformer.spec.ts +++ b/src/ts-jest-transformer.spec.ts @@ -44,6 +44,28 @@ describe('process', () => { expect(result.code).toMatchSnapshot(); }); }); // hoisting + + describe('stringifyContentPathRegex', () => { + const transformer = new TsJestTransformer(); + it('should create a module with stringified content as export', () => { + const config = fakers.jestConfig( + {}, + { stringifyContentPathRegex: '\\.html$' }, + ); + const source = fakers.htmlSource(); + const result = transformer.process( + source, + fakers.filePath('path/to/file.html'), + config, + ) as string; + expect(result).toMatchSnapshot(); + const importer = Function( + `const exports = {}, module = {exports:exports};${result};return module.exports;`, + ); + expect(importer).not.toThrow(); + expect(importer()).toEqual(source); + }); + }); // stringifyContentPathRegex }); // process describe('getCacheKey', () => { diff --git a/src/ts-jest-transformer.ts b/src/ts-jest-transformer.ts index 4b288629d2..4637c6e3b3 100644 --- a/src/ts-jest-transformer.ts +++ b/src/ts-jest-transformer.ts @@ -123,23 +123,26 @@ export default class TsJestTransformer implements jest.Transformer { let result: string | jest.TransformedSource; const config = this.configFor(jestConfig); - // handles here what we should simply stringify - if ( + const stringify = config.stringifyContentPathRegex && - config.stringifyContentPathRegex.test(filePath) - ) { - } + config.stringifyContentPathRegex.test(filePath); + const useBabelJest = !stringify && config.babelJest; // get the tranformer instance const program = this.programFor(jestConfig); const instrument: boolean = !!transformOptions && transformOptions.instrument; + // handles here what we should simply stringify + if (stringify) { + source = `module.exports=${JSON.stringify(source)}`; + } + // transpile TS code (source maps are included) result = program.transpileModule(filePath, source, instrument); // calling babel-jest transformer - if (config.babelJest) { + if (useBabelJest) { result = this.babelJestFor(jestConfig).process( result, filePath, diff --git a/tslint.json b/tslint.json index 7d29343109..d8e9367476 100644 --- a/tslint.json +++ b/tslint.json @@ -1,42 +1,47 @@ { - "defaultSeverity": "error", - "extends": [ - "tslint:recommended" + "defaultSeverity": "error", + "extends": [ + "tslint:recommended" + ], + "rules": { + "variable-name": [ + true, + "ban-keywords", + "check-format", + "allow-leading-underscore" ], - "rules": { - "variable-name": [true, "ban-keywords", "check-format", "allow-leading-underscore"], - "arrow-parens": false, - "quotemark": [ - true, - "single", - "jsx-double" - ], - "semicolon": [ - true, - "always" - ], - "interface-name": [ - true, - "never-prefix" - ], - "ordered-imports": false, - "align": false, - "object-literal-sort-keys": false, - "no-console": [ - true, - "debug", - "info", - "time", - "timeEnd", - "trace" - ], - "no-empty": false, - "member-access": false - }, - "linterOptions": { - "exclude": [ - "dist/**/*", - "**/node_modules/**/*" - ] - } -} + "arrow-parens": false, + "quotemark": [ + true, + "single", + "jsx-double" + ], + "semicolon": [ + true, + "always" + ], + "interface-name": [ + true, + "never-prefix" + ], + "ordered-imports": false, + "align": false, + "object-literal-sort-keys": false, + "no-console": [ + true, + "debug", + "info", + "time", + "timeEnd", + "trace" + ], + "no-empty": false, + "member-access": false + }, + "linterOptions": { + "exclude": [ + "dist/**/*", + "**/node_modules/**/*" + ] + } +} \ No newline at end of file