File tree 9 files changed +77
-7
lines changed
9 files changed +77
-7
lines changed Original file line number Diff line number Diff line change 2
2
All notable changes to this project will be documented in this file.
3
3
This project adheres to [ Semantic Versioning] ( http://semver.org/ ) .
4
4
5
+ ## [ 0.10.0] - 2016-08-06
6
+ - handle JS imports relative to project directory
7
+
5
8
## [ 0.9.3] - 2016-08-01
6
9
- add "exists" global file path checker
7
10
Original file line number Diff line number Diff line change
1
+ "use strict" ;
2
+ var path_1 = require ( 'path' ) ;
3
+ var importPathRegex = / r e q u i r e \( [ " ' ] ( .+ ) [ " ' ] \) ; ? $ | ^ i m p o r t .+ f r o m \s ? [ " ' ] ( .+ ) [ " ' ] ; ? $ / ;
4
+ var relativePathRegex = / ^ \. / ;
5
+ function fixImportPaths ( str ) {
6
+ return str . split ( '\n' ) . map ( function ( line ) {
7
+ var isMatch = line . match ( importPathRegex ) ;
8
+ if ( ! isMatch ) {
9
+ return line ;
10
+ }
11
+ var importPath = isMatch [ 1 ] || isMatch [ 2 ] ;
12
+ if ( importPath . match ( relativePathRegex ) ) {
13
+ return line . replace ( importPath , path_1 . join ( process . env . DIR , importPath ) ) ;
14
+ }
15
+ return line ;
16
+ } ) . join ( '\n' ) ;
17
+ }
18
+ Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
19
+ exports . default = fixImportPaths ;
Original file line number Diff line number Diff line change @@ -44,6 +44,7 @@ function runnerProcess(config) {
44
44
'--bail' ,
45
45
'--harmony' ,
46
46
'--no-colors' ,
47
+ '--timeout=3000' ,
47
48
( "--reporter=" + path_1 . join ( __dirname , 'reporter.js' ) ) ,
48
49
constants_1 . testPath
49
50
] , options ) ;
Original file line number Diff line number Diff line change @@ -4,11 +4,16 @@ var process_console_log_1 = require('process-console-log');
4
4
var exists_1 = require ( './exists' ) ;
5
5
var constants_1 = require ( './constants' ) ;
6
6
var ts = require ( 'typescript' ) ;
7
+ var import_paths_1 = require ( './import-paths' ) ;
7
8
function writeTest ( config , testString ) {
8
9
return new Promise ( function ( resolve , reject ) {
9
- var output = process_console_log_1 . logger + exists_1 . default ( config . dir ) + ts . transpile ( testString , {
10
+ var testStringWithFixedImportPaths = import_paths_1 . default ( testString ) ;
11
+ var output = ''
12
+ . concat ( process_console_log_1 . logger )
13
+ . concat ( exists_1 . default ( config . dir ) )
14
+ . concat ( ts . transpile ( testStringWithFixedImportPaths , {
10
15
module : ts . ModuleKind . CommonJS
11
- } ) ;
16
+ } ) ) ;
12
17
fs_1 . writeFile ( constants_1 . testPath , output , function ( err ) {
13
18
if ( err ) {
14
19
reject ( err ) ;
Original file line number Diff line number Diff line change 26
26
},
27
27
"homepage" : " https://github.com/coderoad/mocha-coderoad#readme" ,
28
28
"dependencies" : {
29
- "mocha" : " ^3.0.0 " ,
29
+ "mocha" : " ^3.0.1 " ,
30
30
"node-file-exists" : " ^1.1.0" ,
31
31
"process-console-log" : " ^0.2.2" ,
32
32
"typescript" : " ^1.8.10"
Original file line number Diff line number Diff line change
1
+ import { join } from 'path' ;
2
+
3
+ /*
4
+ import paths won't match the context of the test runner
5
+ fixImportPaths will replace paths with absolute paths
6
+ */
7
+
8
+ // import or require statement
9
+ const importPathRegex = / r e q u i r e \( [ " ' ] ( .+ ) [ " ' ] \) ; ? $ | ^ i m p o r t .+ f r o m \s ? [ " ' ] ( .+ ) [ " ' ] ; ? $ / ;
10
+ const relativePathRegex = / ^ \. / ;
11
+
12
+ export default function fixImportPaths ( str : string ) : string {
13
+ return str . split ( '\n' ) . map ( line => {
14
+ const isMatch = line . match ( importPathRegex ) ;
15
+ if ( ! isMatch ) {
16
+ return line ;
17
+ }
18
+ // multiple cases due to import or require regex
19
+ const importPath = isMatch [ 1 ] || isMatch [ 2 ] ;
20
+ // import path: may be relative or absolute
21
+ // // relative path
22
+ if ( importPath . match ( relativePathRegex ) ) {
23
+ // process.env.DIR
24
+ return line . replace ( importPath , join ( process . env . DIR , importPath ) ) ;
25
+ }
26
+ // no match, return line
27
+ return line ;
28
+ } ) . join ( '\n' ) ;
29
+ }
Original file line number Diff line number Diff line change @@ -52,6 +52,7 @@ export default function runnerProcess(config: CR.Config) {
52
52
'--bail' ,
53
53
'--harmony' ,
54
54
'--no-colors' ,
55
+ '--timeout=3000' ,
55
56
`--reporter=${ join ( __dirname , 'reporter.js' ) } ` ,
56
57
testPath
57
58
Original file line number Diff line number Diff line change @@ -3,13 +3,24 @@ import {logger} from 'process-console-log';
3
3
import exists from './exists' ;
4
4
import { testPath } from './constants' ;
5
5
import * as ts from 'typescript' ;
6
+ import importPaths from './import-paths' ;
6
7
7
8
export default function writeTest ( config , testString : string ) {
8
9
return new Promise ( ( resolve , reject ) => {
9
- // append logger, compile using ts compiler
10
- const output = logger + exists ( config . dir ) + ts . transpile ( testString , {
11
- module : ts . ModuleKind . CommonJS
12
- } ) ;
10
+ // fix import paths relative to project dir instead of test runner
11
+ const testStringWithFixedImportPaths = importPaths ( testString ) ;
12
+
13
+ const output = ''
14
+ // append logger
15
+ . concat ( logger )
16
+ // exists polyfill for file/folder exists checks
17
+ . concat ( exists ( config . dir ) )
18
+ // compile using ts
19
+ . concat (
20
+ ts . transpile ( testStringWithFixedImportPaths , {
21
+ module : ts . ModuleKind . CommonJS
22
+ } )
23
+ ) ;
13
24
// write test file
14
25
writeFile ( testPath , output , ( err ) => {
15
26
if ( err ) { reject ( err ) ; }
Original file line number Diff line number Diff line change 17
17
"files" : [
18
18
" src/constants.ts" ,
19
19
" src/exists.ts" ,
20
+ " src/import-paths.ts" ,
20
21
" src/index.ts" ,
21
22
" src/reporter.ts" ,
22
23
" src/runner-process.ts" ,
You can’t perform that action at this time.
0 commit comments