Skip to content

Commit e2aa2c0

Browse files
committed
use custom rewire over require
1 parent af4897f commit e2aa2c0

File tree

5 files changed

+18
-21
lines changed

5 files changed

+18
-21
lines changed

lib/writeTests/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
function helpers(projectDir) {
3-
return "\n // run time compiler\n require('babel-core/register')({plugins: [['transform-es2015-modules-commonjs',{\n loose:true}]]});\n\n // fileExists test helper\n function fileExists(e,r){void 0===r&&(r=!0);try{accessSync(e,F_OK)}catch(c){if(c)return r||console.log(c),!1}return!0}var _require=require(\"fs\"),accessSync=_require.accessSync,F_OK=_require.F_OK,_require2=require(\"path\"),resolve=_require2.resolve;function exists(p){return fileExists(resolve('" + projectDir + "',p))}\n\n // overwrite require to catch globals in tests\n require = require('rewire');\n ";
3+
return "\n // run time compiler\n require('babel-core/register')({plugins: [['transform-es2015-modules-commonjs',{\n loose:true}]]});\n\n // fileExists test helper\n function fileExists(e,r){void 0===r&&(r=!0);try{accessSync(e,F_OK)}catch(c){if(c)return r||console.log(c),!1}return!0}var _require=require(\"fs\"),accessSync=_require.accessSync,F_OK=_require.F_OK,_require2=require(\"path\"),resolve=_require2.resolve;function exists(p){return fileExists(resolve('" + projectDir + "',p))}\n\n // overwrite require to catch globals in tests\n require = require('rewire');\n\n";
44
}
55
Object.defineProperty(exports, "__esModule", { value: true });
66
exports.default = helpers;

lib/writeTests/import-paths.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ var importPathRegex = /require\(["'](BASE.+)["']\);?$|^import.+?\s?["'](BASE.+)[
44
var relativePathRegex = /^BASE/;
55
function fixImportPaths(dir, str) {
66
var entries = new Set([]);
7-
var arr = str.split('\n').map(function (line) {
7+
return str.split('\n').map(function (line) {
88
var isMatch = line.match(importPathRegex);
99
if (!isMatch) {
1010
return line;
1111
}
1212
var importPath = isMatch[1] || isMatch[2];
1313
if (importPath.match(relativePathRegex)) {
1414
var newPath = path_1.join(dir, importPath.replace('BASE', ''));
15-
entries.add(line.replace(importPath, newPath));
15+
var newLine = line.replace(importPath, newPath);
16+
if (!entries.has(newLine)) {
17+
entries.add(newLine);
18+
return newLine;
19+
}
1620
return '';
1721
}
1822
return line;
19-
});
20-
return (Array.from(entries.keys())
21-
.concat(arr)
22-
.join('\n'));
23+
}).join('\n');
2324
}
2425
Object.defineProperty(exports, "__esModule", { value: true });
2526
exports.default = fixImportPaths;

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
},
3636
"devDependencies": {
3737
"chai": "^3.5.0",
38-
"chai-as-promised": "^5.3.0",
3938
"chai-spies": "^0.7.1"
4039
}
4140
}

src/writeTests/helpers.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ export default function helpers(projectDir) {
99
1010
// overwrite require to catch globals in tests
1111
require = require('rewire');
12-
`;
12+
13+
`;
1314
}

src/writeTests/import-paths.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function fixImportPaths(dir: string, str: string): string {
1313
// collect import lines
1414
let entries = new Set([]);
1515

16-
let arr = str.split('\n').map(line => {
16+
return str.split('\n').map(line => {
1717
// line has an import or require ?
1818
const isMatch = line.match(importPathRegex);
1919
if (!isMatch) {
@@ -23,22 +23,18 @@ export default function fixImportPaths(dir: string, str: string): string {
2323
const importPath = isMatch[1] || isMatch[2];
2424
// import path: may be relative or absolute
2525

26-
// relative path
26+
// is a relative path
2727
if (importPath.match(relativePathRegex)) {
28-
2928
let newPath = join(dir, importPath.replace('BASE', ''));
30-
29+
let newLine = line.replace(importPath, newPath);
3130
// add to map of entry files
32-
entries.add(line.replace(importPath, newPath));
31+
if (!entries.has(newLine)) {
32+
entries.add(newLine);
33+
return newLine;
34+
}
3335
return '';
3436
}
3537
// no match, return line
3638
return line;
37-
});
38-
// prepend import paths to output
39-
return (
40-
Array.from(entries.keys())
41-
.concat(arr)
42-
.join('\n')
43-
);
39+
}).join('\n');
4440
}

0 commit comments

Comments
 (0)