|
| 1 | +/** |
| 2 | + * Also in material design 3.. git |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + yarn add mocha -D |
| 7 | +
|
| 8 | + package.json |
| 9 | + "imports": { |
| 10 | + "##/*": { |
| 11 | + "default": "./*" |
| 12 | + }, |
| 13 | + }, |
| 14 | + "type": "module", |
| 15 | +
|
| 16 | + jsconfig.json |
| 17 | + { |
| 18 | + "compilerOptions": { |
| 19 | + "baseUrl": ".", |
| 20 | + "paths": { |
| 21 | + "##/*": ["./*"] |
| 22 | + } |
| 23 | + }, |
| 24 | + "exclude": ["node_modules", ".nuxt", "dist"] |
| 25 | +} |
| 26 | +
|
| 27 | +
|
| 28 | +
|
| 29 | + */ |
| 30 | +// import { createRequire } from 'module'; |
| 31 | +// const require = createRequire(import.meta.url); |
| 32 | +// const assert = require('assert'); |
| 33 | +// const {describe,it} = require('mocha'); |
| 34 | +import assert from 'node:assert'; |
| 35 | +import { describe, it} from 'mocha'; |
| 36 | +/* |
| 37 | +1. |
| 38 | +yarn add mocha @babel/polyfill @babel/register @babel/preset-env babel-plugin-module-resolver --dev |
| 39 | +yarn add @babel/core --dev |
| 40 | +2. |
| 41 | +-r @babel/register -r babel-plugin-module-resolver |
| 42 | +
|
| 43 | +3. |
| 44 | +.babelrc |
| 45 | +{ |
| 46 | +
|
| 47 | + "presets": ["@babel/preset-env"], |
| 48 | + "plugins": [ |
| 49 | + ["module-resolver", { |
| 50 | + "root": ["./src"], |
| 51 | + "alias": { |
| 52 | + "test": "./test", |
| 53 | + "underscore": "lodash", |
| 54 | +
|
| 55 | + "~": "./" |
| 56 | + } |
| 57 | + }] |
| 58 | + ] |
| 59 | +
|
| 60 | +} |
| 61 | +test specific timeout |
| 62 | +this.timeout(500);//500ms |
| 63 | +*/ |
| 64 | +/** |
| 65 | + * Should put this somewhere safe |
| 66 | + * todo filepath needs to be initialized as well... |
| 67 | + * @param fileName .json |
| 68 | + * @param data will automatically be changed |
| 69 | + */ |
| 70 | +import fs from 'node:fs'; |
| 71 | +function writeToFile(fileName,data,space=2){ |
| 72 | + const sFileName = /\./.test(fileName) ? fileName : fileName + '.json'; |
| 73 | + const filePath = `dev/pbs/test/${sFileName}` |
| 74 | + fs.writeFileSync(filePath, |
| 75 | + typeof data === 'string' ? data :JSON.stringify(data,null,+space) |
| 76 | + ); |
| 77 | +} |
| 78 | +import {relative,resolve,join} from "node:path/posix"; |
| 79 | +import {fileURLToPath} from "url"; |
| 80 | + |
| 81 | +describe('vite-press-path-utils.test.mjs', function(){ |
| 82 | + let viteConfigFile; |
| 83 | + /** @type {'docs/.vitepress' | string} - .vitepress config */ |
| 84 | + viteConfigFile = 'docs/.vitepress'; |
| 85 | + |
| 86 | + const testMatrix = [ |
| 87 | + //"" is cwd |
| 88 | + [resolve(""),"../.."],//,cwd, new URL('./',import.meta.url).pathname??? |
| 89 | + [resolve("./src"),"../../src"],//cwd/src, cant be ../src |
| 90 | + [resolve("src"),"../../src"],//cwd/src, same as above. left is okay to repeat |
| 91 | + [resolve("./docs/src"),"../src"], |
| 92 | + // [resolve("./docs"),"../"],//'../' also works and is more clear |
| 93 | + [resolve("./docs"),".."],//'../' also works and is more clear |
| 94 | + ] |
| 95 | + it('first try', function(){ |
| 96 | + for (let i = 0; i < testMatrix.length; i++) { |
| 97 | + const [expectedPath,inputRelativePathParam] = testMatrix[i]; |
| 98 | + // destDirCWD = ''; |
| 99 | + // const out = relative(inputRelativePathParam,viteConfigFile); |
| 100 | + /** Resolve docs/.vitepress with relative path to destination dir */ |
| 101 | + const out = resolve(viteConfigFile,inputRelativePathParam);//reverse |
| 102 | + // const out = resolve('./',viteConfigFile); |
| 103 | + // console.log(import.meta.url); |
| 104 | + // const tmp = fileURLToPath(new URL('../..', import.meta.url)) |
| 105 | + assert.strictEqual(out,expectedPath); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + //assert.strictEqual(1,1);//require assert |
| 110 | + }); |
| 111 | + it('reverse use case', function(){ |
| 112 | + for (let i = 0; i < testMatrix.length; i++) { |
| 113 | + const [inputDestinationPath,expectedRelativePath] = testMatrix[i]; //reversed |
| 114 | + const out = relative(viteConfigFile, inputDestinationPath);// some of the relative paths may not work |
| 115 | + assert.strictEqual(out,expectedRelativePath); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + //assert.strictEqual(1,1);//require assert |
| 120 | + }); |
| 121 | + |
| 122 | +}); |
0 commit comments