@dannysir/js-te 0.4.0 → 0.4.1

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.
package/README.md CHANGED
@@ -3,7 +3,20 @@
3
3
  Jest에서 영감을 받아 만든 가벼운 JavaScript 테스트 프레임워크입니다.
4
4
 
5
5
 
6
- ## [📎 최근 업데이트 0.4.0v](https://github.com/dannysir/js-te-package/blob/main/CHANGELOG.md)
6
+ ## [📎 최근 업데이트 0.4.1v](https://github.com/dannysir/js-te-package/blob/main/CHANGELOG.md)
7
+
8
+ ### `mock(path, mockObj)` 함수 개선
9
+ - 기존 `path`를 절대 경로만 등록 가능한 부분에 절대 경로도 가능하게 수정
10
+
11
+ ```js
12
+ test('[mock module] - mocking random function (absolute path)', async () => {
13
+ mock('../test-helper/random.js', { // 0.4.1부터 절대 경로도 등록 가능
14
+ random: () => 3,
15
+ });
16
+
17
+ expect(play()).toBe(30);
18
+ });
19
+ ```
7
20
 
8
21
  ### Mock Functions 기능 추가
9
22
  - `fn()` 함수로 모킹 가능한 함수 생성
@@ -278,9 +291,9 @@ test('mock 상태 초기화', () => {
278
291
 
279
292
  ### Module Mocking
280
293
 
281
- #### `mock(모듈 절대 경로), mock객체)`
294
+ #### `mock(모듈 경로), mock객체)`
282
295
 
283
- 모듈을 모킹합니다. import 하기 **전에** 호출해야 합니다.
296
+ 모듈을 모킹합니다. ~~import 하기 **전에** 호출해야 합니다.~~
284
297
 
285
298
  > **0.4.0 버전부터 `mock()` 함수가 모듈의 모든 함수를 자동으로 mock function으로 변환합니다.**
286
299
  >
@@ -288,8 +301,9 @@ test('mock 상태 초기화', () => {
288
301
 
289
302
  ### **🚨 주의사항 (매우 중요)**
290
303
 
291
- 1. **반드시 경로는 절대 경로로 입력해주세요.**
292
- - babel이 import문에서 절대 경로로 변환하여 확인을 하기 때문에 반드시 절대 경로로 등록해주세요.
304
+ 1. ~~**반드시 경로는 절대 경로로 입력해주세요.**~~
305
+ - ~~babel이 import문에서 절대 경로로 변환하여 확인을 하기 때문에 반드시 절대 경로로 등록해주세요.~~
306
+ > 0.4.1 버전부터 상대 경로 등록이 가능합니다.
293
307
  2. ~~import문을 반드시 mocking 이후에 선언해주세요.~~
294
308
  - ~~mocking 전에 import를 하게 되면 mocking되기 전의 모듈을 가져오게 됩니다.~~
295
309
 
@@ -407,7 +421,7 @@ import {play} from './game.js';
407
421
 
408
422
  test('랜덤 함수 모킹', async () => {
409
423
  // 1. 먼저 모킹
410
- mock('/Users/san/Js-Te/test-helper/random.js', { // 반드시 절대 경로로 등록
424
+ mock('/Users/san/Js-Te/test-helper/random.js', { // 0.4.1 부터 상대 경로도 가능
411
425
  random: () => 0.5
412
426
  });
413
427
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dannysir/js-te",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "JavaScript test library",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -1,6 +1,4 @@
1
- import path from 'path';
2
-
3
- import {BABEL} from "../constants/babel.js";
1
+ import {findAbsolutePath} from "./utils/pathHelper.js";
4
2
 
5
3
  export const createMockCollectorPlugin = (mockedPaths) => {
6
4
  return ({types: t}) => {
@@ -18,14 +16,8 @@ export const createMockCollectorPlugin = (mockedPaths) => {
18
16
 
19
17
  const mockPath = args[0].value;
20
18
  const currentFilePath = state.filename || process.cwd();
21
- const currentDir = path.dirname(currentFilePath);
22
19
 
23
- let absolutePath;
24
- if (mockPath.startsWith(BABEL.PERIOD)) {
25
- absolutePath = path.resolve(currentDir, mockPath);
26
- } else {
27
- absolutePath = mockPath;
28
- }
20
+ const absolutePath = findAbsolutePath(mockPath, currentFilePath);
29
21
 
30
22
  mockedPaths.add(absolutePath);
31
23
  }
@@ -1,10 +1,9 @@
1
-
2
1
  import {findAbsolutePath, shouldTransform} from "./utils/pathHelper.js";
3
2
  import {getModuleInfo} from "./utils/getModuleInfo.js";
4
3
  import {createNamespaceWrapper, createOriginalDeclaration, createWrapperFunction} from "./utils/wrapperCreator.js";
5
4
  import {BABEL, MOCK} from "../constants/babel.js";
6
5
 
7
- export const babelTransformImport = (mockedPaths = null) => {
6
+ export const babelTransform = (mockedPaths = null) => {
8
7
  return ({types: t}) => {
9
8
  return {
10
9
  visitor: {
@@ -177,7 +176,25 @@ export const babelTransformImport = (mockedPaths = null) => {
177
176
  nodePath.node.declarations = newDeclarations;
178
177
  nodePath.node._transformed = true;
179
178
  }
180
- }
179
+ },
180
+
181
+ CallExpression(nodePath, state) {
182
+ if (!t.isIdentifier(nodePath.node.callee, { name: 'mock' })) {
183
+ return;
184
+ }
185
+
186
+ const args = nodePath.node.arguments;
187
+ if (args.length < 1 || !t.isStringLiteral(args[0])) {
188
+ return;
189
+ }
190
+
191
+ const mockPath = args[0].value;
192
+ const currentFilePath = state.filename || process.cwd();
193
+
194
+ const absolutePath = findAbsolutePath(mockPath, currentFilePath);
195
+
196
+ nodePath.node.arguments[0] = t.stringLiteral(absolutePath);
197
+ },
181
198
  }
182
199
  };
183
200
  }
@@ -4,8 +4,8 @@ import {transformFiles} from "./utils/transformFiles.js";
4
4
 
5
5
  export const setupFiles = () => {
6
6
  const testFiles = findTestFiles(process.cwd());
7
- const mockedPaths = collectMockedPaths(testFiles);
8
7
  const sourceFiles = findAllSourceFiles(process.cwd());
8
+ const mockedPaths = collectMockedPaths(testFiles);
9
9
 
10
10
  for (const file of sourceFiles) {
11
11
  transformFiles(file, mockedPaths);
@@ -1,6 +1,6 @@
1
1
  import fs from 'fs';
2
2
  import {transformSync} from "@babel/core";
3
- import {babelTransformImport} from "../../babelPlugins/babelTransformImport.js";
3
+ import {babelTransform} from "../../babelPlugins/babelTransform.js";
4
4
  import {red} from "../../utils/consoleColor.js";
5
5
  import {BABEL} from "../../constants/babel.js";
6
6
 
@@ -17,7 +17,7 @@ export const transformFiles = (filePath, mockPath) => {
17
17
 
18
18
  const transformed = transformSync(originalCode, {
19
19
  filename: filePath,
20
- plugins: [babelTransformImport(mockPath)],
20
+ plugins: [babelTransform(mockPath)],
21
21
  parserOpts: {
22
22
  sourceType: BABEL.MODULE,
23
23
  plugins: ['dynamicImport']