@dannysir/js-te 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,14 @@
1
- import {CHECK, CROSS, DIRECTORY_DELIMITER, EMPTY} from "../constants.js";
2
1
  import {green, red} from "./consoleColor.js";
2
+ import {RESULT_MSG} from "../constants/index.js";
3
3
 
4
4
  export const formatSuccessMessage = (test) => {
5
- const pathString = test.path === '' ? EMPTY : test.path + DIRECTORY_DELIMITER;
6
- return green(CHECK) + pathString + test.description;
5
+ const pathString = test.path === '' ? RESULT_MSG.EMPTY : test.path + RESULT_MSG.DIRECTORY_DELIMITER;
6
+ return green(RESULT_MSG.CHECK) + pathString + test.description;
7
7
  };
8
8
 
9
9
  export const formatFailureMessage = (test, error) => {
10
10
  const messages = [];
11
- messages.push(red(CROSS) + test.path + test.description);
11
+ messages.push(red(RESULT_MSG.CROSS) + test.path + test.description);
12
12
  messages.push(red(` Error Message : ${error.message}`));
13
13
  return messages.join('\n');
14
14
  };
@@ -16,6 +16,16 @@ export const formatFailureMessage = (test, error) => {
16
16
  export const getErrorMsg = (expect, actual) => {
17
17
  return `Expected ${JSON.stringify(expect)} but got ${JSON.stringify(actual)}`;
18
18
  };
19
+
19
20
  export const getThrowErrorMsg = (expect) => {
20
21
  return `Expected function to throw an error containing "${expect}", but it did not throw`;
21
- };
22
+ };
23
+
24
+ export const placeHolder = {
25
+ 's': (value) => value,
26
+ 'o': (value) => JSON.stringify(value),
27
+ };
28
+
29
+ export const getMatcherForReplace = () => {
30
+ return new RegExp(`%([${Object.keys(placeHolder).join('')}])`, 'g')
31
+ };
@@ -0,0 +1,26 @@
1
+ import {bold, green, red, yellow} from "./consoleColor.js";
2
+
3
+ export const getTestResultMsg = (title, success, fail) => {
4
+ let msg = '\n';
5
+
6
+ msg += title;
7
+ msg += green(success + ' passed') + ', ';
8
+ if (fail) {
9
+ msg += red(fail + ' failed') + ', ';
10
+ }
11
+ msg += bold(success + fail + ' total');
12
+
13
+ return msg;
14
+ };
15
+
16
+ export const getFileCountString = (n) => {
17
+ return `\nFound ${green(n)} test file(s)`;
18
+ };
19
+
20
+ export const getFilePath = (path) => {
21
+ return `\n${yellow(path)}\n`;
22
+ };
23
+
24
+ export const getErrorMsgInLogic = (error) => {
25
+ return red(`\n✗ Test execution failed\n Error: ${error}\n`)
26
+ };
@@ -1,11 +1,16 @@
1
1
  import fs from 'fs';
2
2
  import {transformSync} from "@babel/core";
3
- import {babelTransformImport} from "../../babelTransformImport.js";
4
- import {BABEL} from "../../constants.js";
5
- import {red} from "../../utils/consoleColor.js";
3
+ import {babelTransformImport} from "../babelPlugins/babelTransformImport.js";
4
+ import {red} from "./consoleColor.js";
5
+ import {BABEL} from "../constants/babel.js";
6
6
 
7
7
  const originalFiles = new Map();
8
8
 
9
+ /**
10
+ * 파일의 import/require문을 변경하는 함수입니다.
11
+ * @param {string} filePath - 파일 절대 경로
12
+ * @param {Set} mockPath - 모킹한 파일 저장소
13
+ */
9
14
  export const transformFiles = (filePath, mockPath) => {
10
15
  const originalCode = fs.readFileSync(filePath, 'utf-8');
11
16
  originalFiles.set(filePath, originalCode);
@@ -31,4 +36,4 @@ export const restoreFiles = () => {
31
36
  }
32
37
  }
33
38
  originalFiles.clear();
34
- };
39
+ };
@@ -1,317 +0,0 @@
1
- import path from 'path';
2
- import {BABEL, MOCK} from "./constants.js";
3
-
4
- export const babelTransformImport = (mockedPaths = null) => {
5
- return ({types: t}) => {
6
- return {
7
- visitor: {
8
- Program(path) {
9
- const mockStoreDeclaration = t.VariableDeclaration('const', [
10
- t.VariableDeclarator(
11
- t.Identifier(MOCK.STORE_NAME),
12
- t.MemberExpression(
13
- t.Identifier('global'),
14
- t.Identifier(MOCK.STORE_NAME)
15
- )
16
- )
17
- ]);
18
- path.node.body.unshift(mockStoreDeclaration);
19
- },
20
-
21
- ImportDeclaration(nodePath, state) {
22
- const source = nodePath.node.source.value;
23
-
24
- if (source === MOCK.STORE_PATH) {
25
- return;
26
- }
27
-
28
- const currentFilePath = state.filename || process.cwd();
29
- const currentDir = path.dirname(currentFilePath);
30
-
31
- let absolutePath;
32
- if (source.startsWith(BABEL.PERIOD)) {
33
- absolutePath = path.resolve(currentDir, source);
34
- } else {
35
- absolutePath = source;
36
- }
37
-
38
- if (mockedPaths && !mockedPaths.has(absolutePath)) {
39
- return;
40
- }
41
-
42
- const specifiers = nodePath.node.specifiers;
43
- const originalVarName = nodePath.scope.generateUidIdentifier('original');
44
-
45
- /*
46
-
47
- const _original = await import('./random.js');
48
- const random = (...args) => {
49
- const module = mockStore.has('/path/to/random.js')
50
- ? { ..._original, ...mockStore.get('/path/to/random.js') }
51
- : _original;
52
- return module.random(...args);
53
- };
54
- */
55
-
56
- const originalDeclaration = t.variableDeclaration(BABEL.CONST, [
57
- t.variableDeclarator(
58
- originalVarName,
59
- t.awaitExpression(
60
- t.importExpression(t.stringLiteral(source))
61
- )
62
- )
63
- ]);
64
-
65
- const wrapperDeclarations = specifiers.map(spec => {
66
- let importedName, localName;
67
-
68
- if (t.isImportDefaultSpecifier(spec)) {
69
- importedName = 'default';
70
- localName = spec.local.name;
71
- } else if (t.isImportNamespaceSpecifier(spec)) {
72
- localName = spec.local.name;
73
- return t.variableDeclarator(
74
- t.identifier(localName),
75
- t.conditionalExpression(
76
- t.callExpression(
77
- t.memberExpression(t.identifier(MOCK.STORE_NAME), t.identifier(BABEL.HAS)),
78
- [t.stringLiteral(absolutePath)]
79
- ),
80
- t.objectExpression([
81
- t.spreadElement(originalVarName),
82
- t.spreadElement(
83
- t.callExpression(
84
- t.memberExpression(t.identifier(MOCK.STORE_NAME), t.identifier(BABEL.GET)),
85
- [t.stringLiteral(absolutePath)]
86
- )
87
- )
88
- ]),
89
- originalVarName
90
- )
91
- );
92
- } else {
93
- importedName = spec.imported.name;
94
- localName = spec.local.name;
95
- }
96
-
97
- return t.variableDeclarator(
98
- t.identifier(localName),
99
- t.arrowFunctionExpression(
100
- [t.restElement(t.identifier('args'))],
101
- t.blockStatement([
102
- t.variableDeclaration(BABEL.CONST, [
103
- t.variableDeclarator(
104
- t.identifier(BABEL.MODULE),
105
- t.conditionalExpression(
106
- t.callExpression(
107
- t.memberExpression(t.identifier(MOCK.STORE_NAME), t.identifier(BABEL.HAS)),
108
- [t.stringLiteral(absolutePath)]
109
- ),
110
- t.objectExpression([
111
- t.spreadElement(originalVarName),
112
- t.spreadElement(
113
- t.callExpression(
114
- t.memberExpression(t.identifier(MOCK.STORE_NAME), t.identifier(BABEL.GET)),
115
- [t.stringLiteral(absolutePath)]
116
- )
117
- )
118
- ]),
119
- originalVarName
120
- )
121
- )
122
- ]),
123
- t.returnStatement(
124
- t.callExpression(
125
- t.memberExpression(t.identifier(BABEL.MODULE), t.identifier(importedName)),
126
- [t.spreadElement(t.identifier('args'))]
127
- )
128
- )
129
- ])
130
- )
131
- );
132
- });
133
-
134
- const wrapperDeclaration = t.variableDeclaration(BABEL.CONST, wrapperDeclarations);
135
-
136
- nodePath.replaceWithMultiple([originalDeclaration, wrapperDeclaration]);
137
- },
138
-
139
- VariableDeclaration(nodePath, state) {
140
- const declarations = nodePath.node.declarations;
141
-
142
- if (!declarations || declarations.length === 0) {
143
- return;
144
- }
145
-
146
- if (nodePath.node._transformed) {
147
- return;
148
- }
149
-
150
- const newDeclarations = [];
151
- let hasTransformation = false;
152
-
153
- for (const declarator of declarations) {
154
- const init = declarator.init;
155
-
156
- if (!init ||
157
- !t.isCallExpression(init) ||
158
- !t.isIdentifier(init.callee, {name: 'require'}) ||
159
- !init.arguments[0] ||
160
- !t.isStringLiteral(init.arguments[0])) {
161
- newDeclarations.push(declarator);
162
- continue;
163
- }
164
-
165
- const source = init.arguments[0].value;
166
- const currentFilePath = state.filename || process.cwd();
167
- const currentDir = path.dirname(currentFilePath);
168
-
169
- let absolutePath;
170
- if (source.startsWith(BABEL.PERIOD)) {
171
- absolutePath = path.resolve(currentDir, source);
172
- } else {
173
- absolutePath = source;
174
- }
175
-
176
- if (mockedPaths && !mockedPaths.has(absolutePath)) {
177
- newDeclarations.push(declarator);
178
- continue;
179
- }
180
-
181
- hasTransformation = true;
182
- const originalVar = nodePath.scope.generateUidIdentifier('original');
183
-
184
- /*
185
-
186
- const _original = require('./random');
187
- const random = (...args) => {
188
- const module = mockStore.has('/path/to/random.js')
189
- ? { ..._original, ...mockStore.get('/path/to/random.js') }
190
- : _original;
191
- return module.random(...args);
192
- };
193
- */
194
-
195
- newDeclarations.push(
196
- t.variableDeclarator(
197
- originalVar,
198
- t.callExpression(
199
- t.identifier('require'),
200
- [t.stringLiteral(source)]
201
- )
202
- )
203
- );
204
-
205
- if (t.isObjectPattern(declarator.id)) {
206
- const properties = declarator.id.properties;
207
-
208
- properties.forEach(prop => {
209
- const key = prop.key.name;
210
- const localName = prop.value.name;
211
-
212
- newDeclarations.push(
213
- t.variableDeclarator(
214
- t.identifier(localName),
215
- t.arrowFunctionExpression(
216
- [t.restElement(t.identifier('args'))],
217
- t.blockStatement([
218
- t.variableDeclaration(BABEL.CONST, [
219
- t.variableDeclarator(
220
- t.identifier(BABEL.MODULE),
221
- t.conditionalExpression(
222
- t.callExpression(
223
- t.memberExpression(
224
- t.identifier(MOCK.STORE_NAME),
225
- t.identifier(BABEL.HAS)
226
- ),
227
- [t.stringLiteral(absolutePath)]
228
- ),
229
- t.objectExpression([
230
- t.spreadElement(originalVar),
231
- t.spreadElement(
232
- t.callExpression(
233
- t.memberExpression(
234
- t.identifier(MOCK.STORE_NAME),
235
- t.identifier(BABEL.GET)
236
- ),
237
- [t.stringLiteral(absolutePath)]
238
- )
239
- )
240
- ]),
241
- originalVar
242
- )
243
- )
244
- ]),
245
- t.returnStatement(
246
- t.callExpression(
247
- t.memberExpression(
248
- t.identifier(BABEL.MODULE),
249
- t.identifier(key)
250
- ),
251
- [t.spreadElement(t.identifier('args'))]
252
- )
253
- )
254
- ])
255
- )
256
- )
257
- );
258
- });
259
- } else {
260
- newDeclarations.push(
261
- t.variableDeclarator(
262
- declarator.id,
263
- t.arrowFunctionExpression(
264
- [t.restElement(t.identifier('args'))],
265
- t.blockStatement([
266
- t.variableDeclaration(BABEL.CONST, [
267
- t.variableDeclarator(
268
- t.identifier(BABEL.MODULE),
269
- t.conditionalExpression(
270
- t.callExpression(
271
- t.memberExpression(
272
- t.identifier(MOCK.STORE_NAME),
273
- t.identifier(BABEL.HAS)
274
- ),
275
- [t.stringLiteral(absolutePath)]
276
- ),
277
- t.objectExpression([
278
- t.spreadElement(originalVar),
279
- t.spreadElement(
280
- t.callExpression(
281
- t.memberExpression(
282
- t.identifier(MOCK.STORE_NAME),
283
- t.identifier(BABEL.GET)
284
- ),
285
- [t.stringLiteral(absolutePath)]
286
- )
287
- )
288
- ]),
289
- originalVar
290
- )
291
- )
292
- ]),
293
- t.returnStatement(
294
- t.callExpression(
295
- t.memberExpression(
296
- t.identifier(BABEL.MODULE),
297
- t.identifier('default')
298
- ),
299
- [t.spreadElement(t.identifier('args'))]
300
- )
301
- )
302
- ])
303
- )
304
- )
305
- );
306
- }
307
- }
308
-
309
- if (hasTransformation) {
310
- nodePath.node.declarations = newDeclarations;
311
- nodePath.node._transformed = true;
312
- }
313
- }
314
- }
315
- };
316
- }
317
- };
package/constants.js DELETED
@@ -1,46 +0,0 @@
1
- export const RESULT_TITLE = {
2
- TESTS: 'Tests: ',
3
- TOTAL : 'Total Result: '
4
- };
5
-
6
- export const CHECK = '✓ ';
7
-
8
- export const CROSS = '✗ ';
9
-
10
- export const DIRECTORY_DELIMITER = ' > ';
11
-
12
- export const EMPTY = '';
13
-
14
- export const DEFAULT_COUNT = 0;
15
-
16
- export const COLORS = {
17
- reset: '\x1b[0m',
18
- green: '\x1b[32m',
19
- red: '\x1b[31m',
20
- yellow: '\x1b[33m',
21
- cyan: '\x1b[36m',
22
- gray: '\x1b[90m',
23
- bold: '\x1b[1m'
24
- };
25
-
26
- export const MOCK = {
27
- STORE_NAME: 'mockStore',
28
- STORE_PATH : '@dannysir/js-te/src/mock/store.js'
29
- };
30
-
31
- export const BABEL = {
32
- MODULE: 'module',
33
- CONST: 'const',
34
- HAS: 'has',
35
- GET: 'get',
36
- PERIOD: '.',
37
- };
38
-
39
- export const PATH = {
40
- NODE_MODULES: 'node_modules',
41
- TEST_DIRECTORY: 'test',
42
- TEST_FILE: '.test.js',
43
- JAVASCRIPT_FILE: '.js',
44
- BIN: 'bin',
45
- DIST: 'dist',
46
- };
package/src/testRunner.js DELETED
@@ -1,28 +0,0 @@
1
- import {DEFAULT_COUNT, RESULT_TITLE} from "../constants.js";
2
- import {testManager} from "./testManager.js";
3
- import {formatFailureMessage, formatSuccessMessage} from "../utils/formatString.js";
4
- import {getTestResultMsg} from "../utils/makeMessage.js";
5
- import {clearAllMocks} from "./mock/store.js";
6
-
7
- export const run = async () => {
8
- let passed = DEFAULT_COUNT;
9
- let failed = DEFAULT_COUNT;
10
-
11
- for (const test of testManager.getTests()) {
12
- try {
13
- await test.fn();
14
- console.log(formatSuccessMessage(test));
15
- passed++;
16
- clearAllMocks();
17
- } catch (error) {
18
- console.log(formatFailureMessage(test, error));
19
- failed++;
20
- }
21
- }
22
-
23
- console.log(getTestResultMsg(RESULT_TITLE.TESTS, passed, failed));
24
-
25
- testManager.clearTests();
26
-
27
- return {passed, failed};
28
- };
@@ -1,14 +0,0 @@
1
- import {bold, green, red} from "./consoleColor.js";
2
-
3
- export const getTestResultMsg = (title, success, fail) => {
4
- let msg = '\n';
5
-
6
- msg += title;
7
- msg += green(success + ' passed') + ', ';
8
- if (fail) {
9
- msg += red(fail + ' failed') + ', ';
10
- }
11
- msg += bold(success + fail + ' total');
12
-
13
- return msg;
14
- };