Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle 'use strict' #71

Merged
merged 1 commit into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bad idea. We need that folder in each integration test case, because without it, you'll always test version from root node_modules.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix that right away

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']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does -t flag stands for? I haven't found any doc on that, and jest --help haven't shown nothing also.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It runs a test that matches the name specified.

Running jest --help showed this part:

--testNamePattern, -t           Run only tests with a name that matches the
                                  regex pattern.                        [string]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really. My bad - somehow missed it.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found out about it today myself :)


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
}
}