forked from webpack/webpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_defineTest.js.html
More file actions
168 lines (145 loc) · 8.39 KB
/
Copy pathutils_defineTest.js.html
File metadata and controls
168 lines (145 loc) · 8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: utils/defineTest.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: utils/defineTest.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>"use strict";
const fs = require("fs");
const path = require("path");
/**
* Utility function to run a jscodeshift script within a unit test.
* This makes several assumptions about the environment.
*
* Notes:
* - The test should be located in a subdirectory next to the transform itself.
* Commonly tests are located in a directory called __tests__.
*
* - Test data should be located in a directory called __testfixtures__
* alongside the transform and __tests__ directory.
* @param {String} dirName contains the name of the directory the test is located in. This
* should normally be passed via __dirname.
* @param {String} transformName contains the filename of the transform being tested,
* excluding the .js extension.
* @param {String} [testFilePrefix] Optionally contains the name of the file with the test
* data. If not specified, it defaults to the same value as `transformName`.
* This will be suffixed with ".input.js" for the input file and ".output.js"
* for the expected output. For example, if set to "foo", we will read the
* "foo.input.js" file, pass this to the transform, and expect its output to
* be equal to the contents of "foo.output.js".
* @param {Object|Boolean|String} initOptions TBD
* @param {String} action init, update or remove, decides how to format the AST
* @return {Function} Function that fires of the transforms
*/
function runSingleTransform(
dirName,
transformName,
testFilePrefix,
initOptions,
action
) {
if (!testFilePrefix) {
testFilePrefix = transformName;
}
const fixtureDir = path.join(dirName, "__testfixtures__");
const inputPath = path.join(fixtureDir, testFilePrefix + ".input.js");
const source = fs.readFileSync(inputPath, "utf8");
let module;
// Assumes transform and test are on the same level
if (action) {
module = require(path.join(dirName, "index.js"));
} else {
module = require(path.join(dirName, transformName + ".js"));
}
// Handle ES6 modules using default export for the transform
const transform = module.default ? module.default : module;
// Jest resets the module registry after each test, so we need to always get
// a fresh copy of jscodeshift on every test run.
let jscodeshift = require("jscodeshift/dist/core");
if (module.parser) {
jscodeshift = jscodeshift.withParser(module.parser);
}
const ast = jscodeshift(source);
if (initOptions || typeof initOptions === "boolean") {
return transform(
jscodeshift,
ast,
initOptions,
action,
transformName
).toSource({
quote: "single"
});
}
return transform(jscodeshift, ast, source, action).toSource({
quote: "single"
});
}
/**
* Handles some boilerplate around defining a simple jest/Jasmine test for a
* jscodeshift transform.
* @param {String} dirName contains the name of the directory the test is located in. This
* should normally be passed via __dirname.
* @param {String} transformName contains the filename of the transform being tested,
* excluding the .js extension.
* @param {String} [testFilePrefix] Optionally contains the name of the file with the test
* data. If not specified, it defaults to the same value as `transformName`.
* This will be suffixed with ".input.js" for the input file and ".output.js"
* for the expected output. For example, if set to "foo", we will read the
* "foo.input.js" file, pass this to the transform, and expect its output to
* be equal to the contents of "foo.output.js".
* @param {any} transformObject Object to be transformed with the transformations
* @param {String} action init, update or remove, decides how to format the AST
* @return {Void} Jest makes sure to execute the globally defined functions
*/
function defineTest(
dirName,
transformName,
testFilePrefix,
transformObject,
action
) {
const testName = testFilePrefix
? `transforms correctly using "${testFilePrefix}" data`
: "transforms correctly";
describe(transformName, () => {
it(testName, () => {
const output = runSingleTransform(
dirName,
transformName,
testFilePrefix,
transformObject,
action
);
expect(output).toMatchSnapshot();
});
});
}
module.exports = defineTest;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AddGenerator.html">AddGenerator</a></li><li><a href="InitGenerator.html">InitGenerator</a></li><li><a href="LoaderGenerator.html">LoaderGenerator</a></li><li><a href="PluginGenerator.html">PluginGenerator</a></li></ul><h3>Global</h3><ul><li><a href="global.html#addOrUpdateConfigObject">addOrUpdateConfigObject</a></li><li><a href="global.html#addProperty">addProperty</a></li><li><a href="global.html#createIdentifierOrLiteral">createIdentifierOrLiteral</a></li><li><a href="global.html#createLiteral">createLiteral</a></li><li><a href="global.html#createOrUpdatePluginByName">createOrUpdatePluginByName</a></li><li><a href="global.html#createProperty">createProperty</a></li><li><a href="global.html#defineTest">defineTest</a></li><li><a href="global.html#findAndRemovePluginByName">findAndRemovePluginByName</a></li><li><a href="global.html#findInvocation">findInvocation</a></li><li><a href="global.html#findPluginsArrayAndRemoveIfEmpty">findPluginsArrayAndRemoveIfEmpty</a></li><li><a href="global.html#findPluginsByName">findPluginsByName</a></li><li><a href="global.html#findRootNodesByName">findRootNodesByName</a></li><li><a href="global.html#findVariableToPlugin">findVariableToPlugin</a></li><li><a href="global.html#generatorCopy">generatorCopy</a></li><li><a href="global.html#generatorCopyTpl">generatorCopyTpl</a></li><li><a href="global.html#getPackageManager">getPackageManager</a></li><li><a href="global.html#getPathToGlobalPackages">getPathToGlobalPackages</a></li><li><a href="global.html#getRequire">getRequire</a></li><li><a href="global.html#getRootPathModule">getRootPathModule</a></li><li><a href="global.html#isType">isType</a></li><li><a href="global.html#loaderCreator">loaderCreator</a></li><li><a href="global.html#makeLoaderName">makeLoaderName</a></li><li><a href="global.html#mapOptionsToTransform">mapOptionsToTransform</a></li><li><a href="global.html#pluginCreator">pluginCreator</a></li><li><a href="global.html#processPromise">processPromise</a></li><li><a href="global.html#replaceAt">replaceAt</a></li><li><a href="global.html#resolvePackages">resolvePackages</a></li><li><a href="global.html#runMigration">runMigration</a></li><li><a href="global.html#runSingleTransform">runSingleTransform</a></li><li><a href="global.html#serve">serve</a></li><li><a href="global.html#spawnChild">spawnChild</a></li><li><a href="global.html#spawnNPM">spawnNPM</a></li><li><a href="global.html#spawnNPMWithArg">spawnNPMWithArg</a></li><li><a href="global.html#spawnYarn">spawnYarn</a></li><li><a href="global.html#spawnYarnWithArg">spawnYarnWithArg</a></li><li><a href="global.html#transform">transform</a></li><li><a href="global.html#traverseAndGetProperties">traverseAndGetProperties</a></li><li><a href="global.html#webpackGenerator">webpackGenerator</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Sun May 06 2018 17:19:19 GMT+0200 (CEST)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>