From a26cba657ef7c85887d386d78867ccd0ef638c94 Mon Sep 17 00:00:00 2001 From: Ihor Chulinda Date: Thu, 3 Nov 2016 17:32:51 +0100 Subject: [PATCH 1/7] Revert "Removing handlers for source-map-support." --- src/default-retrieve-file-handler.ts | 23 +++++++++++++++++++++++ src/default-retrieve-map-handler.ts | 20 ++++++++++++++++++++ src/index.ts | 4 ++++ src/retrieve-sourceMap-url.ts | 14 ++++++++++++++ src/transpile-if-ts.ts | 21 +++++++++++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 src/default-retrieve-file-handler.ts create mode 100644 src/default-retrieve-map-handler.ts create mode 100644 src/retrieve-sourceMap-url.ts create mode 100644 src/transpile-if-ts.ts diff --git a/src/default-retrieve-file-handler.ts b/src/default-retrieve-file-handler.ts new file mode 100644 index 0000000000..af3b5f44b3 --- /dev/null +++ b/src/default-retrieve-file-handler.ts @@ -0,0 +1,23 @@ +import * as fs from 'fs'; +import { transpileIfTypescript } from './transpile-if-ts'; + +export function defaultRetrieveFileHandler(path) { + // Trim the path to make sure there is no extra whitespace. + path = path.trim(); + + // This was removed because it seems that we can't use cache while expecting correct results + // TODO: check correctness and performance with file caching + // if (path in fileContentsCache) { + // return fileContentsCache[path]; + // } + + var contents: string; + try { + contents = fs.readFileSync(path, 'utf8'); + contents = transpileIfTypescript(path, contents); + } catch (e) { + contents = null; + } + + return contents; +} \ No newline at end of file diff --git a/src/default-retrieve-map-handler.ts b/src/default-retrieve-map-handler.ts new file mode 100644 index 0000000000..cb5fcb25ad --- /dev/null +++ b/src/default-retrieve-map-handler.ts @@ -0,0 +1,20 @@ +import { retrieveSourceMapURL } from './retrieve-sourceMap-url'; + +export function defaultRetrieveMapHandler(source) { + var sourceMappingURL = retrieveSourceMapURL(source); + if (!sourceMappingURL) return null; + var startOfSourceMap = sourceMappingURL.indexOf(',') + 1; + /// Check that there is source map + if (startOfSourceMap === 0) return null; + // Reading source map URL as a data url, because it is always inlined + var rawData = sourceMappingURL.slice(startOfSourceMap); + var sourceMapData = new Buffer(rawData, 'base64').toString(); + sourceMappingURL = null; //TODO: why `null` instead of `source` as in original sourceMaphandler? + + if (!sourceMapData) return null; + + return { + url: sourceMappingURL, + map: sourceMapData + }; +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 755dc771ec..6333eca34d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,11 @@ +import { defaultRetrieveMapHandler } from './default-retrieve-map-handler'; +import { defaultRetrieveFileHandler } from './default-retrieve-file-handler'; import * as sourceMapSupport from 'source-map-support'; export function install() { var options: sourceMapSupport.Options = {}; + options.retrieveFile = defaultRetrieveFileHandler; + options.retrieveSourceMap = defaultRetrieveMapHandler; options.emptyCacheBetweenOperations = true; // left here only for sourceMapCache TODO: check this for correctness and performance with false velue options['environment'] = 'node'; diff --git a/src/retrieve-sourceMap-url.ts b/src/retrieve-sourceMap-url.ts new file mode 100644 index 0000000000..3ac7fed559 --- /dev/null +++ b/src/retrieve-sourceMap-url.ts @@ -0,0 +1,14 @@ +import { defaultRetrieveFileHandler } from './default-retrieve-file-handler'; + +export function retrieveSourceMapURL(source) { + // Get the URL of the source map + var fileData = defaultRetrieveFileHandler(source); + // //# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */ + var re = /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/)[ \t]*$)/mg; + // Keep executing the search to find the *last* sourceMappingURL to avoid + // picking up sourceMappingURLs from comments, strings, etc. + var lastMatch, match; + while (match = re.exec(fileData)) lastMatch = match; + if (!lastMatch) return null; + return lastMatch[1]; +}; \ No newline at end of file diff --git a/src/transpile-if-ts.ts b/src/transpile-if-ts.ts new file mode 100644 index 0000000000..281c97d846 --- /dev/null +++ b/src/transpile-if-ts.ts @@ -0,0 +1,21 @@ +import * as tsc from 'typescript'; +import { getTSConfig } from './utils'; + +export function transpileIfTypescript(path, contents) { + if (path && (path.endsWith('.tsx') || path.endsWith('.ts'))) { + + let transpiled = tsc.transpileModule(contents, { + compilerOptions: addSourceMapToTSConfig(), + fileName: path + }); + + return transpiled.outputText; + } + return contents; +} + +function addSourceMapToTSConfig() { + // if a global __TS_CONFIG__ is set, update the compiler setting to include inline SourceMap + var config = getTSConfig({ __TS_CONFIG__: global['__TS_CONFIG__'] }, true); + return config; +} \ No newline at end of file From d10f342fdb96248262fb31c40969a5bf1a6a4741 Mon Sep 17 00:00:00 2001 From: Igor Chulinda Date: Fri, 4 Nov 2016 17:04:05 +0100 Subject: [PATCH 2/7] Fix for #45 and integration tests --- .gitignore | 3 +++ .npmignore | 3 +++ src/default-retrieve-map-handler.ts | 20 -------------------- src/index.ts | 2 -- src/retrieve-sourceMap-url.ts | 14 -------------- tests/button/__mocks__/ts-jest.ts | 2 -- tests/button/node_modules/ts-jest.js | 1 + tests/simple/__mocks__/ts-jest.ts | 2 -- tests/simple/node_modules/ts-jest.js | 1 + tests/tsconfig-test/__mocks__/ts-jest.ts | 2 -- tests/watch-test/__mocks__/ts-jest.ts | 2 -- tests/watch-test/node_modules/ts-jest.js | 1 + 12 files changed, 9 insertions(+), 44 deletions(-) delete mode 100644 src/default-retrieve-map-handler.ts delete mode 100644 src/retrieve-sourceMap-url.ts delete mode 100644 tests/button/__mocks__/ts-jest.ts create mode 100644 tests/button/node_modules/ts-jest.js delete mode 100644 tests/simple/__mocks__/ts-jest.ts create mode 100644 tests/simple/node_modules/ts-jest.js delete mode 100644 tests/tsconfig-test/__mocks__/ts-jest.ts delete mode 100644 tests/watch-test/__mocks__/ts-jest.ts create mode 100644 tests/watch-test/node_modules/ts-jest.js diff --git a/.gitignore b/.gitignore index 82539a8b8d..b1dc0a4c62 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,6 @@ jspm_packages # Optional REPL history .node_repl_history + +# We need to include this folders, because they are mocks for integration tests +!tests/**/node_modules diff --git a/.npmignore b/.npmignore index 3b6328d017..fd050273ec 100644 --- a/.npmignore +++ b/.npmignore @@ -17,6 +17,9 @@ pids *.pid *.seed +# nodist config for testing in different versions of node +.node-version + # Directory for instrumented libs generated by jscoverage/JSCover lib-cov diff --git a/src/default-retrieve-map-handler.ts b/src/default-retrieve-map-handler.ts deleted file mode 100644 index cb5fcb25ad..0000000000 --- a/src/default-retrieve-map-handler.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { retrieveSourceMapURL } from './retrieve-sourceMap-url'; - -export function defaultRetrieveMapHandler(source) { - var sourceMappingURL = retrieveSourceMapURL(source); - if (!sourceMappingURL) return null; - var startOfSourceMap = sourceMappingURL.indexOf(',') + 1; - /// Check that there is source map - if (startOfSourceMap === 0) return null; - // Reading source map URL as a data url, because it is always inlined - var rawData = sourceMappingURL.slice(startOfSourceMap); - var sourceMapData = new Buffer(rawData, 'base64').toString(); - sourceMappingURL = null; //TODO: why `null` instead of `source` as in original sourceMaphandler? - - if (!sourceMapData) return null; - - return { - url: sourceMappingURL, - map: sourceMapData - }; -} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 6333eca34d..1f8bc46ab0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,9 @@ -import { defaultRetrieveMapHandler } from './default-retrieve-map-handler'; import { defaultRetrieveFileHandler } from './default-retrieve-file-handler'; import * as sourceMapSupport from 'source-map-support'; export function install() { var options: sourceMapSupport.Options = {}; options.retrieveFile = defaultRetrieveFileHandler; - options.retrieveSourceMap = defaultRetrieveMapHandler; options.emptyCacheBetweenOperations = true; // left here only for sourceMapCache TODO: check this for correctness and performance with false velue options['environment'] = 'node'; diff --git a/src/retrieve-sourceMap-url.ts b/src/retrieve-sourceMap-url.ts deleted file mode 100644 index 3ac7fed559..0000000000 --- a/src/retrieve-sourceMap-url.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { defaultRetrieveFileHandler } from './default-retrieve-file-handler'; - -export function retrieveSourceMapURL(source) { - // Get the URL of the source map - var fileData = defaultRetrieveFileHandler(source); - // //# sourceMappingURL=foo.js.map /*# sourceMappingURL=foo.js.map */ - var re = /(?:\/\/[@#][ \t]+sourceMappingURL=([^\s'"]+?)[ \t]*$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^\*]+?)[ \t]*(?:\*\/)[ \t]*$)/mg; - // Keep executing the search to find the *last* sourceMappingURL to avoid - // picking up sourceMappingURLs from comments, strings, etc. - var lastMatch, match; - while (match = re.exec(fileData)) lastMatch = match; - if (!lastMatch) return null; - return lastMatch[1]; -}; \ No newline at end of file diff --git a/tests/button/__mocks__/ts-jest.ts b/tests/button/__mocks__/ts-jest.ts deleted file mode 100644 index d8807e2a35..0000000000 --- a/tests/button/__mocks__/ts-jest.ts +++ /dev/null @@ -1,2 +0,0 @@ -// we have to mock ts-jest package here, because we don't want create extra node_modules folders -module.exports = require('../../../'); \ No newline at end of file diff --git a/tests/button/node_modules/ts-jest.js b/tests/button/node_modules/ts-jest.js new file mode 100644 index 0000000000..6bb2ded8c1 --- /dev/null +++ b/tests/button/node_modules/ts-jest.js @@ -0,0 +1 @@ +module.exports = require('../../../'); \ No newline at end of file diff --git a/tests/simple/__mocks__/ts-jest.ts b/tests/simple/__mocks__/ts-jest.ts deleted file mode 100644 index d8807e2a35..0000000000 --- a/tests/simple/__mocks__/ts-jest.ts +++ /dev/null @@ -1,2 +0,0 @@ -// we have to mock ts-jest package here, because we don't want create extra node_modules folders -module.exports = require('../../../'); \ No newline at end of file diff --git a/tests/simple/node_modules/ts-jest.js b/tests/simple/node_modules/ts-jest.js new file mode 100644 index 0000000000..6bb2ded8c1 --- /dev/null +++ b/tests/simple/node_modules/ts-jest.js @@ -0,0 +1 @@ +module.exports = require('../../../'); \ No newline at end of file diff --git a/tests/tsconfig-test/__mocks__/ts-jest.ts b/tests/tsconfig-test/__mocks__/ts-jest.ts deleted file mode 100644 index d8807e2a35..0000000000 --- a/tests/tsconfig-test/__mocks__/ts-jest.ts +++ /dev/null @@ -1,2 +0,0 @@ -// we have to mock ts-jest package here, because we don't want create extra node_modules folders -module.exports = require('../../../'); \ No newline at end of file diff --git a/tests/watch-test/__mocks__/ts-jest.ts b/tests/watch-test/__mocks__/ts-jest.ts deleted file mode 100644 index d8807e2a35..0000000000 --- a/tests/watch-test/__mocks__/ts-jest.ts +++ /dev/null @@ -1,2 +0,0 @@ -// we have to mock ts-jest package here, because we don't want create extra node_modules folders -module.exports = require('../../../'); \ No newline at end of file diff --git a/tests/watch-test/node_modules/ts-jest.js b/tests/watch-test/node_modules/ts-jest.js new file mode 100644 index 0000000000..6bb2ded8c1 --- /dev/null +++ b/tests/watch-test/node_modules/ts-jest.js @@ -0,0 +1 @@ +module.exports = require('../../../'); \ No newline at end of file From 77b44c851b635a783728da60d47334fd55ff4cf2 Mon Sep 17 00:00:00 2001 From: Igor Chulinda Date: Fri, 4 Nov 2016 17:06:30 +0100 Subject: [PATCH 3/7] Small refactor. --- src/transpile-if-ts.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/transpile-if-ts.ts b/src/transpile-if-ts.ts index 281c97d846..2c9c596ef0 100644 --- a/src/transpile-if-ts.ts +++ b/src/transpile-if-ts.ts @@ -5,17 +5,11 @@ export function transpileIfTypescript(path, contents) { if (path && (path.endsWith('.tsx') || path.endsWith('.ts'))) { let transpiled = tsc.transpileModule(contents, { - compilerOptions: addSourceMapToTSConfig(), + compilerOptions: getTSConfig({ __TS_CONFIG__: global['__TS_CONFIG__'] }, true), fileName: path }); return transpiled.outputText; } return contents; -} - -function addSourceMapToTSConfig() { - // if a global __TS_CONFIG__ is set, update the compiler setting to include inline SourceMap - var config = getTSConfig({ __TS_CONFIG__: global['__TS_CONFIG__'] }, true); - return config; } \ No newline at end of file From e1f95e524ed62091736f70abf63530f1f107ec03 Mon Sep 17 00:00:00 2001 From: Igor Chulinda Date: Fri, 4 Nov 2016 17:20:15 +0100 Subject: [PATCH 4/7] Adding appveyor CI --- README.md | 1 + appveyor.yml | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 appveyor.yml diff --git a/README.md b/README.md index 8bfc9a2798..a84bb29beb 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![Build Status for node v7](https://travis-badges.herokuapp.com/repos/kulshekhar/ts-jest/branches/master?job=0)](https://travis-ci.org/kulshekhar/ts-jest) [![Build Status for node v6](https://travis-badges.herokuapp.com/repos/kulshekhar/ts-jest/branches/master?job=1)](https://travis-ci.org/kulshekhar/ts-jest) [![Build Status for node v4](https://travis-badges.herokuapp.com/repos/kulshekhar/ts-jest/branches/master?job=2)](https://travis-ci.org/kulshekhar/ts-jest) +[![Build Status for Windows](https://ci.appveyor.com/api/projects/status/gknb1pl72o0w0coc?svg=true)](https://ci.appveyor.com/project/Igmat/ts-jest) ## Table of Contents diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000000..3bf2764e53 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,28 @@ +# appveyor file +# http://www.appveyor.com/docs/appveyor-yml + +# build version format +version: "{build}" + +# fix lineendings in Windows +init: + - git config --global core.autocrlf input + +# what combinations to test +environment: + matrix: + - nodejs_version: 4 + - nodejs_version: 6 + - nodejs_version: 7 + +# get the latest stable version of Node 0.STABLE.latest +install: + - ps: Install-Product node $env:nodejs_version + - npm install + +build: off + +test_script: + - node --version + - npm --version + - cmd: npm test --no-color \ No newline at end of file From 9cb9244d00d72544b874f7019aab1e5ed39bc0b2 Mon Sep 17 00:00:00 2001 From: Igor Chulinda Date: Fri, 4 Nov 2016 17:38:45 +0100 Subject: [PATCH 5/7] Appveyor fix. --- appveyor.yml | 1 + tests/__tests__/watch.spec.ts | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/appveyor.yml b/appveyor.yml index 3bf2764e53..2e6b9d6b59 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -18,6 +18,7 @@ environment: # get the latest stable version of Node 0.STABLE.latest install: - ps: Install-Product node $env:nodejs_version + - npm i -g npm@latest - npm install build: off diff --git a/tests/__tests__/watch.spec.ts b/tests/__tests__/watch.spec.ts index d74919f726..b6d4ada4da 100644 --- a/tests/__tests__/watch.spec.ts +++ b/tests/__tests__/watch.spec.ts @@ -48,9 +48,12 @@ describe('Hello Class', () => { describe('hello_world', () => { let result: { childProcess: ChildProcess, getStderrAsync: () => Promise }; + let DEFAULT_TIMEOUT_INTERVAL: number; beforeAll(() => { result = runJestInWatchMode('../watch-test'); + DEFAULT_TIMEOUT_INTERVAL = jasmine['DEFAULT_TIMEOUT_INTERVAL']; + jasmine['DEFAULT_TIMEOUT_INTERVAL'] = 10000; }); it('should show the correct error locations in the typescript files without changes', () => { @@ -81,6 +84,7 @@ describe('hello_world', () => { afterAll(() => { result.childProcess.kill(); // revert changes back + jasmine['DEFAULT_TIMEOUT_INTERVAL'] = DEFAULT_TIMEOUT_INTERVAL; fs.writeFileSync(path.resolve(__dirname, '../watch-test/Hello.ts'), helloFile); fs.writeFileSync(path.resolve(__dirname, '../watch-test/__tests__/Hello.test.ts'), testFile); }); From 4f1b7a16d417cf86685dd103884df677ee9fa6ae Mon Sep 17 00:00:00 2001 From: Igor Chulinda Date: Fri, 4 Nov 2016 18:18:51 +0100 Subject: [PATCH 6/7] One more fix for appveyor. --- appveyor.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/appveyor.yml b/appveyor.yml index 2e6b9d6b59..a3d68361a8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -18,7 +18,9 @@ environment: # get the latest stable version of Node 0.STABLE.latest install: - ps: Install-Product node $env:nodejs_version + - set CI=true - npm i -g npm@latest + - set PATH=%APPDATA%\npm;%PATH% - npm install build: off From 3d48dca3633b80f5480224191db6547ca6b00277 Mon Sep 17 00:00:00 2001 From: Igor Chulinda Date: Mon, 7 Nov 2016 16:01:13 +0100 Subject: [PATCH 7/7] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 033ca8cb65..e7a9c48012 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-jest", - "version": "0.1.12", + "version": "0.1.13", "main": "index.js", "types": "./dist/index.d.ts", "description": "A preprocessor with sourcemap support to help use Typescript with Jest",