Skip to content

Commit

Permalink
Bugfix/long file paths (#300)
Browse files Browse the repository at this point in the history
Use hashing to cache files to allow ts-jest to work with projects that have long paths.

* Revert "Remove test for long paths since we're not remapping anymore"

This reverts commit 9f5569e.
  • Loading branch information
patrickhousley authored and kulshekhar committed Aug 29, 2017
1 parent 2815a09 commit a413a04
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 10 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Mohammad Rajabifard <mo.rajbi@gmail.com>
OJ Kwon <kwon.ohjoong@gmail.com>
Oliver Joseph Ash <oliverjash@gmail.com>
Orta Therox <orta.therox+gh@gmail.com>
Patrick Housley <patrick.f.housley@gmail.com>
Tom Crockett <tomcrockett@tuplehealth.com>
Umidbek Karimov <uma.karimov@gmail.com>

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": "20.0.9",
"version": "20.0.10",
"main": "index.js",
"types": "./dist/index.d.ts",
"description": "A preprocessor with sourcemap support to help use Typescript with Jest",
Expand Down
10 changes: 1 addition & 9 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export function process(
);
const tsJestConfig = getTSJestConfig(config.globals);

const root = pkgDir.sync();

const isTsFile = path.endsWith('.ts') || path.endsWith('.tsx');
const isJsFile = path.endsWith('.js') || path.endsWith('.jsx');
const isHtmlFile = path.endsWith('.html');
Expand All @@ -49,18 +47,12 @@ export function process(
transformOptions,
);

// strip root part from path
// this results in a shorter filename which will also make the encoded base64 filename for the cache shorter
// long file names could be problematic in some OS
// see https://github.com/kulshekhar/ts-jest/issues/158
path = path.startsWith(root) ? path.substr(root.length) : path;

// store transpiled code contains source map into cache, except test cases
if (!config.testRegex || !path.match(config.testRegex)) {
const outputFilePath = nodepath.join(
config.cacheDirectory,
'/ts-jest/',
new Buffer(path).toString('base64'),
crypto.createHash('md5').update(path).digest('hex'),
);

fs.outputFileSync(outputFilePath, outputText);
Expand Down
12 changes: 12 additions & 0 deletions tests/__tests__/long-path.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import runJest from '../__helpers__/runJest';

describe('Long path', () => {
it('should work as expected', () => {
const result = runJest('../simple-long-path/', [
'--no-cache',
'--coverage',
]);

expect(result.status).toBe(0);
});
});
7 changes: 7 additions & 0 deletions tests/simple-long-path/__tests__/Hello.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Hello } from '../long-src-path/long-src-path/long-src-path/long-src-path/long-src-path/long-src-path/long-src-path/long-src-path/long-src-path/long-src-path/long-src-path/long-src-path/long-src-path/long-src-path/Hello';

describe('Hello test in long path', () => {
it('should work', () => {
expect(new Hello().name).toEqual('John Doe');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Hello {
name: string = 'John Doe';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function nullCoverageFunction(value) {
if (value) {
return value;
}
return null;
}
20 changes: 20 additions & 0 deletions tests/simple-long-path/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"jest": {
"transform": {
".(ts|tsx)": "../../preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"coverageReporters": [
"json"
],
"collectCoverageFrom": [
"long-src-path/**/*.ts",
"long-src-path/**/*.js"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
}
}
10 changes: 10 additions & 0 deletions tests/simple-long-path/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": false,
"jsx": "react",
"allowJs": true
}
}

0 comments on commit a413a04

Please sign in to comment.