Skip to content

Commit f9d70b7

Browse files
committed
test: getInstalledPkgJsonPath unit test
1 parent 977f505 commit f9d70b7

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

packages/tailwindcss-patch/src/patcher.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@ export function getInstalledPkgJsonPath(options: PatchOptions = {}) {
1616
basedir: options.basedir ?? process.cwd()
1717
})
1818

19-
const pkgJson = require(tmpJsonPath) as PackageJson
19+
return tmpJsonPath
2020
// https://github.com/sonofmagic/weapp-tailwindcss-webpack-plugin
2121
// only tailwindcss version > 3.0.0
22-
if (gte(pkgJson.version!, '3.0.0')) {
23-
return tmpJsonPath
24-
}
2522
} catch (error) {
2623
if ((<Error & { code: string }>error).code === 'MODULE_NOT_FOUND') {
27-
console.warn('Can\'t find npm pkg: `tailwindcss`, Please ensure it has been installed!')
24+
console.warn("Can't find npm pkg: `tailwindcss`, Please ensure it has been installed!")
2825
}
2926
}
3027
}
@@ -77,8 +74,12 @@ export function monkeyPatchForExposingContext(twDir: string, opt: InternalPatchO
7774

7875
export function internalPatch(pkgJsonPath: string | undefined, options: InternalPatchOptions): any | undefined {
7976
if (pkgJsonPath) {
77+
const pkgJson = require(pkgJsonPath) as PackageJson
8078
const twDir = path.dirname(pkgJsonPath)
81-
const result = monkeyPatchForExposingContext(twDir, options)
82-
return result
79+
if (gte(pkgJson.version!, '3.0.0')) {
80+
const result = monkeyPatchForExposingContext(twDir, options)
81+
return result
82+
}
83+
// no sth
8384
}
8485
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`patcher getInstalledPkgJsonPath baseDir options 1`] = `"node_modules\\tailwindcss\\package.json"`;
4+
5+
exports[`patcher getInstalledPkgJsonPath common options 1`] = `"node_modules\\tailwindcss\\package.json"`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { getInstalledPkgJsonPath } from '../src/patcher'
2+
import path from 'path'
3+
4+
describe('patcher', () => {
5+
it('getInstalledPkgJsonPath common options', () => {
6+
const pkgJsonPath = getInstalledPkgJsonPath()
7+
expect(pkgJsonPath).toBeTruthy()
8+
pkgJsonPath && expect(path.relative(process.cwd(), pkgJsonPath)).toMatchSnapshot()
9+
})
10+
11+
it('getInstalledPkgJsonPath baseDir options', () => {
12+
const pkgJsonPath = getInstalledPkgJsonPath({
13+
basedir: path.resolve(__dirname, '../../../')
14+
})
15+
expect(pkgJsonPath).toBeTruthy()
16+
})
17+
})

0 commit comments

Comments
 (0)