Skip to content

Commit

Permalink
Merge pull request #438 from kamijin-fanta/feat/multi-project
Browse files Browse the repository at this point in the history
support for jest multi-projects
  • Loading branch information
kulshekhar committed Feb 11, 2018
2 parents 63f95bb + 36e93c6 commit ea5b0a5
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 7 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Jim Cummins <[email protected]>
Joscha Feth <[email protected]>
Junle Li <[email protected]>
Justin Bay <[email protected]>
Kamijin Fanta <[email protected]>
Kulshekhar Kabra <[email protected]>
Kyle Roach <[email protected]>
Marshall Bowers <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-jest",
"version": "22.0.3",
"version": "22.0.4",
"main": "index.js",
"types": "./dist/index.d.ts",
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
Expand Down
7 changes: 6 additions & 1 deletion src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export function process(
// https://github.com/kulshekhar/ts-jest/issues/201#issuecomment-300572902
const compilerOptions = getTSConfig(
jestConfig.globals,
jestConfig.rootDir,
transformOptions.instrument,
);

Expand Down Expand Up @@ -84,7 +85,11 @@ export function getCacheKey(
): string {
const jestConfig: JestConfig = JSON.parse(jestConfigStr);

const tsConfig = getTSConfig(jestConfig.globals, transformOptions.instrument);
const tsConfig = getTSConfig(
jestConfig.globals,
jestConfig.rootDir,
transformOptions.instrument,
);

return crypto
.createHash('md5')
Expand Down
8 changes: 7 additions & 1 deletion src/transpile-if-ts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import * as tsc from 'typescript';
import { getTSConfig, mockGlobalTSConfigSchema } from './utils';

export function transpileIfTypescript(path, contents, config?) {
export function transpileIfTypescript(
path,
contents,
config?,
rootDir: string = '',
) {
if (path && (path.endsWith('.tsx') || path.endsWith('.ts'))) {
let transpiled = tsc.transpileModule(contents, {
compilerOptions: getTSConfig(
config || mockGlobalTSConfigSchema(global),
rootDir,
true,
),
fileName: path,
Expand Down
12 changes: 8 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function formatTscParserErrors(errors: tsc.Diagnostic[]) {
return errors.map(s => JSON.stringify(s, null, 4)).join('\n');
}

function readCompilerOptions(configPath: string) {
configPath = path.resolve(configPath);
function readCompilerOptions(configPath: string, rootDir: string) {
configPath = path.resolve(rootDir, configPath);

// First step: Let tsc pick up the config.
const loaded = tsc.readConfigFile(configPath, file => {
Expand Down Expand Up @@ -118,7 +118,11 @@ export function mockGlobalTSConfigSchema(globals: any) {
const tsConfigCache: { [key: string]: any } = {};
// TODO: Perhaps rename collectCoverage to here, as it seems to be the official jest name now:
// https://github.com/facebook/jest/issues/3524
export function getTSConfig(globals, collectCoverage: boolean = false) {
export function getTSConfig(
globals,
rootDir: string = '',
collectCoverage: boolean = false,
) {
let configPath = getTSConfigPathFromConfig(globals);
logOnce(`Reading tsconfig file from path ${configPath}`);
const skipBabel = getTSJestConfig(globals).skipBabel;
Expand All @@ -135,7 +139,7 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
return tsConfigCache[tsConfigCacheKey];
}

const config = readCompilerOptions(configPath);
const config = readCompilerOptions(configPath, rootDir);
logOnce('Original typescript config before modifications: ', config);

// ts-jest will map lines numbers properly if inlineSourceMap and
Expand Down
10 changes: 10 additions & 0 deletions tests/__tests__/jest-projects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import runJest from '../__helpers__/runJest';

describe('Jest Projects', () => {
it('should compile typescript succesfully', () => {
const result = runJest('../jest-projects', ['--no-cache']);
const stderr = result.stderr;
expect(result.status).toEqual(0);
expect(stderr).toContain('1 passed, 1 total');
});
});
5 changes: 5 additions & 0 deletions tests/jest-projects/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"projects": ["<rootDir>", "<rootDir>/packages/*"]
}
}
3 changes: 3 additions & 0 deletions tests/jest-projects/packages/child/Hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const Hello = {
filed: 'Hello',
};
5 changes: 5 additions & 0 deletions tests/jest-projects/packages/child/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Hello } from '../Hello';

it('jest packages', () => {
expect(Hello.filed).toEqual('Hello');
});
8 changes: 8 additions & 0 deletions tests/jest-projects/packages/child/custom-tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": false
}
}
22 changes: 22 additions & 0 deletions tests/jest-projects/packages/child/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"jest": {
"transform": {
".(ts|tsx)": "ts-jest"
},
"moduleDirectories": [
"node_modules"
],
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"globals": {
"ts-jest": {
"tsConfigFile": "custom-tsconfig.json"
}
}
}
}

0 comments on commit ea5b0a5

Please sign in to comment.