electron-vite / vite-plugin-electron

:electron: Electron⚡️Vite core repo
https://github.com/electron-vite
MIT License
691 stars 56 forks source link

fix: notBundle test is failed #247

Closed think-flow closed 2 months ago

think-flow commented 4 months ago

环境:windows 11 node版本:v20.10.0 当我运行notBundle测试时会出现以下错误

 FAIL  test/plugin.test.ts > src/plugin > notBundle
AssertionError: expected '"use strict";\r\nvar __create = Objec…' to equal '"use strict";\nvar __create = Object.…'

- Expected
+ Received

- "use strict";
- var __create = Object.create;
- var __defProp = Object.defineProperty;
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
- var __getOwnPropNames = Object.getOwnPropertyNames;
- var __getProtoOf = Object.getPrototypeOf;
- var __hasOwnProp = Object.prototype.hasOwnProperty;
- var __copyProps = (to, from, except, desc) => {
-   if (from && typeof from === "object" || typeof from === "function") {
-     for (let key of __getOwnPropNames(from))
-       if (!__hasOwnProp.call(to, key) && key !== except)
-         __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
-   }
-   return to;
- };
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
-   // If the importer is in node compatibility mode or this is not an ESM
-   // file that has been converted to a CommonJS file using a Babel-
-   // compatible transform (i.e. "__esModule" has not been set), then set
-   // "default" to the CommonJS "module.exports" for node compatibility.
-   isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
-   mod
- ));
- const vite = require("vite");
- const message = "foo";
- console.log(import("vite"));
- console.log(vite);
- console.log(message);
+ "use strict";
+ var __create = Object.create;
+ var __defProp = Object.defineProperty;
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
+ var __getOwnPropNames = Object.getOwnPropertyNames;
+ var __getProtoOf = Object.getPrototypeOf;
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
+ var __copyProps = (to, from, except, desc) => {
+   if (from && typeof from === "object" || typeof from === "function") {
+     for (let key of __getOwnPropNames(from))
+       if (!__hasOwnProp.call(to, key) && key !== except)
+         __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
+   }
+   return to;
+ };
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
+   // If the importer is in node compatibility mode or this is not an ESM
+   // file that has been converted to a CommonJS file using a Babel-
+   // compatible transform (i.e. "__esModule" has not been set), then set
+   // "default" to the CommonJS "module.exports" for node compatibility.
+   isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
+   mod
+ ));
+ const vite = require("vite");
+ const message = "foo";
+ console.log(import("vite"));
+ console.log(vite);
+ console.log(message);

 ❯ test/plugin.test.ts:36:22
     34|     const normalSnapMain = snapMain.replace(normalizingNewLineRE, '\n')
     35|
     36|     expect(distMain).equal(snapMain)
       |                      ^
     37|   })
     38| })

经检查是因为两个文件的换行符不一致。 __snapshots__/external-main.js\r\n,而生成出来的dis/external-main.js\n 我个人认为,两个文件的差异只有不可见的换行符时,可以通过标准化换行符后进行测试,而不影响该测试原来的目的

iugo commented 1 month ago

历史原因 Windows 下一般使用 CRLF, 但是 Linux, macOS 这样的类 UNIX 一般使用 LF (古早版本的 macOS 使用 CR 的情况现在已经罕见).

@think-flow

我打开 https://github.com/electron-vite/vite-plugin-electron/blob/4aae5577faa39928d3ab6af35bde827a91c29be9/test/__snapshots__/external-main.js 看了一下, 应该是 LF 的.

所以怀疑您是否转过结构, 导致您本地 __snapshots__/external-main.js 文件是 CRLF?

think-flow commented 1 month ago

历史原因 Windows 下一般使用 CRLF, 但是 Linux, macOS 这样的类 UNIX 一般使用 LF (古早版本的 macOS 使用 CR 的情况现在已经罕见).

@think-flow

我打开 https://github.com/electron-vite/vite-plugin-electron/blob/4aae5577faa39928d3ab6af35bde827a91c29be9/test/__snapshots__/external-main.js 看了一下, 应该是 LF 的.

所以怀疑您是否转过结构, 导致您本地 __snapshots__/external-main.js 文件是 CRLF?

感谢您的指正,我的本地__snapshots__/external-main.js确实是CRLF。 经过排查,是因为我使用的windows版git没有配置core.autocrlf,导致git将我拉取的代码由LF转换为了CRLF。 我会谨记这次疏漏。 再次感谢