|
| 1 | +import { join } from 'node:path'; |
| 2 | +import { pathToFileURL } from 'node:url'; |
| 3 | +import { createSnapshotSerializer } from 'path-serializer'; |
| 4 | +import { expect, test } from 'vitest'; |
| 5 | + |
| 6 | +test('should work with __dirname', () => { |
| 7 | + expect.addSnapshotSerializer( |
| 8 | + createSnapshotSerializer({ |
| 9 | + root: __dirname, |
| 10 | + features: { |
| 11 | + escapeDoubleQuotes: false, |
| 12 | + addDoubleQuotes: false, |
| 13 | + }, |
| 14 | + }), |
| 15 | + ); |
| 16 | + |
| 17 | + const case0 = __dirname; |
| 18 | + expect(case0).toMatchInlineSnapshot('<ROOT>'); |
| 19 | + |
| 20 | + const case1 = join(__dirname, 'src', 'foo.ts'); |
| 21 | + expect(case1).toMatchInlineSnapshot('<ROOT>/src/foo.ts'); |
| 22 | + |
| 23 | + const case2 = pathToFileURL(case0).toString(); |
| 24 | + expect(case2).toMatchInlineSnapshot('<ROOT>'); |
| 25 | +}); |
| 26 | + |
| 27 | +test('real world case: nodejs win32 error stack', () => { |
| 28 | + let stack: string; |
| 29 | + if (process.platform === 'win32') { |
| 30 | + expect.addSnapshotSerializer( |
| 31 | + createSnapshotSerializer({ |
| 32 | + root: 'E:\\Project\\lynx-app', |
| 33 | + features: { |
| 34 | + escapeDoubleQuotes: false, |
| 35 | + addDoubleQuotes: false, |
| 36 | + }, |
| 37 | + }), |
| 38 | + ); |
| 39 | + stack = `× Error: Cannot find module 'E:\\Project\\lynx-app\\node_modules\\@lynx-js\\tasm\\build\\win32\\Release\\lepus.node' |
| 40 | + │ Require stack: |
| 41 | + │ - E:\\Project\\lynx-app\\node_modules\\@lynx-js\\tasm\\index.js |
| 42 | + │ at Module._resolveFilename (node:internal/modules/cjs/loader:1225:15) |
| 43 | + │ at Module._load (node:internal/modules/cjs/loader:1051:27) |
| 44 | + │ at Module.require (node:internal/modules/cjs/loader:1311:19) |
| 45 | + │ at require (node:internal/modules/helpers:179:18) |
| 46 | + │ at encode_napi (E:\\Project\\lynx-app\\node_modules\\@lynx-js\\tasm\\index.js:48:17) |
| 47 | + │ at Object.fn (file:///E:/Project/lynx-app/node_modules/@lynx-js/template-webpack-plugin/lib/LynxEncodePlugin.js:145:71) |
| 48 | + │ at Object.fn (file:///E:/Project/lynx-app/node_modules/@lynx-js/template-webpack-plugin/lib/LynxEncodePlugin.js:145:71)`; |
| 49 | + } else { |
| 50 | + expect.addSnapshotSerializer( |
| 51 | + createSnapshotSerializer({ |
| 52 | + root: '/usr/project/lynx-app', |
| 53 | + features: { |
| 54 | + escapeDoubleQuotes: false, |
| 55 | + addDoubleQuotes: false, |
| 56 | + }, |
| 57 | + }), |
| 58 | + ); |
| 59 | + stack = `× Error: Cannot find module '/usr/project/lynx-app/node_modules/@lynx-js/tasm/build/win32/Release/lepus.node' |
| 60 | + │ Require stack: |
| 61 | + │ - /usr/project/lynx-app/node_modules/@lynx-js/tasm/index.js |
| 62 | + │ at Module._resolveFilename (node:internal/modules/cjs/loader:1225:15) |
| 63 | + │ at Module._load (node:internal/modules/cjs/loader:1051:27) |
| 64 | + │ at Module.require (node:internal/modules/cjs/loader:1311:19) |
| 65 | + │ at require (node:internal/modules/helpers:179:18) |
| 66 | + │ at encode_napi (/usr/project/lynx-app/node_modules/@lynx-js/tasm/index.js:48:17) |
| 67 | + │ at Object.fn (file:///usr/project/lynx-app/node_modules/@lynx-js/template-webpack-plugin/lib/LynxEncodePlugin.js:145:71) |
| 68 | + │ at Object.fn (file:///usr/project/lynx-app/node_modules/@lynx-js/template-webpack-plugin/lib/LynxEncodePlugin.js:145:71)`; |
| 69 | + } |
| 70 | + expect(stack).toMatchInlineSnapshot(` |
| 71 | + × Error: Cannot find module '<ROOT>/node_modules/@lynx-js/tasm/build/win32/Release/lepus.node' |
| 72 | + │ Require stack: |
| 73 | + │ - <ROOT>/node_modules/@lynx-js/tasm/index.js |
| 74 | + │ at Module._resolveFilename (node:internal/modules/cjs/loader:1225:15) |
| 75 | + │ at Module._load (node:internal/modules/cjs/loader:1051:27) |
| 76 | + │ at Module.require (node:internal/modules/cjs/loader:1311:19) |
| 77 | + │ at require (node:internal/modules/helpers:179:18) |
| 78 | + │ at encode_napi (<ROOT>/node_modules/@lynx-js/tasm/index.js:48:17) |
| 79 | + │ at Object.fn (<ROOT>/node_modules/@lynx-js/template-webpack-plugin/lib/LynxEncodePlugin.js:145:71) |
| 80 | + │ at Object.fn (<ROOT>/node_modules/@lynx-js/template-webpack-plugin/lib/LynxEncodePlugin.js:145:71) |
| 81 | + `); |
| 82 | +}); |
0 commit comments