|
1 | 1 | import { join } from 'node:path';
|
2 |
| -import { afterEach, beforeEach, describe } from 'node:test'; |
3 | 2 | import { buildAndGetResults } from 'test-helper';
|
4 |
| -import { afterAll, beforeAll, expect, test, vi } from 'vitest'; |
| 3 | +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; |
5 | 4 |
|
6 | 5 | test('tree shaking is enabled by default, bar and baz should be shaken', async () => {
|
7 | 6 | const fixturePath = join(__dirname, 'default');
|
@@ -30,40 +29,45 @@ test('tree shaking is disabled by the user, bar and baz should be kept', async (
|
30 | 29 | `);
|
31 | 30 | });
|
32 | 31 |
|
33 |
| -test('minify is enabled by default in mf format, bar and baz should be minified', async () => { |
34 |
| - const fixturePath = join(__dirname, 'mf/default'); |
35 |
| - const nodeEnv = process.env.NODE_ENV; |
36 |
| - process.env.NODE_ENV = 'production'; |
37 |
| - const { mfExposeEntry } = await buildAndGetResults({ fixturePath }); |
38 |
| - process.env.NODE_ENV = nodeEnv; |
39 |
| - // biome-ignore format: snapshot |
40 |
| - expect(mfExposeEntry).toMatchInlineSnapshot(`""use strict";(globalThis["default_minify"]=globalThis["default_minify"]||[]).push([["249"],{163:function(__unused_webpack_module,__webpack_exports__,__webpack_require__){__webpack_require__.r(__webpack_exports__);__webpack_require__.d(__webpack_exports__,{foo:function(){return foo}});const foo=()=>{}}}]);"`); |
41 |
| -}); |
| 32 | +describe('tree shaking is disabled by the user, bar and baz should be kept', () => { |
| 33 | + beforeEach(() => { |
| 34 | + // 使用 vi.stubEnv 来模拟环境变量 |
| 35 | + vi.stubEnv('NODE_ENV', 'production'); |
| 36 | + }); |
42 | 37 |
|
43 |
| -test('minify is disabled by the user, bar and baz should not be minified', async () => { |
44 |
| - const fixturePath = join(__dirname, 'mf/config'); |
45 |
| - const nodeEnv = process.env.NODE_ENV; |
46 |
| - process.env.NODE_ENV = 'production'; |
47 |
| - const { mfExposeEntry } = await buildAndGetResults({ fixturePath }); |
48 |
| - process.env.NODE_ENV = nodeEnv; |
| 38 | + afterEach(() => { |
| 39 | + // 清理环境变量 |
| 40 | + vi.unstubAllEnvs(); |
| 41 | + }); |
49 | 42 |
|
50 |
| - expect(mfExposeEntry).toMatchInlineSnapshot(` |
51 |
| - ""use strict"; |
52 |
| - (globalThis['disable_minify'] = globalThis['disable_minify'] || []).push([["249"], { |
53 |
| - "163": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { |
54 |
| - __webpack_require__.r(__webpack_exports__); |
55 |
| - __webpack_require__.d(__webpack_exports__, { |
56 |
| - foo: function() { return foo; } |
57 |
| - }); |
58 |
| - const foo = ()=>{}; |
59 |
| - const bar = ()=>{}; |
60 |
| - const baz = ()=>{ |
61 |
| - return bar(); |
62 |
| - }; |
| 43 | + test('minify is enabled by default in mf format, bar and baz should be minified', async () => { |
| 44 | + const fixturePath = join(__dirname, 'mf/default'); |
| 45 | + const { mfExposeEntry } = await buildAndGetResults({ fixturePath }); |
| 46 | + // biome-ignore format: snapshot |
| 47 | + expect(mfExposeEntry).toMatchInlineSnapshot(`""use strict";(globalThis["default_minify"]=globalThis["default_minify"]||[]).push([["249"],{163:function(__unused_webpack_module,__webpack_exports__,__webpack_require__){__webpack_require__.r(__webpack_exports__);__webpack_require__.d(__webpack_exports__,{foo:function(){return foo}});const foo=()=>{}}}]);"`); |
| 48 | + }); |
63 | 49 |
|
| 50 | + test('minify is disabled by the user, bar and baz should not be minified', async () => { |
| 51 | + const fixturePath = join(__dirname, 'mf/config'); |
| 52 | + const { mfExposeEntry } = await buildAndGetResults({ fixturePath }); |
64 | 53 |
|
65 |
| - }), |
| 54 | + console.log(333, mfExposeEntry); |
| 55 | + expect(mfExposeEntry).toMatchInlineSnapshot(`""use strict"; |
| 56 | +(globalThis['disable_minify'] = globalThis['disable_minify'] || []).push([["249"], { |
| 57 | +"163": (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { |
| 58 | +__webpack_require__.r(__webpack_exports__); |
| 59 | +__webpack_require__.d(__webpack_exports__, { |
| 60 | + foo: function() { return foo; } |
| 61 | +}); |
| 62 | +const foo = ()=>{}; |
| 63 | +const bar = ()=>{}; |
| 64 | +const baz = ()=>{ |
| 65 | + return bar(); |
| 66 | +}; |
66 | 67 |
|
67 |
| - }]);" |
68 |
| - `); |
| 68 | +
|
| 69 | +}), |
| 70 | +
|
| 71 | +}]);"`); |
| 72 | + }); |
69 | 73 | });
|
0 commit comments