Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7cc0896

Browse files
committed
add __text__ for easy access to a file as text
1 parent a750521 commit 7cc0896

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
---------
33

4+
## 3.1.0
5+
- remove "use strict" checking, opt always for use strict
6+
- add `__text__` property, allowing easy access to the text output of a file
7+
48
### 3.0.0
59
- Capture imports and add them to the global scope for testing
610

lib/addImportsAsVars.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function detectImports(src) {
4141
if (importType === 'named') {
4242
output += `global.${key} = require('${path}').${key};`;
4343
} else {
44-
output += `global.${key} = require('${path}').default`;
44+
output += `global.${key} = require('${path}')`;
4545
}
4646

4747
} catch(e) {}

lib/getDefinePropertySrc.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
'use strict';
22

3-
const __get__ = require("./__get__.js");
3+
const __get__ = require('./__get__');
44

5-
const srcs = {
6-
"__get__": __get__.toString(),
7-
};
5+
function getDefinePropertySrc(src) {
6+
7+
const getMethods = src => ({
8+
'__get__': __get__.toString(),
9+
'__text__': '`' + src.toString() + "`",
10+
});
11+
12+
const methods = getMethods(src);
813

9-
function getDefinePropertySrc() {
1014
return `
1115
if (typeof(module.exports) === 'function' ||
1216
(typeof(module.exports) === 'object' && module.exports !== null && Object.isExtensible(module.exports))) {
13-
${Object.keys(srcs).reduce((preValue, value) => {
14-
return preValue += `Object.defineProperty(module.exports, '${value}', {enumerable: false, value: ${srcs[value]}, writable: true});`;
17+
${Object.keys(methods).reduce((preValue, value) => {
18+
return preValue += `Object.defineProperty(module.exports, '${value}', {enumerable: false, value: ${methods[value]}, writable: true});`;
1519
}, '')
1620
}
1721
}`;

lib/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ function rewire(filename) {
1515
// create a __get__ mock to prevent tests breaking
1616
if (!fileExists(filename)) {
1717
return {
18-
__get__: () => {}
18+
__get__: () => {},
19+
__text__: undefined,
1920
};
2021
}
2122
}

lib/rewire.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function internalRewire(parentModulePath, targetPath) {
4545
prelude += '(function () { ';
4646

4747
// We append our special setter and getter.
48-
appendix = '\n' + getDefinePropertySrc();
48+
appendix = '\n' + getDefinePropertySrc(src);
4949

5050
appendix += '\n' + addImportsAsVars(src);
5151

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rewire-coderoad",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"description": "CodeRoad implementation to override 'require' in order to monkey-patch test globals",
55
"keywords": [
66
"dependency",

0 commit comments

Comments
 (0)