Skip to content

Commit

Permalink
feat: jest preset
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Aug 13, 2018
1 parent 12146c3 commit beb50b5
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 22 deletions.
6 changes: 1 addition & 5 deletions e2e/__templates__/default/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
moduleFileExtensions: ['ts', 'js'],
preset: 'ts-jest',
testEnvironment: 'node',
globals: { 'ts-jest': { tsConfig: {} } },
};
6 changes: 1 addition & 5 deletions e2e/__templates__/with-babel-6/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
moduleFileExtensions: ['ts', 'js'],
preset: 'ts-jest',
testEnvironment: 'node',
globals: { 'ts-jest': { tsConfig: {}, babelJest: true } },
};
6 changes: 1 addition & 5 deletions e2e/__templates__/with-babel-7/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
moduleFileExtensions: ['ts', 'js'],
preset: 'ts-jest',
testEnvironment: 'node',
globals: { 'ts-jest': { tsConfig: {}, babelJest: true } },
};
6 changes: 1 addition & 5 deletions e2e/__templates__/with-jest-22/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
moduleFileExtensions: ['ts', 'js'],
preset: 'ts-jest',
testEnvironment: 'node',
globals: { 'ts-jest': { tsConfig: {} } },
};
3 changes: 3 additions & 0 deletions jest-preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const createJestPreset = require('./dist/utils/create-jest-preset').default;

module.exports = createJestPreset();
2 changes: 1 addition & 1 deletion src/__snapshots__/ts-jest-transformer.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ var lower_1 = __importDefault(require(\\"./lower\\"));describe('hello', function
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZpbGUudHMiXSwibmFtZXMiOlsiamVzdCIsIm1vY2siLCJzIiwidG9VcHBlckNhc2UiLCJfX2ltcG9ydERlZmF1bHQiLCJtb2QiLCJfX2VzTW9kdWxlIiwidXBwZXJfMSIsInJlcXVpcmUiLCJsb3dlcl8xIiwiZGVzY3JpYmUiLCJ0ZXN0IiwidG9Mb3dlckNhc2UiLCJleHBlY3QiLCJkZWZhdWx0IiwidG9CZSJdLCJtYXBwaW5ncyI6IjtBQUNBOztBQU1BQSxLQUFLQyxJQUFMLENBQVUsU0FBVixFQUFxQixZQUFZO0FBQUUsV0FBTyxVQUFVQyxDQUFWLEVBQWE7QUFBRSxlQUFPQSxFQUFFQyxXQUFGLEVBQVA7QUFBeUIsS0FBL0M7QUFBa0QsQ0FBckY7QUFMQSxJQUFJQyxrQkFBbUIsUUFBUSxLQUFLQSxlQUFkLElBQWtDLFVBQVVDLEdBQVYsRUFBZTtBQUNuRSxXQUFRQSxPQUFPQSxJQUFJQyxVQUFaLEdBQTBCRCxHQUExQixHQUFnQyxFQUFFLFdBQVdBLEdBQWIsRUFBdkM7QUFDSCxDQUZEO0FBR0EsSUFBSUUsVUFBVUgsZ0JBQWdCSSxRQUFRLFNBQVIsQ0FBaEIsQ0FBZDtBQUNBLElBQUlDLFVBQVVMLGdCQUFnQkksUUFBUSxTQUFSLENBQWhCLENBQWQsQ0FFQUUsU0FBUyxPQUFULEVBQWtCLFlBQVk7QUFDMUJDLFNBQUssU0FBTCxFQU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QobG93ZXJfMS5kZWZhdWx0KCdIRUxMTycpKS50b0JlKCdoZWxsbycpO1xuICAgICAgICBqZXN0Lm1vY2soJy4vbG93ZXInLCBmdW5jdGlvbiAoKSB7IHJldHVybiBmdW5jdGlvbiAocykgeyByZXR1cm4gcy50b0xvd2VyQ2FzZSgpOyB9OyB9KTtcbiAgICB9KTtcbn0pO1xuIl19"
`;

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\\""`;
exports[`process stringifyContentPathRegex should create a module with stringified content as export 1`] = `"module.exports=\\"\\\\n<div>\\\\n <span>some text with \`backtick\`</span>\\\\n</div>\\\\n\\""`;
16 changes: 16 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as tsJest from './index';

describe('ts-jest', () => {
it('should export a `createTransformer` function', () => {
expect(typeof tsJest.createTransformer).toBe('function');
});
it('should export a `process` function', () => {
expect(typeof tsJest.process).toBe('function');
});
it('should export a `getCacheKey` function', () => {
expect(typeof tsJest.getCacheKey).toBe('function');
});
it('should export a `jestPreset` object', () => {
expect(typeof tsJest.jestPreset).toBe('object');
});
});
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import TsJestTransformer from './ts-jest-transformer';
import createJestPreset from './utils/create-jest-preset';

// TODO: allow a `TsJestGlobalOptions` object to be give to createTransformer()
// so that presets could totally customize and extend the transfomer;
Expand All @@ -13,4 +14,6 @@ function getCacheKey(...args: any[]): any {
return (createTransformer() as any).getCacheKey(...args);
}

export { createTransformer, tsProcess as process, getCacheKey };
const jestPreset = createJestPreset();

export { createTransformer, tsProcess as process, getCacheKey, jestPreset };
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ export interface TsJestHooksMap {
result: string | jest.TransformedSource,
): string | jest.TransformedSource | void;
}

export interface CreateJestPresetOptions {
allowJs?: boolean;
}
22 changes: 22 additions & 0 deletions src/utils/create-jest-preset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defaults } from 'jest-config';
import { CreateJestPresetOptions } from '../types';

// TODO: find out if tsconfig that we'll use contains `allowJs`
// and change the transform so that it also uses ts-jest for js files

export default function createJestPreset({
allowJs = false,
}: CreateJestPresetOptions = {}) {
return {
transform: {
...defaults.transform,
[allowJs ? '^.+\\.[tj]sx?$' : '^.+\\.tsx?$']: 'ts-jest',
},
testMatch: [
...defaults.testMatch,
'**/__tests__/**/*.ts?(x)',
'**/?(*.)+(spec|test).ts?(x)',
],
moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts', 'tsx'],
};
}

0 comments on commit beb50b5

Please sign in to comment.