diff --git a/.gitignore b/.gitignore index b1dc0a4c62..79eb29c2b1 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/src/preprocessor.ts b/src/preprocessor.ts index b14a126f45..0146a23990 100644 --- a/src/preprocessor.ts +++ b/src/preprocessor.ts @@ -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; } \ No newline at end of file diff --git a/tests/__tests__/use-strict.spec.ts b/tests/__tests__/use-strict.spec.ts new file mode 100644 index 0000000000..c32a743be5 --- /dev/null +++ b/tests/__tests__/use-strict.spec.ts @@ -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); + + }); + +}); \ No newline at end of file diff --git a/tests/use-strict/Strict-valid.ts b/tests/use-strict/Strict-valid.ts new file mode 100644 index 0000000000..4208b5d16d --- /dev/null +++ b/tests/use-strict/Strict-valid.ts @@ -0,0 +1,5 @@ +'use strict'; + +export function checkStrictValid() { + var v = 33; +} diff --git a/tests/use-strict/Strict.ts b/tests/use-strict/Strict.ts new file mode 100644 index 0000000000..8244ea9f03 --- /dev/null +++ b/tests/use-strict/Strict.ts @@ -0,0 +1,5 @@ +'use strict'; + +export function checkStrict() { + v = 33; +}; diff --git a/tests/use-strict/__tests__/Strict-valid.test.ts b/tests/use-strict/__tests__/Strict-valid.test.ts new file mode 100644 index 0000000000..c2e48250a0 --- /dev/null +++ b/tests/use-strict/__tests__/Strict-valid.test.ts @@ -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(); + + }); + +}); \ No newline at end of file diff --git a/tests/use-strict/__tests__/Strict.test.ts b/tests/use-strict/__tests__/Strict.test.ts new file mode 100644 index 0000000000..aa9349c8ac --- /dev/null +++ b/tests/use-strict/__tests__/Strict.test.ts @@ -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(); + + }); + +}); \ No newline at end of file diff --git a/tests/use-strict/package.json b/tests/use-strict/package.json new file mode 100644 index 0000000000..5d69a11426 --- /dev/null +++ b/tests/use-strict/package.json @@ -0,0 +1,14 @@ +{ + "jest": { + "transform": { + ".(ts|tsx)": "../../preprocessor.js" + }, + "testResultsProcessor": "../../coverageprocessor.js", + "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$", + "moduleFileExtensions": [ + "ts", + "tsx", + "js" + ] + } +} \ No newline at end of file diff --git a/tests/use-strict/tsconfig.json b/tests/use-strict/tsconfig.json new file mode 100644 index 0000000000..5e2c68f9ff --- /dev/null +++ b/tests/use-strict/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "ES5", + "module": "commonjs", + "moduleResolution": "node", + "noEmitOnError": false, + "jsx": "react", + "allowJs": true + } +} \ No newline at end of file