Skip to content

Commit

Permalink
Merge pull request #71 from kulshekhar/allow-use-strict
Browse files Browse the repository at this point in the history
Handle 'use strict'
  • Loading branch information
kulshekhar authored Dec 1, 2016
2 parents 47267d2 + 370b962 commit 22b5035
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 33 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ jspm_packages

# We need to include this folders, because they are mocks for integration tests
!tests/**/node_modules

.vscode
.idea
**/node_modules
71 changes: 38 additions & 33 deletions src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,51 @@ declare const global: any;

const root = getPackageRoot();
const {
testRegex,
collectCoverage,
coverageDirectory,
coverageReporters,
collectCoverageFrom,
testResultsProcessor
testRegex,
collectCoverage,
coverageDirectory,
coverageReporters,
collectCoverageFrom,
testResultsProcessor
} = getJestConfig(root);

//setting up cache to global object to resultprocessor consume
if (testResultsProcessor) {
global.__ts_coverage__cache__ = {};
global.__ts_coverage__cache__.sourceCache = {};
global.__ts_coverage__cache__.coverageConfig = { collectCoverage, coverageDirectory, coverageReporters };
global.__ts_coverage__cache__.coverageCollectFiles =
collectCoverage &&
testResultsProcessor &&
collectCoverageFrom &&
collectCoverageFrom.length ?
glob.sync(collectCoverageFrom).map(x => nodepath.resolve(root, x)) : [];
global.__ts_coverage__cache__ = {};
global.__ts_coverage__cache__.sourceCache = {};
global.__ts_coverage__cache__.coverageConfig = { collectCoverage, coverageDirectory, coverageReporters };
global.__ts_coverage__cache__.coverageCollectFiles =
collectCoverage &&
testResultsProcessor &&
collectCoverageFrom &&
collectCoverageFrom.length ?
glob.sync(collectCoverageFrom).map(x => nodepath.resolve(root, x)) : [];
}

export function process(src, path, config) {
if (path.endsWith('.ts') || path.endsWith('.tsx')) {
const transpiled = tsc.transpileModule(
src,
{
compilerOptions: getTSConfig(config.globals, collectCoverage),
fileName: path
});

//store transpiled code contains source map into cache, except test cases
if (global.__ts_coverage__cache__) {
if (!testRegex || !path.match(testRegex)) {
global.__ts_coverage__cache__.sourceCache[path] = transpiled.outputText;
}
}
if (path.endsWith('.ts') || path.endsWith('.tsx')) {
const transpiled = tsc.transpileModule(
src,
{
compilerOptions: getTSConfig(config.globals, collectCoverage),
fileName: path
});

//store transpiled code contains source map into cache, except test cases
if (global.__ts_coverage__cache__) {
if (!testRegex || !path.match(testRegex)) {
global.__ts_coverage__cache__.sourceCache[path] = transpiled.outputText;
}
}

const start = transpiled.outputText.length > 12 ? transpiled.outputText.substr(1, 10) : '';

const modified = `require('ts-jest').install();${transpiled.outputText}`;
return modified;
}
const modified = start === 'use strict'
? `'use strict';require('ts-jest').install();${transpiled.outputText}`
: `require('ts-jest').install();${transpiled.outputText}`;

return modified;
}

return src;
return src;
}
27 changes: 27 additions & 0 deletions tests/__tests__/use-strict.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { } from 'jest';
import { } from 'node';
import runJest from '../__helpers__/runJest';

describe('use strict', () => {

it('should show the error locations for "use strict" violations', () => {

const result = runJest('../use-strict', ['--no-cache', '-t', 'Invalid Strict']);

const stderr = result.stderr.toString();

expect(result.status).toBe(1);
expect(stderr).toContain('Strict.ts:4:4');
expect(stderr).toContain('Strict.test.ts:9:5');

});

it('should work with "use strict"', () => {

const result = runJest('../use-strict', ['--no-cache', '-t', 'Valid Strict']);

expect(result.status).toBe(0);

});

});
5 changes: 5 additions & 0 deletions tests/use-strict/Strict-valid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

export function checkStrictValid() {
var v = 33;
}
5 changes: 5 additions & 0 deletions tests/use-strict/Strict.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

export function checkStrict() {
v = 33;
};
13 changes: 13 additions & 0 deletions tests/use-strict/__tests__/Strict-valid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
declare var jest, describe, it, expect;

import { checkStrictValid } from '../Strict-valid';

describe('Valid Strict', () => {

it('should not throw an error', () => {

checkStrictValid();

});

});
13 changes: 13 additions & 0 deletions tests/use-strict/__tests__/Strict.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
declare var jest, describe, it, expect;

import { checkStrict } from '../Strict';

describe('Invalid Strict', () => {

it('should throw an error on line 9', () => {

checkStrict();

});

});
14 changes: 14 additions & 0 deletions tests/use-strict/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"jest": {
"transform": {
".(ts|tsx)": "../../preprocessor.js"
},
"testResultsProcessor": "../../coverageprocessor.js",
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
}
}
10 changes: 10 additions & 0 deletions tests/use-strict/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 22b5035

Please sign in to comment.