Skip to content

Commit

Permalink
feat: handles stringifyContentPathRegex
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Aug 13, 2018
1 parent ec271c7 commit 3fcb4bd
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 79 deletions.
7 changes: 7 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dist/
e2e/
!e2e/__helpers__
!e2e/__serializers__
!e2e/__tests__
node_modules/
src/**/__snapshots__/
12 changes: 12 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion e2e/__tests__/hoisting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/simple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down
1 change: 0 additions & 1 deletion e2e/__tests__/source-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
21 changes: 7 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 10 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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"
}
}
}
12 changes: 9 additions & 3 deletions src/__helpers__/fakers.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -28,6 +26,14 @@ describe('hello', function () {
`;
}

export function htmlSource() {
return `
<div>
<span>some text with \`backtilt\`</span>
</div>
`;
}

export function typescriptSource() {
return `
import upper from './upper';
Expand Down
2 changes: 2 additions & 0 deletions src/__snapshots__/ts-jest-transformer.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ var lower_1 = __importDefault(require(\\"./lower\\"));describe('hello', function
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZpbGUudHMiXSwibmFtZXMiOlsiamVzdCIsIm1vY2siLCJzIiwidG9VcHBlckNhc2UiLCJfX2ltcG9ydERlZmF1bHQiLCJtb2QiLCJfX2VzTW9kdWxlIiwidXBwZXJfMSIsInJlcXVpcmUiLCJsb3dlcl8xIiwiZGVzY3JpYmUiLCJ0ZXN0IiwidG9Mb3dlckNhc2UiLCJleHBlY3QiLCJkZWZhdWx0IiwidG9CZSJdLCJtYXBwaW5ncyI6IjtBQUNBOztBQU1BQSxLQUFLQyxJQUFMLENBQVUsU0FBVixFQUFxQixZQUFZO0FBQUUsV0FBTyxVQUFVQyxDQUFWLEVBQWE7QUFBRSxlQUFPQSxFQUFFQyxXQUFGLEVBQVA7QUFBeUIsS0FBL0M7QUFBa0QsQ0FBckY7QUFMQSxJQUFJQyxrQkFBbUIsUUFBUSxLQUFLQSxlQUFkLElBQWtDLFVBQVVDLEdBQVYsRUFBZTtBQUNuRSxXQUFRQSxPQUFPQSxJQUFJQyxVQUFaLEdBQTBCRCxHQUExQixHQUFnQyxFQUFFLFdBQVdBLEdBQWIsRUFBdkM7QUFDSCxDQUZEO0FBR0EsSUFBSUUsVUFBVUgsZ0JBQWdCSSxRQUFRLFNBQVIsQ0FBaEIsQ0FBZDtBQUNBLElBQUlDLFVBQVVMLGdCQUFnQkksUUFBUSxTQUFSLENBQWhCLENBQWQsQ0FFQUUsU0FBUyxPQUFULEVBQWtCLFlBQVk7QUFDMUJDLFNBQUssU0FBTCxFQUFnQixZQUFZO0FBR3hCWCxhQUFLQyxJQUFMLENBQVUsU0FBVixFQUFxQixZQUFZO0FBQUUsbUJBQU8sV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Jy4vbG93ZXInLCBmdW5jdGlvbiAoKSB7IHJldHVybiBmdW5jdGlvbiAocykgeyByZXR1cm4gcy50b0xvd2VyQ2FzZSgpOyB9OyB9KTtcbiAgICB9KTtcbn0pO1xuIl19"
`;

exports[`process stringifyContentPathRegex should create a module with stringified content as export 1`] = `"module.exports=\\"\\\\n<div>\\\\n <span>some text with \`backtilt\`</span>\\\\n</div>\\\\n\\""`;
22 changes: 22 additions & 0 deletions src/ts-jest-transformer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
15 changes: 9 additions & 6 deletions src/ts-jest-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
85 changes: 45 additions & 40 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -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/**/*"
]
}
}

0 comments on commit 3fcb4bd

Please sign in to comment.